Question

I would like to use transition_for of state_machine gem but the docs does not show which object I am supposed to use:

http://rdoc.info/github/pluginaweek/state_machine/master/StateMachine/Event:transition_for

Do you have an example to show me?

Était-ce utile?

La solution

class Stately
  state_machine :state, initial: :pending do
    state :approved
    state :declined

    event :approve do
      transition pending: :approved
    end

    event :decline do
      transition all => :declined
    end
  end
end

stately = Stately.new
stately.state
#=> :pending

stately.state_events
#=> [ :approve, :decline ]

stately.approve
stately.state_events
#=> [ :decline ]

If what you want to do is to prevent yourself from accidentally triggering an event that will throw an exception (much narrower scope than trying to see ALL events) then you can also do this...

stately = Stately.new
stately.state
#=> :pending

stately.can_approve?
#=> true
stately.can_decline?
#=> true

stately.approve
stately.can_approve?
#=> false
stately.can_decline?
#=> true
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top