{"id":13878542,"url":"https://github.com/bsm/filterable-by","last_synced_at":"2025-04-07T13:31:18.867Z","repository":{"id":1878690,"uuid":"45182963","full_name":"bsm/filterable-by","owner":"bsm","description":null,"archived":false,"fork":false,"pushed_at":"2024-08-02T01:42:06.000Z","size":48,"stargazers_count":2,"open_issues_count":2,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-22T19:02:17.271Z","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/bsm.png","metadata":{"files":{"readme":"README.md","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,"governance":null}},"created_at":"2015-10-29T12:55:02.000Z","updated_at":"2022-06-02T14:30:13.000Z","dependencies_parsed_at":"2023-07-05T20:46:10.364Z","dependency_job_id":null,"html_url":"https://github.com/bsm/filterable-by","commit_stats":{"total_commits":28,"total_committers":3,"mean_commits":9.333333333333334,"dds":0.1071428571428571,"last_synced_commit":"471243fa4c2b15d83b8d0ad6e99364dfeba96452"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Ffilterable-by","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Ffilterable-by/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Ffilterable-by/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bsm%2Ffilterable-by/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bsm","download_url":"https://codeload.github.com/bsm/filterable-by/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247661653,"owners_count":20975095,"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-08-06T08:01:52.629Z","updated_at":"2025-04-07T13:31:18.530Z","avatar_url":"https://github.com/bsm.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Filterable By\n\n[![Test](https://github.com/bsm/filterable-by/actions/workflows/test.yml/badge.svg)](https://github.com/bsm/filterable-by/actions/workflows/test.yml)\n\nActiveRecord plugin to parse e.g. a `filter` query parameter apply scopes. Useful for [JSON-API][jsonapi] compatibility.\n\n[jsonapi]: http://jsonapi.org/format/#fetching-filtering\n\n## Installation\n\nAdd `gem 'filterable-by'` to your Gemfile.\n\n## Usage\n\n```ruby\nclass Comment \u003c ActiveRecord::Base\n  belongs_to :post\n\n  filterable_by :post_id, :user_id\n  filterable_by :post_author_id do |value|\n    joins(:posts).where(:'posts.author_id' =\u003e value)\n  end\n  filterable_by :only do |value, **opts|\n    case value\n    when 'mine'\n      where(user_id: opts[:user_id]) if opts[:user_id]\n    else\n      all\n    end\n  end\nend\n\nComment.filter_by(params[:filter], user_id: current_user.id)  # =\u003e ActiveRecord::Relation\n```\n\nSimple use cases:\n\n```ruby\nComment.filter_by({ 'post_id' =\u003e '1' })\n# =\u003e WHERE post_id = 1\n\nComment.filter_by({ 'post_id' =\u003e ['1', '2'] })\n# =\u003e WHERE post_id IN (1, 2)\n\nComment.filter_by({ 'post_id_not' =\u003e '3' })\n# =\u003e WHERE post_id != 3\n\nComment.filter_by({ 'user_id' =\u003e '2', 'ignored' =\u003e '3' })\n# =\u003e WHERE user_id = 2\n\nComment.filter_by({ 'only' =\u003e 'mine' }, user_id: 4)\n# =\u003e WHERE user_id = 4\n\nComment.filter_by({ 'post_author_id' =\u003e '5' })\n# =\u003e JOINS posts ON posts.id = comments.post_id WHERE posts.author_id = 5\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbsm%2Ffilterable-by","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbsm%2Ffilterable-by","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbsm%2Ffilterable-by/lists"}