AASM在子类中添加回调

时间:2015-09-10 20:13:22

标签: ruby aasm

在父模型中定义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

1 个答案:

答案 0 :(得分:0)

您可以在子课程中更新活动

class Child

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

end