StateMachine Real World Intended Usage / Best Practices

Skip to first unread message

Karl Falconer

Oct 31, 2012, 4:55:17 PM10/31/12

I am working on a prototype using state_machine, but I am a bit confused about how it is intended to be used in a real application. I have a state machine (see below) that, has a few states, that move in a linear progression with the exception that any action can cause an error.

1. In my application, each event corresponds to logic that should be performed via a web service call. What I am confused about is how should I be tying my application logic into these events? From the examples, there are many different ways to do this. before_transition callbacks, Observer, override the event method, and call super. What are the best practices? 

2. An additional concern is how will transitions work for methods that are executed asynchronously (via delayed_job). Since 

3. Lastly, are there any examples/patterns that would be helpful in creating a "Driver Class" that would execute its next event based on its transition sequence. In my application there is no user interaction, so it need to know and fire the next event until it reaches the end state.

Thanks for the help.

state_machine :status, :initial => :new, :action => :save do

  event :process do
    transition :from => :new, :to => [:processing, :errored]
  event :close do
    transition :from => :processing, :to => [:closed, :errored]

  state :errored

  state :new

  state :processing
  state :closed
Reply all
Reply to author
0 new messages