basecamp/concerning

Cannot define class methods using concerning

Closed this issue · 6 comments

I've got 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

And I would expect this to work:

User.manager?
User.programmer?

But the second raises NoMethodError: undefined method programmer?' for #Class:0x007f9641beafd0`

How can I define class methods with concerning?

If I write it like this, it does work as expected, but it isn't nearly as clean:

  concerning :Programmers do
    included do
      extend User::ProgrammersClassMethods
    end

    module ProgrammersClassMethods 
      def programmer?
        true
      end
    end
  end

Yes - can't do classmethods modules within the block. Plan is to introduce
a DSL for these instead, like class_methods do ...

On Monday, December 23, 2013, John Naegle wrote:

If I write it like this, it does work as expected, but it isn't nearly as
clean:

concerning :Programmers do
included do
extend User::ProgrammerClassMethods
end

module ProgrammerClassMethods
  def programmer?
    true
  end
end

end


Reply to this email directly or view it on GitHubhttps://github.com//issues/1#issuecomment-31141240
.

Another workaround:

concerning :Programmers do
  included do
    def self.programmer?
      true
    end
  end
end

@javan - that works

Just noticed this issue after I wrote this PR #2. It looks like my solution is consistent with @jeremy's #1 (comment).

Merged #2 - check out class_methods do ... for declaring class methods. Thanks @barelyknown!