开发者

Make ClassMethods also available as module function with ActiveSupport::Concern

Given the following code:

module Foo
  extend ActiveSupport::Concern

  module ClassMethods
    def foo
      puts 'foo'
    end
  end
end

class Bar
  include Foo
end

What I'd like to do is call Foo.foo instead of Bar.foo. Sometimes i开发者_StackOverflowt feels more natural to call a class method on the original module, especially when the functionality has nothing to do with the included class and is better described along with the original module name.


This seems like a code smell. Having said that, you can just have the Foo module extend itself with the class methods:

module Foo
  extend ActiveSupport::Concern

  module ClassMethods
    def foo
      puts 'foo'
    end
  end

  extend ClassMethods
end

class Bar
  include Foo
end

Bar.foo
Foo.foo
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜