This gem will provide a few basic features for filtering:
- Filter method for the ActiveRecord model – Rails 3 only
- Builds a filter form with a single call
gem install filterable-models
Unfortunately there is not enough documentation yet. However, I can explain how it works in a few lines:
Lets take a Product as an example, which has a name, a code, a state and belongs to many categories:
On the model, just add these lines:
- Tells the model what it is filtering for
class Product < ActiveRecord::Base filters :name => "string", :code => "exact_string", :created_at => "date_range", :price => "float_range", :category => {:name => "string"} end
- Options for a drop down menu
class Product < ActiveRecord::Base filters :state => "drop_down" filter_options_for :state, ["active", "on hold", "inactive"] end
- Or you could use a hash for the drop down menu as well:
class Product < ActiveRecord::Base filters :state => "drop_down" filter_options_for :state, {"Active" => "active", "On Hold" => "on_hold", "Inactive" => "inactive"} end
On your controller, you can use the filter scope instead of your common all() or paginate() method.
Replace
def index
@products = Product.all
end
By
def index
@products = Product.filter(:name => "Computer")
end
You can also try these:
# Filtering by an exact string
@products = Product.filter(:code => "P-001")
# Filtering a date_range
@products = Product.filter(:created_at_from => 1.year.ago, :created_at_to => 1.day.ago)
# Filtering a float_range
@products = Product.filter(:price_from => 1.20, :price_to => 1.99)
# Filtering related objects (as on habtm associations)
@products = Product.filter(:category => {:name => "Electronics"})
Note: If you are submitting by the form generated by the code below, then the params hash will
have the filters hash, which should be used on the Product.filter method, like this:
params[:filters] ||= {}
@products = Product.filter(params[:filters])
Last, but not least, on your view file you can have it build a filter form automatically:
<%= filter_form_for :product, params %>
This will generate a filter form based on what you defined for your model as filterable.
The second attribute is your params hash, so that it can be used to pre-populate the fields
when some filters are already in place.
- NOTE 1: The form will be prepared using lists of fields for each of the filterable elements in your model. Those lists can
become pretty big so you would like to divide them into columns, or different <ul> elements. Where that is the case, use
the following option to define how many fields per column you want the system to build for you (Only one field per column for
your form in this example. Dont forget to float them ‘left’ on your css):
class Product < ActiveRecord::Base filters :name => "string", :code => "exact_string" filter_fields_per_column 1 end
- NOTE 2: Once Ruby 1.8 doesnt sort hash elements in the order you defined them, it may be a little bit unpredictable to use
a hash for filters. I have added the functionality to define them in an array, in case you want to have total control of
the order the fields come in. I find it ugly so open to ideas to make it better for 1.8:
class Product < ActiveRecord::Base filters [{:name => "string"}, {:code => "exact_string"}, {:created_at => "date_range"}, {:price => "float_range"}, {:category => {:name => "string"}}] end
I have written a few tests, but those are within my application which uses the plugin, so
still need to think about a way to properly test the plugin.
Also, more form elements for the filter form are on their way, like autocomplete items or
checkboxes, dropdown date items, etc.
Please submit any bugs or annoyances to [email protected] or message me at
github: marcelorocks
I have just started to collaborate with the opensource community, so still learning how to do stuff properly! :-D
I am more than open for patches or anything to make it better, of course