开发者

Extending a module that extends a class

开发者 https://www.devze.com 2023-02-15 05:09 出处:网络
Is there a way to \"flatten\" nested modules so that all of their methods can be used when extending another class or module?For ex开发者_运维问答ample:

Is there a way to "flatten" nested modules so that all of their methods can be used when extending another class or module? For ex开发者_运维问答ample:

class User
  extend UserStats
end

module UserStats

  module Current
    def active
      where('status = "active"')
    end
  end

end

I want to be able to extend UserStats (or User) such that the methods in UserStats::Current are available as class methods for User.

I tried "extend Current" in UserStats, but that doesn't seem to work. Is there any way to do this?


Why not just extend UserStats::Current ?


Do you mean something like this?

module UserStats

  def self.extended(klass)
    klass.send(:extend, Current)
  end

  module Current
    def active
      puts "test"
    end
  end

end

class User
  extend ::UserStats
end

puts User.active
0

精彩评论

暂无评论...
验证码 换一张
取 消