Project

state_gate

0.0
The project is in a healthy, maintained state
State management for ActiveRecord, with states; transitions; and just the right amount of syntactic sugar.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 5.0.0.beta1
 Project Readme
Main Branch Dev Branch
ruby 3.3 ruby 3.3
ruby 3.2 ruby 3.2
ruby 3.1 ruby 3.1
ruby 3.0 ruby 3.0
ruby 2.7 ruby 2.7
ruby 2.6 ruby 2.6
ruby 2.5 ruby 2.5
 
License: MIT License: MIT
Gem Version Gem Version
100% Coverage 100% Coverage

Index


State Management for ActiveRecord!

The state, the whole state and nothing but the state!

Designed for use with ActiveRecord, StateGate has a single responsibility:

  • to only allow a valid state to be set.
  • to only allow a valid transition from one state to another.

With a simple DSL and a sprinkling of syntactic sugar, StateGate is intuitive and easy to use.

No guard clauses?

Nope! ActiveRecord validations are the best way to keep everything in order.

No events?

Nope! Changing state is often the smallest part of an event. Events often access other attributes or associations, and are the responsibility of the model, not the StateGate.

Is it opinionated?

Very! Any attempt to set an invalid state or transition will raise an exception. Exceptions are raised before accessing the database or triggering any callbacks.


Requirements

  • Ruby 2.5+
  • ActiveRecord 5.0+

Installation

Add this line to your Gemfile:

gem 'state_gate'


Summary

A quick list of StateGate's creation, configuration, class & instance methods. Each method links to a more in-depth explanation within the Wiki.

...creation

Creating a StateGate on the :status attribute with states of :draft, :pending, :published & :archived.

class Post < ActiveRecord::Base
  include StateGate

  state_gate :status do
    state :draft,     transitions_to: :pending
    state :pending,   transitions_to: [:published, :archived], human: 'Pending Approval'
    state :published, transitions_to: :archived
    state :archived
  end
end

...configuration options

...class methods

...scope methods

...instance methods


Testing with RSpec


Contributing

  • Security issues
  • Reporting issues
  • Pull requests

In all cases please respect our Contributor Code of Conduct.

Security issues

If you have found a security related issue, please follow our Security Policy.

Reporting issues

Please try to answer the following questions in your bug report:

  • What did you do?
  • What did you expect to happen?
  • What happened instead?

Make sure to include as much relevant information as possible, including:

  • Ruby version.
  • StateGate version.
  • ActiveRecord version.
  • OS version.
  • The steps needed to replicate the issue.
  • Any stack traces you have are very valuable.

Pull Requests

We encourage contributions via GitHub pull requests.

Our Developer Guide details how to fork the project; get it running locally; run the tests; check the documentation; check your style; and submit a pull request.


Code of Conduct

We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.

Read the full details in our Contributor Code of Conduct.


License

The MIT License (MIT)

Copyright (c) 2020 CodeMeister

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.