How to list all transitions of model?

80 views
Skip to first unread message

Alexey Zakharov

unread,
Jan 18, 2012, 4:05:28 AM1/18/12
to pluginaw...@googlegroups.com
In your sample you've showed how to list transition of model instance @user.state_transitions.

But how can list all transition for model class. I want to use this list for filtering. I want something like this: User. state_transitions.

Alexey Zakharov

unread,
Jan 18, 2012, 4:10:50 AM1/18/12
to pluginaw...@googlegroups.com
User.state_machine(:status).states

Aaron Pfeifer

unread,
Feb 11, 2012, 4:15:10 PM2/11/12
to pluginaw...@googlegroups.com
Hey Alexey -

Sorry that it's taken so long for someone to get back to you.  Unfortunately, there's currently no way to get the list of possible transitions from the model class.  It is only possible to get this list within the context of an actual instance of the model.  The reason for this is that transitions often require object context in order to know that the can succeed.  Consider the following example:

  class Vehicle
    state_machine :initial => :parked do
      event :ignite do
        transition :parked => :idling, :if => :seatbelt_on?
        transition :parked => same
      end
    end
    
    ...
  end

In this example, it would be impossible to know what transition to choose from the ignite even without having an object to check the seatbelt_on? conditional for the transition.

Now, you could simply get around this by adding your own class method like so:

  class Vehicle
    def self.state_transitions(*args)
      new.state_transitions(*args)
    end
  end

The reason something as simple as this isn't built into state_machine is because it would be impossible to create instances for everyone's use cases.  Instead, it's up to you to provide this implementation.

Hope this helps!

Best,
Aaron
Reply all
Reply to author
Forward
0 new messages