2

当在父模型中定义 AASM 时,有没有办法在子模型中添加回调?

class Parent
  include AASM
  aasm do
    state :initialised, :initial => true
    state :filled_out
    state :authorised

    event :fill_out do
      transitions :from => :initialised, :to => :filled_out
    end

    event :authorise, :before => :stamp_it do
      transitions :from => :filled_out, :to => :authorised
    end

    def stamp_it
      puts "Stamped"
    end
  end
end

在子类中,我们要添加回调mail_it。看来我们必须event从父级重新定义整个以添加另一个回调?

class Child < Parent
  aasm do
    event :authorise, :before => [:stamp_it, :mail_it] do
      transitions :from => :filled_out, :to => :authorised
    end
  end

  def mail_it
    puts "Mail it"
  end
end
4

1 回答 1

0

您可以在 Child 类中更新事件

class Child

  def initialize
    event = self.class.aasm.events.select{|e| e.name == :authorise}[0]
    event.options[:before] = [:stamp_it, :mail_it]
  end

end
于 2015-10-16T08:58:01.590 回答