3

Consider this code

module Auth

  def sign_in(user)
    #some stuff
    session[:user_id] = user.id
  end

end

Now, I want to include this in my application controller.

ApplicationController < ActionController::Base
  include Auth
end

This makes the sign_in method available in all my controllers. Now, to make it clear that this is not a controller action, I would like to maintain the name, so my controllers read

def sign_user_in
  # Some stuff
  Auth.sign_in(@user)
end

This obviously does not work, because Rails will look for the class method in the Auth module. So the question is... Is it possible to include a module into the controller, preserve it's name or namespace, but still get access to the same scope as the controller? (in this case, the session variable).

So far the least bad way I have come up with is stop including the module and in the ApplicationController and instead pass the application controller instance along when calling an auth method like this:

def current_user(controller)
  User.find(controller.session[:user_id])
end

Calling this method from a controller using using self as an argument works.

15
  • 1
    IMO that would be the worst of both worlds: all the dangers of exposure with none of the single-namespace benefits. Commented May 11, 2013 at 22:25
  • Single-namespace benefits? What are you talking about? I have a collection of methods that are operates on controller instances, but I don't want my application controller to grow fat. Also, I like the method context that a constant provides. I think it improves readability. I know that I can use prefixes instead and do auth_method_name, but I really think Auth.method_name looks better. JavaScript offers this thanks to it's execution context, but how does Ruby do it? If I thought there was anything beneficial about "single namespacing", I'd be programming PHP. Commented May 11, 2013 at 22:30
  • TL;DR. You explicitly said you wanted to access session from inside the mixin--but only in the mixin, not in the controller. The dangers of exposure, w/o the benefits. If you're going to separate concerns, separate them, and use a service object. Simply moving functionality to a mixin doesn't make the controller any less fat: moving it to an isolated class does. Commented May 11, 2013 at 22:39
  • You are being rude. If you don't want to read my posts, close the browser tab. I want my controller to be fat at runtime, but manageable in my editor. One application controller with 20 methods that don't answer the router is not manageable to me. It is confusing. A controller method calling some method called sign_in in this single namespace, is not helpful either. Is it a (view) helper? Perhaps another controller action? Maybe it's inherited from ActionController::Base? Is there really nothing better than a method prefix for this? Commented May 11, 2013 at 22:51
  • 2
    I know you want to, which is precisely why I think it's the worst of both worlds: a fat controller that pretends Auth is something separate when it's not. Code that looks like it's calling class methods when it's not is code that lies. Commented May 11, 2013 at 23:45

2 Answers 2

2

Try this out?

Uses an actual class for all the functionality and the controller has an instance of that class available; basically the exact same code you have above - see current_user but you only need to pass the controller instance in once, not on every method call

module Auth
  # this method is 'mixed' into controller (self)
  def initialize_authorizer
    @authorizer = ::Auth::Authorizer(self)
  end

  # this class doesn't need to be in this namespace (module), put it where ever makes sense
  class Authorizer
    def initialize(controller)
      @controller = controller
    end

    attr_reader :controller

    def sign_in(user)
      #some stuff
      controller.session[:user_id] = user.id
    end

    def current_user
      User.find(controller.session[:user_id])
    end        
  end
end

ApplicationController < ActionController::Base
  include Auth

  before_filter :initialize_authorizer
end

def sign_user_in
  # Some stuff
  @authorizer.sign_in(@user)
end    
Sign up to request clarification or add additional context in comments.

1 Comment

I really like the idea of using an instance variable. With your solution I don't need the namespace. I can create the controller extension as a class called auth and then set it up with for use in the application controller using the before_filter.
0

It has been 9 years since I asked this question. In the meantime I have realized that it is not a good idea to do this as it rubs against the language.

Constants have their own self and when referencing a constant, you would expect any methods to be class methods. You would not expect them to have access to the calling object unless an a reference is explicitly passed during the method call, in which case you have a bidirectional dependency, which comes with its own set of problems. That would be a code smell and should be a cause for refactoring the software design.

Comments

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.