Main Branch | Dev Branch |
---|---|
Index
- state management for ActiveRecord
- requirements
- installation
- change log
- summary
- wiki - how to do anything
- RSpec test helpers
- contributing
- code of conduct
- license
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
- #statuses
- #human_statuses
- #status_transitions
- #status_transitions_for(:draft | :pending | :published | :archived)
- #statuses_for_select, #statuses_for_select(:sorted)
...scope methods
...instance methods
- .statuses
- .human_statuses
- .human_status
- .force_draft, .force_published, :force_archived
- .draft?, .pending?, .published?, .archived?
- .not_draft?, .not_pending?, .not_published?, .not_archived?
- .status_transitions
- .status_transitions_to?(:draft | :pending | :published | :archived)
- .statuses_for_select, .statuses_for_select(:sorted)
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.