在模块前面加上 ActiveSupport::Concern ? ruby 2+

prepend module with ActiveSupport::Concern ? ruby 2+

Module Baz
   def foo
     super
     :baz
   end
end


Class A
   prepend Baz

   def foo
     :bar
   end
end

A.new.foo //works fine

现在,如果我将我的模块转换为 Concern 模块,则不会...

module BazConcern
  extend ActiveSupport::Concern

  included do    
    def foo
      super
      :baz
    end
  end
end

那么 我们如何使用 ActiveSupport::Concern 前缀? 和 ruby 2+

这里似乎有一个支持前置的 ActiveSupport::Concern 版本:https://gist.github.com/bcardarella/5735987

我还没有尝试过,但总有一天我会的。

(链接到来自 https://groups.google.com/forum/#!topic/rubyonrails-core/sSk9IEW74Ro

prependActiveSupport::Concern (Rails 6.1+)

Rails 6.1 添加了对 prepend with ActiveSupport::Concern.

的支持

请看下面的例子:

module Imposter
  extend ActiveSupport::Concern

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

class Person
  prepend Imposter
end

另外值得一提的是concerning也更新了:

class Person
  concerning :Imposter, prepend: true do

  end
end

来源: