Search code examples
ruby-on-railsruby-on-rails-4.1activesupport-concernamoeba-gem

Rails 4, Amoeba Gem inside a Concern


Is it possible to use the Amoeba gem within a Concern? Currently I'm getting this error when I try to run it inside a Concern:

undefined method `amoeba' for Amoeba:Module

The same code works fine inside the actual models but I want to DRY it up, since it is highly similar between 2 of my models... Here is what the Concern looks like:

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

Solution

  • amoeba is a class method. With ActiveSupport::Concern it should be called within included block

    module Amoeba
      extend ActiveSupport::Concern
      included do
        amoeba do 
        ....
        end
      end
    

    ActiveSupport::Concern