1

I'm trying to understand the piece of Rails code below, and I'm not getting how the request object, with the header's attributes, is visible inside a method defined in a module that is required by the ApplicationController.

The relevant code:

in controllers/application_controller.rb

class ApplicationController < ActionController::Base
    ...
    include Authenticable
    ...
end

in the controllers/concerns/authenticable.rb

module Authenticable

    def current_user
        @current_user = User.find_by( auth_token: request.headers['Authorization'])
    end

in controllers/my_controller.rb

class MyController < ApplicationController
    ...
    def some_action
        user = current_user
        ...
    end
    ...
end

As far as I (currently) know, a request object instance is created by Rack and passed onto a MyController object by Router. Correct?

This MyController object descends from ApplicationController and so inherits its properties; the opposite is not true: ApplicationController has no access to MyController properties. Correct?

If ApplicationController requires a module, it expands its properties by the methods in the module. So, these 'required' methods have no access to MyController properties, in the current case, the request object. Correct?

Where am I failing, so that method current_user inside module Authenticable can access request.headers['Authorization']?

Thanks

1 Answers1

0

Including a module can add both class and instance methods, and those methods can have various visibilities (private, public, protected).

Your included module probably has a public method which is visible inside your controller.

Piotr Kruczek
  • 2,384
  • 11
  • 18
  • That is not the issue here. Module is just that. The point is how it's possible a super class method access a descendent class property. I'm investigating whether, as this is interpreted, by the run time that this access is done, i.e., when some_action is executed, all its methods, both its own and its inherited ones share the same visibility/ context. – Pedro Centeno Jun 16 '15 at 08:38