Gem of the Week: Filterrific

filterrific

I recently ran into a situation where I needed to add responsive and fast search functionality for a database of over 300,000 records. Enter the filterrific gem. This gem has some of the best documentation I’ve found and even has a live demo where you can play around with the search functionality and a corresponding github repo so you can investigate the underlying code of the demo.  It’s definitely worth checking out all the resources for this very powerful gem.

The gem itself can be as simple or complicated as you need it to be. To get started, just add

gem ‘filterrific’

to your Gemfile. Then add the filterrific parameters to whichever Model you are needing the filtering/searching functionality for, specifying all the filters that you are wanting. The example from the documentation is:

filterrific(
    default_filter_params: { sorted_by: 'created_at_desc' },
    available_filters: [
        :sorted_by,
        :search_query,
        :with_country_id
    ]
)

You would then need to define scopes for each of the filters listed in your Model. For example, you can set up :sorted_by as follows:

scope :sorted_by, lambda { |sort_option|
    # extract the sort direction from the param value.
    direction = (sort_option =~ /asc$/) ? 'asc' : 'desc'
    case sort_option.to_s
    when /^account_id_/
        order("account_id #{ direction }")
    when /^state_/
        order("LOWER(state) #{ direction }")
    when /^account_status_/
        order("LOWER(account_status)")
    else
        raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
    end

}

Or you could set up :with_country_id as follows:


scope :with_country_id, lambda { |country_ids|

    return nil if country_ids.blank?

   where( :country_id => [*country_ids])

}

The docs have several examples of different filters that are great places to start. You can also check out this interesting tutorial that discusses filtering by multiple options to see some more complex uses of the gem.

After you have your Model setup with its filters, you will need to edit your corresponding Controller:

def index
    @filterrific = initialize_filterrific(
        Student,
        params[:filterrific]
    )or return
    @students = @filterrific.find.page(params[:page])
end

Last but not least, you will need to adjust your views to utilize the filterrific functionality that you have created. Here, again, the developers of filterrific have outdone themselves with their ActionView API documentation, which discusses adding AJAX to your filter/search form to make your filters responsive. An example final product would be:

filterrific-view

 

And that’s basically all there is to it! You can quickly and easily create a complex and multifaceted filter that is very fast and easy to use. I strongly encourage you to take the time to read through all the documentation that the amazing developers of this gem have put together. And if you want to play around with filtering a large amount of data, be sure to check out our article on Faker to learn about seeding massive amounts of high quality data quickly!