prepend module with ActiveSupport::Concern ? ruby 2+

2k Views Asked by At
Module Baz
   def foo
     super
     :baz
   end
end


Class A
   prepend Baz

   def foo
     :bar
   end
end

A.new.foo //works fine

now if I transform my module to Concern module, it's not...

module BazConcern
  extend ActiveSupport::Concern

  included do    
    def foo
      super
      :baz
    end
  end
end

So how can we use prepend with ActiveSupport::Concern ? with ruby 2+

2

There are 2 best solutions below

0
Marian13 On BEST ANSWER

prepend with ActiveSupport::Concern (Rails 6.1+)

Rails 6.1 added support of prepend with ActiveSupport::Concern.

Please, see the following example:

module Imposter
  extend ActiveSupport::Concern

  # Same as `included`, except only run when prepended.
  prepended do
    
  end
end

class Person
  prepend Imposter
end

It is also worth to mention that concerning is also updated:

class Person
  concerning :Imposter, prepend: true do

  end
end

Sources:

0
Tyler Rick On

It looks like there is a version of ActiveSupport::Concern that supports prepending available here: https://gist.github.com/bcardarella/5735987.

I haven't tried it out yet, but I might some day.

(Linked to from https://groups.google.com/forum/#!topic/rubyonrails-core/sSk9IEW74Ro)