Domanda

I'm new to AASM, and trying to get a list of all states in a MyModel model that can transition to state :newstate.

So, for example:

  aasm_event :finish do
    transitions :to => :finalstate, :from => [:start, :working]
  end

Basically, I want to return the [:start, :working] array via the model, so something along the lines of

MyModel.aasm_events.finish.transitions.from

But, well, that's not the syntax...and I can't find anything for it in the docs.

Any suggestions appreciated.

È stato utile?

Soluzione

It wasn't supported until now. Freshly released gem version 3.0.12 now supports a class method called aasm_from_states_for_state which does exactly what you asked for. Unfortunately, your suggested little DSL (...finish.transitions.from) is very nice, but not available for now. But I will keep it in mind ;)

Use the new class method like this:

MyModel.aasm_from_states_for_state(:finalstate)

which returns all possible from states. If you want only the from states for a specific transition:

MyModel.aasm_from_states_for_state(:finalstate, :transition => :finish)

or more modern (using Ruby 1.9)

MyModel.aasm_from_states_for_state(:finalstate, transition: :finish)

All calls return an array of possible from states.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top