How to set after callback for a specific transition only in aasm?
I have 2 events:
event :event1, after: :event2! do
transitions to: :state2, from: :state1, guard: proc {some func}
transitions to: :state3, from: :state1
end
event :event2 do
transitions to: :state3, from: state2, guard: proc {some func}
end
How to set after callback for first transition only in event1? (I cannot replace the second transition with another event)
I tried
event :event1 do
transitions to: :state2, from: :state1, after: :event2!, guard: proc {some func}
transitions to: :state3, from: :state1
end
But it doesn't work
source to share
I'm not sure if I got you exactly. You event2
only want to run after this transition:, transitions to: :state2, from: :state1, guard: proc {some func}
right?
event :event1 do
transitions to: :state2, from: :state1, :after => Proc.new {|*args| set_process(*args) } , guard: proc {some func}
transitions to: :state3, from: :state1
end
def set_process(name)
event2!
end
Or try this.
event :event1 do
transitions to: :state2, from: :state1, guard: proc {some func}
transitions to: :state3, from: :state1
after do
event2! if self.state2?
end
end
event :event2 do
transitions to: :state3, from: state2, guard: proc {some func}
end
This code means that when guard returns true it will do the first jump, otherwise it will do the second. After transition, if specified by state2 it will do event2. For more details you can find in aasm .
In case there are multiple transitions for an event, the first transition that completes successfully will stop other transitions to the same event from being processed.
You can write any code in the callback, just like rails callbacks.
source to share