Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/state-machines/state_machines-activemodel
StateMachines ActiveModel Integration
https://github.com/state-machines/state_machines-activemodel
activemodel ruby state-machine
Last synced: 4 months ago
JSON representation
StateMachines ActiveModel Integration
- Host: GitHub
- URL: https://github.com/state-machines/state_machines-activemodel
- Owner: state-machines
- License: mit
- Created: 2014-04-29T16:41:17.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-01-22T16:06:37.000Z (10 months ago)
- Last Synced: 2024-04-27T04:22:59.584Z (7 months ago)
- Topics: activemodel, ruby, state-machine
- Language: Ruby
- Homepage:
- Size: 102 KB
- Stars: 33
- Watchers: 5
- Forks: 35
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
![Build Status](https://github.com/state-machines/state_machines-activemodel/actions/workflows/ruby.yml/badge.svg)
[![Code Climate](https://codeclimate.com/github/state-machines/state_machines-activemodel.svg)](https://codeclimate.com/github/state-machines/state_machines-activemodel)# StateMachines ActiveModel Integration
The ActiveModel integration is useful for both standalone usage and for providing
the base implementation for ORMs which implement the ActiveModel API. This
integration adds support for validation errors and dirty attribute tracking.## Installation
Add this line to your application's Gemfile:
gem 'state_machines-activemodel'
And then execute:
$ bundle
Or install it yourself as:
$ gem install state_machines-activemodel
## Dependencies
Active Model 5.1+
## Usage
```ruby
class Vehicle
include ActiveModel::Dirty
include ActiveModel::Validationsattr_accessor :state
define_attribute_methods [:state]state_machine :initial => :parked do
before_transition :parked => any - :parked, :do => :put_on_seatbelt
after_transition any => :parked do |vehicle, transition|
vehicle.seatbelt = 'off'
end
around_transition :benchmarkevent :ignite do
transition :parked => :idling
endstate :first_gear, :second_gear do
validates_presence_of :seatbelt_on
end
enddef put_on_seatbelt
...
enddef benchmark
...
yield
...
end
endclass VehicleObserver < ActiveModel::Observer
# Callback for :ignite event *before* the transition is performed
def before_ignite(vehicle, transition)
# log message
end# Generic transition callback *after* the transition is performed
def after_transition(vehicle, transition)
Audit.log(vehicle, transition)
end# Generic callback after the transition fails to perform
def after_failure_to_transition(vehicle, transition)
Audit.error(vehicle, transition)
end
end```
## Contributing
1. Fork it ( https://github.com/state-machines/state_machines-activemodel/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request