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?的前置
答案 0 :(得分:2)
prepend
与ActiveSupport::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链接到)