ruby-on-railsrubyloopsacts-as-state-machine

Acts as state machine loop


Is there any problem with adding a loop in the state machine for the rails plugin acts_as_state_machine?

I'm trying to do something like this and is not working:

state :not_sent
state :sent

event :test do
 transitions :from => :not_sent, :to => :sent
 transitions :from => :sent, :to => :sent
end

I want to do this because the state machine is for the state of an email. There more states that the ones that I showed here, but for practical reason I'm just showing the section concerning to the loop.

So now, I would like to add the possibility of resending a message. So I thought in adding a loop to the state machine, but it doesn't work. I try to add I new state "resend", just for debugging purposes and it worked. But I need some way to create a loop in the state machine.


Solution

  • You're not actually looping anything, though it may seem like that. If your beginning and end states are the same, you're not making a transition, and aasm will skip it, IIRC. The normal reason for wanting to do something like that is to repeat some logic that's part of the transition; in this case, i'd replace the transition with a method call, or add an intermediate state like :resending, which then immediately transitions back to the sent state after redoing the work you're after.