Gem for comfortable filtering of ActiveRecord queries.
Add this line to your application's Gemfile:
gem 'filtering'
And then execute:
$ bundle
Or install it yourself as:
$ gem install filtering
All you need to do is inherit custom filter service from Filtering::Base
and define params for filtering.
class Filters::UsersFilter < Filtering::Base
def initialize(params, args)
super(params, args)
end
private
# Required methods
def relation
User.all
end
def plain_acessible_params
%i[city age]
end
def complex_acessible_params
%i[name]
end
# Complex filters
def filter_by_name(name)
result.where('name ILIKE ? or name_auto ILIKE ?', "%#{name}%", "%#{name}%")
end
end
There are private methods:
relation
is an ActiveRecord initial relation which must be filtered
plain_acessible_params
array of params for an auto-filtering by where
, so if you have %i[city age]
will be called User.where(city: city).where(age: age)
complex_acessible_params
array of params for filters with custom logic. If you have some params in that method you have to create methods for those custom filters with format: filter_by_{param}
Controller usage example:
def index
render json: Filters::UsersFilter.new(params, page: params[:page], order: 'id DESC').call
end
If you don't use Kaminari just delete page from initializer, the same with order param
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Filtering project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.