state_machine

https://github.com/pluginaweek/state_machine

Ruby

Adds support for creating state machines for attributes on any Ruby class

StateMachine::EventCollection#attribute_transition_for

Gets the transition that should be performed for the event stored in the
given object's event attribute.  This also takes an additional parameter
for automatically invalidating the object if the event or transition are
invalid.  By default, this is turned off.

*Note* that if a transition has already been generated for the event, then
that transition will be used.

== Examples

  class Vehicle < ActiveRecord::Base
    state_machine :initial => :parked do
      event :ignite do
        transition :parked => :idling
      end
    end
  end
  
  vehicle = Vehicle.new                       # => #<Vehicle id: nil, state: "parked">
  events = Vehicle.state_machine.events
  
  vehicle.state_event = nil
  events.attribute_transition_for(vehicle)    # => nil # Event isn't defined
  
  vehicle.state_event = 'invalid'
  events.attribute_transition_for(vehicle)    # => false # Event is invalid
  
  vehicle.state_event = 'ignite'
  events.attribute_transition_for(vehicle)    # => #<StateMachine::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>

Source | Google | Stack overflow

Edit

git clone [email protected]:pluginaweek/state_machine.git

cd state_machine

open lib/state_machine/event_collection.rb

Contribute

# Make a new branch

git checkout -b -your-name--update-docs-StateMachine--EventCollection-attribute_transition_for-for-pr


# Commit to git

git add lib/state_machine/event_collection.rbgit commit -m "better docs for StateMachine::EventCollection#attribute_transition_for"


# Open pull request

gem install hub # on a mac you can `brew install hub`

hub fork

git push <your name> -your-name--update-docs-StateMachine--EventCollection-attribute_transition_for-for-pr

hub pull-request


# Celebrate!