AASM:保护回调的正确语法
这是我的示例代码:
class Foo < ActiveRecord::Base
include AASM
aasm_column :status
aasm_initial_state :start_state
aasm_state :start_state
aasm_state :state_two
aasm_state :end_state
aasm_event :move_to_two, :guard => :guard_callback, :after => :after_callback do
transitions :from => :start_state, :to => :state_two
end
def guard_callback
puts "executing guard callback..."
false
end
def after_callback
puts "executing after callback..."
end
这是我的代码的玩具表示。我只是从守卫回调返回 false 来测试不执行转换或之后的行为。这是我在测试中调用的代码
foo = Foo.new
foo.move_to_two!
puts "foo's current status: #{foo.status}"
这是输出
executing after callback...
foo's current status: state_two
请注意,守卫永远不会被调用...
我是否将守卫放在错误的位置?我是否错误地认为返回 false 会停止转换?停止转换是否也会导致 after 回调被忽略?或者无论如何它都会执行 after ?
如果最后一件事是真的,我如何将状态传递到该回调中?
提前致谢,如果您需要更多信息,请告诉我...
jd
Here is my example code:
class Foo < ActiveRecord::Base
include AASM
aasm_column :status
aasm_initial_state :start_state
aasm_state :start_state
aasm_state :state_two
aasm_state :end_state
aasm_event :move_to_two, :guard => :guard_callback, :after => :after_callback do
transitions :from => :start_state, :to => :state_two
end
def guard_callback
puts "executing guard callback..."
false
end
def after_callback
puts "executing after callback..."
end
This is a toy representation of what my code looks like. I'm only returning false from the guard callback to test the behavior of NOT executing the transition or the after. Here is the code I call in my test
foo = Foo.new
foo.move_to_two!
puts "foo's current status: #{foo.status}"
Here's the output
executing after callback...
foo's current status: state_two
Notice that the guard never gets called...
Am I putting the guard in the wrong place? Am I mistaken that returning false will halt the transition? Does halting the transition also cause the after callback to be ignored? Or will it always execute the after no matter what?
If this last thing is true, how do I pass state into that callback?
thanks in advance, and let me know if you need more information...
jd
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想通了(整个“只要你问,你就会找到答案”)...... :guard 本身就这样进行转换:
OK, I figured it out (the whole "as soon as you ask, you find the answer" thing)...the :guard goes on the transitions themselves as such: