简体   繁体   中英

Extending a module that extends a class

Is there a way to "flatten" nested modules so that all of their methods can be used when extending another class or module? For example:

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?

为什么不仅仅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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM