Can I define class methods using module number?

I would like to define a class method using Module # relatively ( https://github.com/37signals/concerning - part of Rails 4.1). This will allow me to move the modules that are used by one class back to the class.

However, it seems like I cannot define class methods. Considering this:

class User < ActiveRecord::Base attr_accessible :name concerning :Programmers do module ClassMethods def programmer? true end end end module Managers extend ActiveSupport::Concern module ClassMethods def manager? true end end end include Managers end 

I would expect both of them to work:

 User.manager? User.programmer? 

But the second one causes

 NoMethodError: undefined method `programmer?' for #<Class:0x007f9641beafd0> 

How to define class methods using module # related to?

+7
ruby-on-rails ruby-on-rails-3
source share
3 answers

https://github.com/basecamp/concerning/pull/2 fixed this:

 class User < ActiveRecord::Base concerning :Programmers do class_methods do def im_a_class_method puts "Yes!" end end end end 

Console:

 > User.im_a_class_method Yes! 
+7
source share

Try this instead:

 concerning :Programmers do included do def self.programmer? true end end end 
+5
source share

Quick workaround:

 concerning :MeaningOfLife do included { extend ClassMethods } module ClassMethods ... end 
+2
source share

All Articles