AASM:保护回调的正确语法

时间:2011-10-27 11:48:04

标签: ruby-on-rails 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来测试NOT执行转换或after之后的行为。这是我在测试中调用的代码

foo = Foo.new
foo.move_to_two!
puts "foo's current status: #{foo.status}"

这是输出

executing after callback...
foo's current status: state_two

请注意,警卫永远不会被召唤......

我把警卫放在错误的地方吗?我错误地认为返回虚假会阻止过渡吗?暂停转换是否也会导致后回调被忽略?或者无论如何都会一直执行?

如果最后一件事情是真的,我如何将状态传递给该回调?

提前致谢,如果您需要更多信息,请告诉我......

JD

1 个答案:

答案 0 :(得分:1)

好吧,我想通了(整个“你一问,你找到了答案”的事情)......:警卫继续自己的过渡:

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