daniel
daniel

Reputation: 3115

activeadmin custom filter that contains 2 words on the same attribute

I am using Rails 4.2 with activeadmin 1.0.0.pre1 and postgresql 9.3.

My model attribute is a string.

Right now if I filter for "invoice 2011" it will give me results that contain "invoice 2011", which is the default behavior if you select "contains". I want results that either contain "invoice" or "2011" in my case.

I know there is a cont_any chain method on ransack. This is what I am trying to achieve in a filter:

Model.search(attribute_cont_any: %w(invoice 2011))

Upvotes: 0

Views: 502

Answers (1)

Timo Schilling
Timo Schilling

Reputation: 3073

There is no solution at the moment, but maybe this will help you.

You can use the cont_any filter by doing this:

filter :attribute, filters: [:cont_any] # or [:start, :cont_any, ...]

But there are some problems at the moment:

  1. That produces a url like: ?q[attribute_cont_any]=invoice+2011, but it need's to be ?q[attribute_cont_any][]=invoice&q[attribute_cont_any][]=2011.

    1. Maybe you can write a Javascript that will manipulate the form to split the one input into two.
    2. Or you can write a before_action which splits the params params[:attribute_cont_any] = params[:attribute_cont_any].split
  2. If you request the correct URL ?q[attribute_cont_any][]=invoice&q[attribute_cont_any][]=2011, it will return the right result, but the value of the filter input is now `["invoice", "2011"]

    This can maybe fixed by placing a join in the ActiveAdmin code

Upvotes: 2

Related Questions