How to list all transitions of model?

80 wyświetleń
Przejdź do pierwszej nieodczytanej wiadomości

Alexey Zakharov

nieprzeczytany,
18 sty 2012, 04:05:2818.01.2012
do 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

nieprzeczytany,
18 sty 2012, 04:10:5018.01.2012
do pluginaw...@googlegroups.com
User.state_machine(:status).states

Aaron Pfeifer

nieprzeczytany,
11 lut 2012, 16:15:1011.02.2012
do 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
Odpowiedz wszystkim
Odpowiedz autorowi
Przekaż
Nowe wiadomości: 0