{"id":13878431,"url":"https://github.com/joeldrapper/action_state","last_synced_at":"2025-10-08T17:11:01.895Z","repository":{"id":37668403,"uuid":"467514819","full_name":"joeldrapper/action_state","owner":"joeldrapper","description":"Rails scopes and predicates defined with the same DSL.","archived":false,"fork":false,"pushed_at":"2022-06-28T13:01:50.000Z","size":31,"stargazers_count":60,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-09-03T09:54:55.979Z","etag":null,"topics":["activerecord","rails","ruby"],"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/joeldrapper.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-08T13:04:16.000Z","updated_at":"2025-04-30T01:57:10.000Z","dependencies_parsed_at":"2022-08-29T07:42:17.563Z","dependency_job_id":null,"html_url":"https://github.com/joeldrapper/action_state","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/joeldrapper/action_state","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joeldrapper%2Faction_state","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joeldrapper%2Faction_state/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joeldrapper%2Faction_state/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joeldrapper%2Faction_state/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joeldrapper","download_url":"https://codeload.github.com/joeldrapper/action_state/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joeldrapper%2Faction_state/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278665810,"owners_count":26024884,"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-10-06T02:00:05.630Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","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":["activerecord","rails","ruby"],"created_at":"2024-08-06T08:01:49.469Z","updated_at":"2025-10-08T17:11:01.877Z","avatar_url":"https://github.com/joeldrapper.png","language":"Ruby","readme":"# action_state\n\nActionState provides a simple DSL for defining Rails model states allowing you to query the state as an ActiveRecord scope on the class and a predicate on the instance.\n\nFor example, the following state definition defines a class scope `Article.published` and an instance predicate `article.published?`.\n\n```ruby\nclass Article \u003c ApplicationRecord\n  state(:published) { where(published_at: ..Time.current) }\n  ...\nend\n```\n\n## ✨ Contributing ✨\n\nIf you’re interested in contributing to this project, you can [book a pairing session with me](https://savvycal.com/joeldrapper/pair) and we can work through it together and I can hopefully share some context with you. Otherwise, please feel free top open a PR / issue / description. ❤️\n\n## Usage\n\nActionState supports a small subset of ActiveRecord queries for the predicate definition, and delegates the scope definition to ActiveRecord.\n\nIt's not meant to comprehensively support every possible ActiveRecord query; rather it supports a few features that tend to lend themselves well to predicate definitions.\n\n### `where`\n\nThe `where` method checks for inclusion in an Enumerable, coverage by a Range, and equality with other types of value.\n\n#### Inclusion in an Enumerable\n\n```ruby\nstate(:crafter) { where(role: [\"designer\", \"developer\"]) }\n```\n\n#### Covered by a Range\n\n```ruby\nstate(:negative) { where(stars: 1..4 }\nstate(:indifferent) { where(stars: 5..6) }\nstate(:positive) { where(stars: 7..9) }\n\nstate(:recently_published) { where(published_at: 1.week.ago..) }\n```\n\n#### Equality\n\n```ruby\nstate(:featured) { where(featured: true) }\n```\n\n### `where.not`\n\nThe counterpart to `where` is `where.not` which checks for exclusion from an Enumerable or Range, and inequality with other types of value.\n\n```ruby\nstate(:deleted) { where.not(deleted_at: nil) }\n```\n\n### `excluding`\n\nThe `excluding` method excludes specific instances of a model.\n\n```ruby\nstate(:normal) { excluding(special_post) }\n```\n\n### Passing arguments\n\nStates can also be defined to accept arguments.\n\n```ruby\nstate(:before) { |whenever| where(created_at: ..whenever) }\nstate(:after) { |whenever| where(created_at: whenever..) }\n```\n\n### Composing states\n\nYou can chain query methods together to form more complex queries.\n\n```ruby\nstate(:can_edit) { where(role: \"admin\").where.not(disabled: true) }\n```\n\nYou can also compose multiple states together.\n\n```ruby\nstate(:published) { where(published: true) }\nstate(:featured) { published.where(featured: true) }\n```\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem \"action_state\"\n```\n\nAnd then execute:\n\n```other\n$ bundle\n```\n\nOr install it yourself as:\n\n```other\n$ gem install action_state\n```\n\nFinally, include `ActionState` in your model class or `ApplicationRecord`:\n\n```ruby\nclass ApplicationRecord \u003c ActiveRecord::Base\n  include ActionState\n  ...\nend\n```\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoeldrapper%2Faction_state","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoeldrapper%2Faction_state","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoeldrapper%2Faction_state/lists"}