I have a Rails 4 app using Devise for authentication — and it is currently working.
I am now following this coderwall tutorial about Creating a Scoped Invitation System for Rails.
In the Newly Invited user registration section, the author recommends to udpate the RegistrationsController
, as follows:
def new
@token = params[:invite_token] #<-- pulls the value from the url query string
end
def create
@newUser = build_user(user_params)
@newUser.save
@token = params[:invite_token]
if @token != nil
org = Invite.find_by_token(@token).user_group #find the user group attached to the invite
@newUser.user_groups.push(org) #add this user to the new user group as a member
else
# do normal registration things #
end
end
There are plenty of questions and answers, on Stack Overflow as well as accross the web, explaining how to override Devise RegistrationsControllers
, including:
- Override devise registrations controller
- How to override devise registrations controller
- How to Override and Customize the Devise Controller in Rails
So, I understand that creating a MyDevise::RegistrationController
that would inherit from Devise::RegistrationsController
and calling the super
command at the beginning of the actions I want to modify will keep the original functions of these actions.
I could probably do something like:
def new
super
@token = params[:invite_token] #<-- pulls the value from the url query string
end
def create
@newUser = build_user(user_params)
@newUser.save
@token = params[:invite_token]
if @token != nil
org = Invite.find_by_token(@token).user_group #find the user group attached to the invite
@newUser.user_groups.push(org) #add this user to the new user group as a member
else
super
end
end
My only concern is that I am not comfortable overriding a controller and its actions without knowing the original content of this controller and those actions.
—————
UPDATE: I know we can access the Devise::RegistrationsController
from Devise GitHub repository, but I am not sure mine is still the same. It could for instance have been modified when I implemented my authentication system.
—————
UPDATE 2: if I use the code from Devise::RegistrationsController
mention in my first update, I can come up with the following code for my new RegistrationsController
:
def new
@token = params[:invite_token] #<-- pulls the value from the url query string
build_resource({})
set_minimum_password_length
yield resource if block_given?
respond_with self.resource
end
def create
build_resource(sign_up_params)
resource.save
@token = params[:invite_token]
if @token != nil
org = Invite.find_by_token(@token).calendar #find the calendar attached to the invite
resource.calendars.push(org) #add this user to the new calendar as a member
else
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_flashing_format?
sign_up(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
set_minimum_password_length
respond_with resource
end
end
end
Does that make sense at all?
—————
So, is there a way to pull out the content of my current Devise::RegistrationsController
from somewhere in the app?
If not, does the code I am considering implementing make sense?