3

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 can I set after callback for first transition only in event1? (I cann't replace second transition to other 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 not working

1 Answers1

0

I am not sure whether I got you exactly.You want run event2 only 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 first transition,else it will do second one.After transition,if it's state2, it will do event2.You can find more detail at aasm.

In the event of having multiple transitions for an event, the first transition that successfully completes will stop other transitions in the same event from being processed.

You can write any code in the callback,just like rails callbacks.

rubyu2
  • 270
  • 1
  • 3
  • 14