2

I am using AASM. I have an event defined with a transition. It works if the event is raised and the model is in :from state. However it throws InValidTransition exception if the model is in any other state.

aasm_state :first
aasm_sate  :second
aasm_state :third

aasm_event :myevent do
  transitions :from => :second, :to => :third
end

Now, if I do mymodel.myevent! when mymodel is in :first or :third state, aasm throws InValidTransition. How can I tell aasm to ignore the event when in those states?

3 Answers3

3
aasm :column => :state, :whiny_transitions => false do
 state :first
 state  :second
 state :third

 event :myevent do
   transitions :from => :second, :to => :third
 end
end

This should do what you want.

0

The point of a state machine is that you want to limit which states you can transition to and from. Why are you using the constraints of a state machine if you want the above functionality? You could do it the same with

def myevent
  self.update_attribute(:state, 'third') if self.state == 'second'
end

Or you can do this if you want to continue using state machine

aasm_event :myevent do 
    transitions :to => :second, :from => [:second]
    transitions :to => :second, :from => [:first]
end
toonsend
  • 1,296
  • 13
  • 16
0

If you want to get only permitted events of your AASM object based on the current state you can do:

#assuming your aasm object is saved to 'dummy' variable dummy.aasm.events(permitted: true)

you can also get the name by

dummy.aasm.events(permitted: true).map(&:name)

dodo
  • 109
  • 4