This Ruby on Rails plugin adds a .filtered_by
method to your models. It accepts a hash of filters that are applied using scopes. In addition the plugin adds some view helpers to easily build filter forms.
You can install filter_fu as a gem or as a rails plugin.
To install the filter_fu gem, simply add it to your Gemfile:
gem 'filter_fu'
In order to get the latest development version of filter_fu:
gem 'filter_fu', :git => 'git://github.com/benedikt/filter_fu.git'
and run
bundle install
To enable filter_fu on a model, simply add one line to its definition:
class Project < ActiveRecord::Base filter_fu end
Let’s say you don’t want your Projects list filtered by its hidden
column. You can easly tell filter_fu to ignore certian named scopes or columns:
class Project < ActiveRecord::Base filter_fu :except => [:hidden] end
Of course this also works the other way round using the :only
option to only allow filtering for the given named scopes or columns:
class Project < ActiveRecord::Base filter_fu :only => [:starts_on, :ends_on] end
Once you enabled filter_fu on your model it provides a filtered_by method.
Project.filtered_by(params[:filter]) Project.filtered_by(params[:filter]).some.named_scopes Project.some.other.scopes.filtered_by(params[:filter])
filter_fu comes with helpers to simplify the generation of filter forms:
<%= filter_form do |f| %> # f is a ActionView::Helpers::FormBuilder <% end %>
You can also specify a name for the filter (Default is :filter
). This way you’re able to have multiple filters on the same page.
<%= filter_form_for(:product_filter) do |f| %> # f is a ActionView::Helpers::FormBuilder <% end %>
filter_fu will automatically include all other parameters for the current page (ie. pagination parameters) within the form. If you wish to explicitly exclude some you can do this by passing an array as :ignore_parameters option.
<%= filter_form_for(:product_filter, :ignore_parameters => [:page]) do |f| %> # f is a ActionView::Helpers::FormBuilder <% end %>
It’s possible to add default values for :ignore_parameters
. Simply add something like this to your initializers:
config.filter_fu.ignore_parameters = [:param_to_ignore]
See github.com/benedikt/filter_fu/issues
See github.com/benedikt/filter_fu and feel free to fork it!
Copyright © 2009, 2010 Benedikt Deicke. See LICENSE for details.