gpt4 book ai didi

ruby-on-rails - 如何仅在 aasm 中为特定转换设置回调?

转载 作者:数据小太阳 更新时间:2023-10-29 08:11:49 25 4
gpt4 key购买 nike

我有两个事件:

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

如何仅在 event1 中设置第一次转换后的回调? (我无法将第二次转换替换为其他事件)

我试过了

event :event1 do
transitions to: :state2, from: :state1, after: :event2!, guard: proc {some func}
transitions to: :state3, from: :state1
end

但是没有效果

最佳答案

我不确定我是否准确地理解了你。你只想在这个转换之后运行 event2:transitions to: :state2, from: :state1, guard: proc {some func} ,对吧?

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

或者试试这个。

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

这段代码意味着,当 guard 返回 true 时,它​​将进行第一次转换,否则进行第二次转换。转换后,如果它是 state2,它将进行 event2。您可以在 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.

您可以在回调中编写任何代码,就像 rails 回调一样。

关于ruby-on-rails - 如何仅在 aasm 中为特定转换设置回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com