Rails 4,Amoeba Gem在一个关注点内

时间:2014-07-13 11:24:45

标签: ruby-on-rails ruby-on-rails-4.1 activesupport-concern amoeba-gem

是否可以在关注范围内使用Amoeba宝石?目前,当我尝试在一个关注点内运行它时,我收到此错误:

undefined method `amoeba' for Amoeba:Module

相同的代码在实际模型中运行良好,但我想干它,因为它在我的两个模型之间非常相似......这就是关注的样子:

module Amoeba
  extend ActiveSupport::Concern

  amoeba do
    enable
    customize(lambda { |original, clone|
      clone.uid        = SecureRandom.hex(2)
      clone.activities = []

    if original.class.name == "Widget"
      clone.bookings   = []
    end

    if orignal.class.name == "Flotsam"
      clone.remaining  = 100
      if original.expiration.past?
        clone.expiration = Date.today + 5.years
      end
    end

    if original.icon.present?
      clone.icon = original.icon
    end
    if original.pdf.present?
      clone.pdf = original.pdf
    end
  })
end

1 个答案:

答案 0 :(得分:1)

变形虫是一种类方法。使用ActiveSupport :: Concern时,应在include block

中调用它
module Amoeba
  extend ActiveSupport::Concern
  included do
    amoeba do 
    ....
    end
  end

ActiveSupport::Concern