0.0
No release in over 3 years
Low commit activity in last 3 years
Map incoming controller parameters to named scopes in your models
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.17.2
~> 5.0
>= 1.8.0
~> 10.0
 Project Readme

ActiveFilters

Active Filters allows you to map incoming controller parameters to filter your resources by chaining scopes.

It is inspired by Justin Weiss solution offering a cleaner way to declare your filters.

The gem has_scope exists but does not handle params stored as the value of a hash.

Installation

Add this line to your application's Gemfile:

gem 'active_filters'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install active_filters

Usage

Let's say you have several scopes on your User model

class User < ApplicationRecord
    scope :country, -> country_code { where(country: country_code) }
    scope :gender, -> gender { where(gender: gender) }
end

And you want to chain thoses two scopes. You first need to include the Filterable controller module in your application controller

class ApplicationController < ActionController::Base
    include ActiveFilters::Controller::Filterable
end

and then include the model Filterable one in the model you want to filter.

class User < ApplicationRecord
    include ActiveFilters::Model::Filterable

    scope :country, -> country_code { where(country: country_code) }
    scope :gender, -> gender { where(gender: gender) }
end

Then you just need to declare your named scopes as filters using the has_filters DSL

class UsersController < ApplicationController
    has_filters :country, :gender
end

and apply them to a specific resource using the filter class method and the filterable params hash of params as argument.

class UsersController < ApplicationController
    has_filters :country, :gender

    def index
        @users = User.with_filter(filterable_params)
    end
end

For each request:

/users
#=> acts like a normal request

/users?country=FR&gender=female
#=> calls the named scope and bring only females in France

Now let's say you want to use incoming params stored as the value of a hash as scopes:

/users?filter[country]=FR&filter[gender]=female
#=> { filter: { country: 'FR', gender: 'female' } }

Then you can add the in_key attribute with the name of the key in which params are stored.

class UsersController < ApplicationController
    has_filters :country, :gender, in_key: :filter

    def index
        @users = User.filter(filterable_params)
    end
end

Initializer

By default the params variable used by Active Filters is params. If you want to use another params variable, you have to create an initializer in config/initializers/active_filters.rb and set the variable name: (for instance with @params)

ActiveFilters::Setup.params_variable = '@params'

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/FidMe/active_filters.

License

The gem is available as open source under the terms of the MIT License.