Home > Blockchain >  The reason why it's possible to use helper method like current_user, authenticate_user!, and so
The reason why it's possible to use helper method like current_user, authenticate_user!, and so

Time:10-09

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!

CodePudding user response:

So one very good thing about rails is the fact that you get a lot of things for free out of the box. One of these things at the top level is autoloading.

So in the case of Devise. When you install Devise and run the generate command, you get a devise.rb file inside of your config/initializers/ folder. This file is always autoloaded by rails on server startup and reload. That's how all these methods are able to be use these devise methods without importing anything.

Read more at rails docs

CodePudding user response:

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).

  • Related