1

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Rafael
  • 614
  • 2
  • 7
  • 13

1 Answers1

2

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.

Keith Gaddis
  • 4,113
  • 23
  • 20