Fork it up - acts_as_state_machine

Ryan Sonnek bio photo By Ryan Sonnek

Consider the following class definition:

class Door
 acts_as_state_machine :initial => :open
 state :closed
 state :open
 event :lock do
   transitions :from => :open, :to => closed

 event :unlock do
   transitions :from => :closed, :to => open

Now, what is wrong with this script?

door =
door.lock! #=> closes the door
door.lock! #=> doesn't do anything. door is already closed

The script should produce an error because the _lock_event does not have a valid transition when the door is already in the _closed_state. Instead, the current _acts_as_state_machine_plugin quietly returns _false_if there is no valid transition. That definitely isn’t what I expected and seriously took me several hours to track down this mysterious behavior.

I have published [a fork of the acts_as_state_machine_project]( which _correctly raises exceptions if there are no valid transitions available. The code change is very minor if you want to apply a patch to your own install.

There’s an entire rant just ripe for the picking, but I’ll save that for another time. For now, I’ve “scratched my own itch” and have made a library work much more intuitively for me. I’m happy… =)