Filterrific
Filterrific is a Rails Engine plugin that makes it easy to filter, search, and sort your ActiveRecord lists.
Live demo using classic Rails views.
Live demo of JSON API using React and Mobx.
It comes with the following features:
- Let your app’s users search, filter and sort lists of ActiveRecord objects.
- Persist filter settings in the HTTP session or DB (for saved searches).
- Integrates with pagination.
- Filters can be reset to default settings.
- Relies on ActiveRecord scopes for building DB queries.
- Shuttles filter settings from a filter UI to the controller and ActiveRecord.
- Can be used for HTML/JS/JSON/XML response formats.
You only have to define the required ActiveRecord scopes and style your filter form and record lists.
I couldn't live without Filterrific. It makes it super easy to add user configurable reporting to my client projects.
Jeff Ward, Animikii
How to use it
Let’s say you want a list of students that can be filtered by your app’s users.
-
Add the gem to your app
1 2
# Gemfile gem 'filterrific'
-
Add Filterrific to your
Student
model:1 2 3 4 5 6 7 8 9 10 11
filterrific( default_filter_params: { sorted_by: 'created_at_desc' }, available_filters: [ :sorted_by, :search_query, :with_country_id, :with_created_at_gte ] ) # define ActiveRecord scopes for # :search_query, :sorted_by, :with_country_id, and :with_created_at_gte
-
Use Filterrific in
StudentsController#index
:1 2 3 4 5 6 7 8 9 10 11 12
def index @filterrific = initialize_filterrific( Student, params[:filterrific] ) or return @students = @filterrific.find.page(params[:page]) respond_to do |format| format.html format.js end end
-
And finally build your view files as explained in the View API:
Learn about the example application →
Resources
Dependencies
- Rails and ActiveRecord 3.x, 4.x, or 5.x
- jQuery and Asset pipeline for form observers and spinner