How to get the `self` to refer to a my class inside a Mixin module (even if it is stated outside the context of a method)?

Deadly 提交于 2019-12-04 21:19:19

Just use Concern's included method instead:

module MyModule
  extend ActiveSupport::Concern

  included do
    include MyModule::AnotherModule if self.my_article_method?
  end
end

You can use the self.included hook:

def self.included(klass)
  klass.include MyModule::AnotherMyModule if klass.my_article_method?
end

I'd rather put the logic on the actual Article class. The module shouldn't need to know about the classes it is included in:

class Article
  include MyModule
  include MyModule::AnotherModule if self.my_article_method?
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!