{"id":23705848,"url":"https://github.com/fidme/active_filters","last_synced_at":"2025-09-03T11:31:02.121Z","repository":{"id":34082522,"uuid":"169384470","full_name":"FidMe/active_filters","owner":"FidMe","description":"Map incoming controller parameters to named scopes in your models","archived":false,"fork":false,"pushed_at":"2022-12-14T04:54:20.000Z","size":22,"stargazers_count":7,"open_issues_count":6,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-20T23:37:29.644Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FidMe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-06T09:55:21.000Z","updated_at":"2019-09-25T07:12:23.000Z","dependencies_parsed_at":"2023-01-15T04:31:48.820Z","dependency_job_id":null,"html_url":"https://github.com/FidMe/active_filters","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FidMe%2Factive_filters","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FidMe%2Factive_filters/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FidMe%2Factive_filters/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FidMe%2Factive_filters/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FidMe","download_url":"https://codeload.github.com/FidMe/active_filters/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231874112,"owners_count":18439220,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-30T14:58:31.077Z","updated_at":"2024-12-30T14:58:32.019Z","avatar_url":"https://github.com/FidMe.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ActiveFilters\n\nActive Filters allows you to map incoming controller parameters to filter your resources by chaining scopes.\n\nIt is inspired by [Justin Weiss solution](https://www.justinweiss.com/articles/search-and-filter-rails-models-without-bloating-your-controller/) offering a cleaner way to declare your filters.\n\nThe gem [has_scope](https://github.com/plataformatec/has_scope) exists but does not handle params stored as the value of a hash.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'active_filters'\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install active_filters\n\n## Usage\n\nLet's say you have several scopes on your User model\n\n```ruby\nclass User \u003c ApplicationRecord\n    scope :country, -\u003e country_code { where(country: country_code) }\n    scope :gender, -\u003e gender { where(gender: gender) }\nend\n```\n\nAnd you want to chain thoses two scopes.\nYou first need to include the Filterable controller module in your application controller\n\n```ruby\nclass ApplicationController \u003c ActionController::Base\n    include ActiveFilters::Controller::Filterable\nend\n```\n\nand then include the model Filterable one in the model you want to filter.\n\n```ruby\nclass User \u003c ApplicationRecord\n    include ActiveFilters::Model::Filterable\n\n    scope :country, -\u003e country_code { where(country: country_code) }\n    scope :gender, -\u003e gender { where(gender: gender) }\nend\n```\n\nThen you just need to declare your named scopes as filters using the `has_filters` DSL\n\n```ruby\nclass UsersController \u003c ApplicationController\n    has_filters :country, :gender\nend\n```\n\nand apply them to a specific resource using the `filter` class method and the `filterable params` hash of params as argument.\n\n```ruby\nclass UsersController \u003c ApplicationController\n    has_filters :country, :gender\n\n    def index\n        @users = User.with_filter(filterable_params)\n    end\nend\n```\n\nFor each request:\n\n```\n/users\n#=\u003e acts like a normal request\n\n/users?country=FR\u0026gender=female\n#=\u003e calls the named scope and bring only females in France\n```\n\nNow let's say you want to use incoming params stored as the value of a hash as scopes:\n\n```\n/users?filter[country]=FR\u0026filter[gender]=female\n#=\u003e { filter: { country: 'FR', gender: 'female' } }\n```\n\nThen you can add the `in_key` attribute with the name of the key in which params are stored.\n\n```ruby\nclass UsersController \u003c ApplicationController\n    has_filters :country, :gender, in_key: :filter\n\n    def index\n        @users = User.filter(filterable_params)\n    end\nend\n```\n\n## Initializer\n\nBy default the params variable used by Active Filters is `params`.\nIf 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)\n\n```ruby\nActiveFilters::Setup.params_variable = '@params'\n```\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/FidMe/active_filters.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffidme%2Factive_filters","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffidme%2Factive_filters","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffidme%2Factive_filters/lists"}