{"id":18978067,"url":"https://github.com/simplificator/simplificator-filter","last_synced_at":"2025-07-22T14:34:36.470Z","repository":{"id":984596,"uuid":"788468","full_name":"simplificator/simplificator-filter","owner":"simplificator","description":"An attempt to generalize filtering of AR objects","archived":false,"fork":false,"pushed_at":"2012-03-15T11:23:08.000Z","size":149,"stargazers_count":1,"open_issues_count":5,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-02T15:51:19.588Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/simplificator.png","metadata":{"files":{"readme":"README.textile","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-07-21T09:54:54.000Z","updated_at":"2014-01-29T18:59:44.000Z","dependencies_parsed_at":"2022-07-18T15:00:33.048Z","dependency_job_id":null,"html_url":"https://github.com/simplificator/simplificator-filter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/simplificator/simplificator-filter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplificator%2Fsimplificator-filter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplificator%2Fsimplificator-filter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplificator%2Fsimplificator-filter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplificator%2Fsimplificator-filter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simplificator","download_url":"https://codeload.github.com/simplificator/simplificator-filter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplificator%2Fsimplificator-filter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266510687,"owners_count":23940696,"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","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-11-08T15:32:04.994Z","updated_at":"2025-07-22T14:34:36.442Z","avatar_url":"https://github.com/simplificator.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"h1. Simplificator Filter\n\nAn Extension to ActiveRecord in order to manage filtering and ordering of collections.\n\nh2. Install\n\n\u003cpre\u003e\n  gem install simplificator-filter\n\u003c/pre\u003e\n\nh2. Usage\n\nYou can define filters and orders in your models and then using this definitions to retrieve the data.\nSee the examples below.\n\nh2. Filter\n\nh3. Definition\n\nAssume you have a product model with name:string, price:integer and available_as_of:date as attributes. Then you could define the following filter definition in your product class.\n\u003cpre\u003e\n  filter_definition do |filter|\n    filter.fuzzy_name  :strategy =\u003e :like, :attribute =\u003e 'name'\n    filter.price_range :strategy =\u003e :between, :attribute =\u003e 'price'\n    filter.available_as_of :strategy =\u003e :equal\n  end\n\u003c/pre\u003e\n\nYou can use different strategies according to the attributes type. For string you can define :like, :begins_with, :ends_with or :equal.\nThe strategy option is not mandatory, if not set then the default strategy for that attributes type is used. For a string type this would be the :like strategy.\n(See filter strategies for more information)\n\nIf you just want a filter with the same name as the attribute and its default strategy, you can use:\n\n\u003cpre\u003e\n  default_filter_for_attribute :name\n  default_filters_for_attributes :name, :price\n\u003c/pre\u003e\n\nor just define for all your attributes a default filter:\n\n\u003cpre\u003e\n  default_filters_for_all_attributes\n\u003c/pre\u003e\n\nh3. filter_by\n\nKeeping the first definition as example, you can now use the filter_by class method:\n\n\u003cpre\u003e\n  Product.filter_by(:fuzzy_name =\u003e 'carpet')\n  Product.filter_by(:fuzzy_name =\u003e 'carpet', :price_range =\u003e '15 - 45')\n  Product.filter_by(:fuzzy_name =\u003e 'carpet', :price_range =\u003e '15 - 45', :available_as_of =\u003e 1.day.ago.to_date)\n\u003c/pre\u003e\n\nfilter_by returns a scope, therefore you can use it with your other scopes or with will_paginate.\n\n\u003cpre\u003e\n  Product.cheap.filter_by(:fuzzy_name =\u003e 'carpet').red\n  Product.cheap.filter_by(:fuzzy_name =\u003e 'carpet').red.paginate(:page =\u003e 2)\n\u003c/pre\u003e\n\nh3. Associations\n\nYou can even filter associated attributes. Let's add an order model with following associations and filter definition.\n\n\u003cpre\u003e\n  belongs_to :product\n  belongs_to :customer\n\n  filter_definition do |filter|\n    filter.customer_name :strategy =\u003e :like, :attribute =\u003e 'customer.name'\n    filter.product_name :strategy =\u003e :like, :attribute =\u003e 'product.name'\n    filter.purchased_at :strategy =\u003e :equal\n  end\n\u003c/pre\u003e\n\nThe filter_by works still the same way, all necessary tables are included automatically.\n\n\u003cpre\u003e\n  Product.filter_by(:customer_name =\u003e 'foo', :product_name =\u003e 'magic carpet')\n\u003c/pre\u003e\n\nh3. Filter Forms\n\n\u003cpre\u003e\n  \u003c% form_for @products.filter |f| %\u003e\n    \u003c%= f.input_field f.fuzzy_name %\u003e\n    \u003c%= f.input_field f.price_range %\u003e\n    \u003c%= f.input_field f.purchased_at %\u003e\n    \u003c%= f.sumbit_tag 'Filter' %\u003e\n  \u003c% end %\u003e\n\u003c/pre\u003e\n\nIn your controller you can just use:\n\u003cpre\u003e\n  @products = Product.filter_by params[:filter]\n\u003c/pre\u003e\n\nh3. View Helpers\n\nh3. Filter Strategies\n\ntable.\n{font-weight:bold}.|Type     | strategies    | default |\n|string   | :like, :begins_with, :ends_with, | :like   |\n|text     | :like, :begins_with, :ends_with, | :like   |\n|integer  | :equal, :between                 | :equal  |\n|float    | :equal, :between                 | :equal  |\n|time     | :equal, :between                 | :equal  |\n|date     | :equal, :between                 | :equal  |\n|datetime | :equal, :between                 | :equal  |\n|binary   | :equal,                          | :equal  |\n|boolean  | :equal,                          | :equal  |\n\nh3. Patterns\n\nIf you define more than one strategy for an attribute or leave it completly.\n\n\u003cpre\u003e\n  filter_definition do |filter|\n    filter.name :strategy =\u003e [:begins_with, :ends_with]\n    filter.price\n  end\n\u003c/pre\u003e\n\nIn this cases filter_by tries to match a pattern against the passed value to detect the right strategy.\nIf name matches \"* carpet\" a :ends_with strategy would be applied, if it would match \"magic* \" a :begins_with would be used.\nIf no pattern matches, then default strategy is used.\nThe only difference between the first and the second filter definition is: If a pattern could be found but it is not included in the array passed, an ArgumentError will be raised.\n\nPatterns\n\ntable.\n| pattern                    | strategy      |\n| /^\\*(.*?)\\*$/              | :like         |\n| /^\\*(.*?)$/,               | :ends_with    |\n| /^(.*?)\\*$/,               | :begins_with  |\n| /^(-?\\d+)\\s?-\\s?(-?\\d+)$/  | :between      |\n\nh2. Order\n\n\u003cpre\u003e\n\n\u003c/pre\u003e","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplificator%2Fsimplificator-filter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimplificator%2Fsimplificator-filter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplificator%2Fsimplificator-filter/lists"}