1

I have a controller that I'd like to include some standard methods.

class Main::UsersController < Main::BaseController
  include MyModule::ControllerMethods
end

uninitialized constanct MyModule::ClassMethods::InstanceMethods

My module looks like this, which is also wrong, and was originally meant for a model. What's the best way to do it so that I can use it with a controller as well?

module MyModule
  def self.included(base)
    base.has_one  :example, :autosave => true
    base.before_create :make_awesome        

    base.extend ClassMethods
  end

  module ClassMethods
    ...
    include InstanceMethods
  end

  module InstanceMethods
     ...
  end

  module ControllerMethods
    ...
    # I want to include these in my controller
    def hello; end
    def world; end
  end

end

1 Answer 1

4

Use extend instead of include for your ClassMethods. You should also split your model and controller modules:

module MyModule    
  module ModelMethods
    def acts_as_something
      send :has_one,  :example, :autosave => true
      send :before_create, :make_awesome  
      send :include, InstanceMethods
    end
    module InstanceMethods
      ...
    end


  end

  module ControllerMethods
    ...
    # I want to include these in my controller
    def hello; end
    def world; end
  end

end

ActiveRecord::Base.extend MyModule::ModelMethods

Your Model would then look like this:

class Model < ActiveRecord::Base
  acts_as_something
end
Sign up to request clarification or add additional context in comments.

3 Comments

This article by Yehuda Katz goes in to more detail about why just using extend is better than overriding include to extend: yehudakatz.com/2009/11/12/better-ruby-idioms
Just curious, you put Base.extend MyModule::ModelMethods, but what does Base pertain to. Should it be ActiveRecord::Base?
I thought I would keep it simple by using Base, but it's probably more confusing. I've changed the answer to use ActiveRecord::Base

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.