使用ActiveSupport :: Concern预置模块?红宝石2+

时间:2018-04-26 14:21:16

标签: ruby module ruby-on-rails-5 ruby-2.0 activesupport-concern

Module Baz
   def foo
     super
     :baz
   end
end


Class A
   prepend Baz

   def foo
     :bar
   end
end

A.new.foo //works fine

现在,如果我将模块转换为关注模块,那么它不是......

module BazConcern
  extend ActiveSupport::Concern

  included do    
    def foo
      super
      :baz
    end
  end
end

所以我们如何使用带有rubSu 2 +的ActiveSupport :: Concern?的前置

2 个答案:

答案 0 :(得分:2)

prependActiveSupport::Concern(Rails 6.1 +)

Rails 6.1通过prepend添加了对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

来源:

答案 1 :(得分:1)

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

我还没有尝试过,但是也许有一天。

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