簡體   English   中英

在模塊前面加上 ActiveSupport::Concern ? 紅寶石 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

現在如果我將我的模塊轉換為關注模塊,它不是......

module BazConcern
  extend ActiveSupport::Concern

  included do    
    def foo
      super
      :baz
    end
  end
end

那么我們如何使用 prepend 和 ActiveSupport::Concern 呢? 紅寶石 2+

prepend ActiveSupport::Concern (Rails 6.1+)

Rails 6.1 添加了對ActiveSupport::Concern prepend支持。

請看下面的例子:

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

資料來源:

看起來有一個版本的ActiveSupport::Concern支持在此處可用: https : ActiveSupport::Concern

我還沒有嘗試過,但有一天我可能會嘗試。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM