ruby-on-railsdevisewarden

The reason why it's possible to use helper method like current_user, authenticate_user!, and so on without including any module


The title is my question.

devise provide us many useful methods like current_user, authenticate_user!, and so on. I want to know why is it possible to use them without including any module like below.

class ApplicationController < ActionController::Base
  before_action :authenticate_user!
end

Those method's definition is here

Somebody please help me!


Solution

  • The answer is devise included its helper methods into ActionController on behalf of you when Rails on_load

    # devise/rails.rb
    module Devise
      class Engine < ::Rails::Engine
        # ...
        initializer "devise.url_helpers" do
          Devise.include_helpers(Devise::Controllers)
        end
        # ...
    end
    
    # devise.rb
      # ...
      def self.include_helpers(scope)
        ActiveSupport.on_load(:action_controller) do
          include scope::Helpers if defined?(scope::Helpers)
          include scope::UrlHelpers
        end
    
        ActiveSupport.on_load(:action_view) do
          include scope::UrlHelpers
        end
      end
      # ...   
    

    I saw many 3rd gems using on_load to include their methods (or themselves) into Rails core, maybe it's a typical way to do that (allows Rails to lazily load a lot of components and thus making the app boot faster). If you install some gems and you could use their methods on your model/controller/view then those gems did the same thing devise did above.

    About those methods current_user, authenticate_user! ... they are dynamic methods devise will generate when it did include scope::Helpers into Rails (see code above and the link).