DEV Community

Tomek Urban
Tomek Urban

Posted on • Updated on

How come your model class can call ::devise?

I've decided to read some devise code, for learning purposes. Since I'm on that mission, I'll also write some posts. The thing the internet needs is another blog that will not be read.

Ok, let's get back to the question from the title.

When you'll install devise in your project it will add to your User class these four lines:

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
Enter fullscreen mode Exit fullscreen mode

Nothing else changes in the class. So where the ::devise method comes from?
https://github.com/heartcombo/devise/blob/f39c6fd92774cb66f96f546d8d5e8281542b4e78/lib/devise/orm/active_record.rb

ActiveSupport.on_load(:active_record) do
  extend Devise::Models
end
Enter fullscreen mode Exit fullscreen mode

What does ::on_load do? According to the documentation it:

Declares a block that will be executed when a Rails component is fully loaded.

That's all folks.

Top comments (0)