AASM:保护回调的正确语法

发布于 2024-12-12 08:05:05 字数 979 浏览 0 评论 0原文

这是我的示例代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我偏爱纯白色 2024-12-19 08:05:05

好吧,我想通了(整个“只要你问,你就会找到答案”)...... :guard 本身就这样进行转换:

aasm_event :move_to_two, :after => :after_callback do
  transitions :from => :start_state, :to => :state_two, :guard => :guard_callback
end

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:

aasm_event :move_to_two, :after => :after_callback do
  transitions :from => :start_state, :to => :state_two, :guard => :guard_callback
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文