{"id":13879773,"url":"https://github.com/state-machines/state_machines-activerecord","last_synced_at":"2025-07-16T15:32:56.900Z","repository":{"id":25439570,"uuid":"28869403","full_name":"state-machines/state_machines-activerecord","owner":"state-machines","description":"StateMachines Active Record Integration","archived":false,"fork":false,"pushed_at":"2025-07-03T15:57:17.000Z","size":159,"stargazers_count":410,"open_issues_count":10,"forks_count":84,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-07-11T18:48:22.128Z","etag":null,"topics":["activerecord","ruby","state-machine"],"latest_commit_sha":null,"homepage":"https://github.com/state-machines/state_machines-activerecord","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/state-machines.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2015-01-06T15:29:46.000Z","updated_at":"2025-07-11T15:00:26.000Z","dependencies_parsed_at":"2023-01-14T02:44:31.021Z","dependency_job_id":"c44edb68-c4be-423d-9659-b75f2154db1f","html_url":"https://github.com/state-machines/state_machines-activerecord","commit_stats":{"total_commits":68,"total_committers":21,"mean_commits":3.238095238095238,"dds":0.7647058823529411,"last_synced_commit":"8b34ac57329f52fa259ace88d2f67bfeb5135f18"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/state-machines/state_machines-activerecord","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/state-machines%2Fstate_machines-activerecord","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/state-machines%2Fstate_machines-activerecord/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/state-machines%2Fstate_machines-activerecord/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/state-machines%2Fstate_machines-activerecord/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/state-machines","download_url":"https://codeload.github.com/state-machines/state_machines-activerecord/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/state-machines%2Fstate_machines-activerecord/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264904059,"owners_count":23681144,"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":["activerecord","ruby","state-machine"],"created_at":"2024-08-06T08:02:32.488Z","updated_at":"2025-07-16T15:32:56.892Z","avatar_url":"https://github.com/state-machines.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"[![Build Status](https://github.com/state-machines/state_machines-activerecord/actions/workflows/ruby.yml/badge.svg)](https://github.com/state-machines/state_machines-activerecord/actions/workflows/ruby.yml)\n\n# StateMachines Active Record Integration\n\nThe Active Record 7.2+ integration adds support for database transactions, automatically\nsaving the record, named scopes, validation errors.\n\n## Requirements\n\n- Ruby 3.2+\n- Rails 7.2+\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'state_machines-activerecord'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install state_machines-activerecord\n\n## Usage\n\nFor the complete usage guide, see http://www.rubydoc.info/github/state-machines/state_machines-activerecord/StateMachines/Integrations/ActiveRecord\n\n### Example\n\n```ruby\nclass Vehicle \u003c ApplicationRecord\n  state_machine :initial =\u003e :parked do\n    before_transition :parked =\u003e any - :parked, :do =\u003e :put_on_seatbelt\n    after_transition any =\u003e :parked do |vehicle, transition|\n      vehicle.seatbelt = 'off'\n    end\n    around_transition :benchmark\n\n    event :ignite do\n      transition :parked =\u003e :idling\n    end\n\n    state :first_gear, :second_gear do\n      validates :seatbelt_on, presence: true\n    end\n  end\n\n  def put_on_seatbelt\n    ...\n  end\n\n  def benchmark\n    ...\n    yield\n    ...\n  end\nend\n```\n\n### Scopes\nUsage of the generated scopes (assuming default column `state`):\n\n```ruby\nVehicle.with_state(:parked)                         # also plural #with_states\nVehicle.without_states(:first_gear, :second_gear)   # also singular #without_state\n```\n\n#### Transparent Scopes\nState scopes will return all records when `nil` is passed, making them perfect for search filters:\n\n```ruby\nVehicle.with_state(nil)                            # Returns all vehicles\nVehicle.with_state(params[:state])                 # Returns all vehicles if params[:state] is nil\nVehicle.where(color: 'red').with_state(nil)        # Returns all red vehicles (chainable)\n```\n\n### State driven validations\n\nAs mentioned in `StateMachines::Machine#state`, you can define behaviors,\nlike validations, that only execute for certain states. One *important*\ncaveat here is that, due to a constraint in ActiveRecord's validation\nframework, custom validators will not work as expected when defined to run\nin multiple states. For example:\n\n```ruby\nclass Vehicle \u003c ApplicationRecord\n  state_machine do\n    state :first_gear, :second_gear do\n      validate :speed_is_legal\n    end\n  end\nend\n```\n\nIn this case, the \u003ctt\u003e:speed_is_legal\u003c/tt\u003e validation will only get run\nfor the \u003ctt\u003e:second_gear\u003c/tt\u003e state.  To avoid this, you can define your\ncustom validation like so:\n\n```ruby\nclass Vehicle \u003c ApplicationRecord\n  state_machine do\n    state :first_gear, :second_gear do\n      validate {|vehicle| vehicle.speed_is_legal}\n    end\n  end\nend\n```\n\n## Contributing\n\n1. Fork it ( https://github.com/state-machines/state_machines-activerecord/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstate-machines%2Fstate_machines-activerecord","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstate-machines%2Fstate_machines-activerecord","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstate-machines%2Fstate_machines-activerecord/lists"}