ruby - 在模块前添加 ActiveSupport::Concern ? ruby 2+

标签 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

那么我们如何在 ActiveSupport::Concern 中使用 prepend ? 与 ruby​​ 2+

最佳答案

前面添加 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

来源:

关于ruby - 在模块前添加 ActiveSupport::Concern ? ruby 2+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50045415/

相关文章:

ruby-on-rails - 'Travel' 时间助手是否在功能规范中不可用?

ruby-on-rails - Rails 3 - 通过嵌套属性保存属于少数模型的模型

ruby-on-rails - 在 TDD 中 stub 依赖有哪些好的方法?

ruby - 如何匹配 URL 但从匹配中排除终止符?

python - 如何安全使用exec?我想使用动态加载的代码模块作为配置文件

ruby-on-rails - 是否运行 rake Assets :precompile always return the same filenames?

ruby - 无法将哈希表中的符号转换为整数

graph - OCaml 有向图顶点模块

python - 如何将多个 Python 源文件连接成一个文件?

ruby - Rails 5 API Omniauth 使用 ActiveRecord session 存储