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)

Let's Get Wacky


Use any Linode offering to create something unique or silly in the DEV x Linode Hackathon 2022 and win the Wacky Wildcard category

β†’ Join the Hackathon <-