DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

mattr_accessor and meta programming. Thanks you, devise_invitable 👍

It was a impressed for me.

I have a code that inherits Devise::InvitationController, and the controller seems to inherits ApplicationController too.
It was magic. I wanted to understand why. so I read the code.

Then, open them.

$ bundle open devise_invitable
$ bundle open devise

app/controllers/devise/invitations_controller.rb

class Devise::InvitationsController < DeviseController

app/controllers/devise_controller.rb

class DeviseController < Devise.parent_controller.constantize

lib/devise.rb

# The parent controller all Devise controllers inherits from.
# Defaults to ApplicationController. This should be set early
# in the initialization process and should be set to a string.
mattr_accessor :parent_controller
@@parent_controller = "ApplicationController"

What happen?

The mattr_accessor is similar with attr_accessor. It automatically makes a get/set method for the module.

Then, Devise.parent_controller become "ApplicationController". Of course, you can customize it.

The reference of mattr_accessor.

This programming gives you power.

🔗 Parent Note

Top comments (0)