Created
March 1, 2014 11:39
-
-
Save superbilk/9288685 to your computer and use it in GitHub Desktop.
Please have a look at the changes I made in do_show and do_confirm in the confirmations_controller.rb (Related question: https://groups.google.com/forum/#!topic/plataformatec-devise/4amIRvFCcxY)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /app/controller/confirmations_controller.rb | |
class ConfirmationsController < Devise::ConfirmationsController | |
skip_before_filter :authenticate_user! | |
# PUT /resource/confirmation | |
def update | |
with_unconfirmed_confirmable do | |
if @confirmable.has_no_password? | |
@confirmable.attempt_set_password(params[:user]) | |
if @confirmable.valid? | |
do_confirm | |
else | |
do_show | |
@confirmable.errors.clear #so that we wont render :new | |
end | |
else | |
self.class.add_error_on(self, :email, :password_allready_set) | |
end | |
end | |
if [email protected]? | |
render 'devise/confirmations/new' #Change this if you don't have the views on default path | |
end | |
end | |
# GET /resource/confirmation?confirmation_token=abcdef | |
def show | |
with_unconfirmed_confirmable do | |
if @confirmable.has_no_password? | |
do_show | |
else | |
do_confirm | |
end | |
end | |
if [email protected]? | |
self.resource = @confirmable | |
render 'devise/confirmations/new' #Change this if you don't have the views on default path | |
end | |
end | |
protected | |
def with_unconfirmed_confirmable | |
original_token = params[:confirmation_token] | |
confirmation_token = Devise.token_generator.digest(User, :confirmation_token, original_token) | |
@confirmable = User.find_or_initialize_with_error_by(:confirmation_token, confirmation_token) | |
if [email protected]_record? | |
@confirmable.only_if_unconfirmed {yield} | |
end | |
end | |
def do_show | |
@confirmation_token = params[:confirmation_token] | |
@requires_password = true | |
self.resource = @confirmable | |
# added to enrich the confirmation form | |
@company = Company.find_or_create_by(domain: Mail::Address.new(@confirmable.email).domain) | |
render 'devise/confirmations/show' #Change this if you don't have the views on default path | |
end | |
def do_confirm | |
@confirmable.confirm! | |
# added to update other models on confirmation | |
user = User.find(@confirmable) | |
company = Company.find_or_create_by(domain: Mail::Address.new(@confirmable.email).domain) | |
company.name = params[:user][:company_name] if company.name.nil? | |
if company.admin.nil? | |
company.admin = user | |
end | |
company.save | |
user.first_name = params[:user][:first_name] | |
user.last_name = params[:user][:last_name] | |
user.employer = company | |
user.save | |
set_flash_message :notice, :confirmed | |
sign_in_and_redirect(resource_name, @confirmable) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /app/models/user.rb | |
class User < ActiveRecord::Base | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable, :confirmable | |
attr_readonly :email | |
attr_accessor :company_name | |
attr_accessor :company_domain | |
# removed methods needed to Override confirmations so users can pick their own passwords as part of confirmation activation | |
# https://github.com/plataformatec/devise/wiki/How-To:-Override-confirmations-so-users-can-pick-their-own-passwords-as-part-of-confirmation-activation | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /app/lib/user_sanitizer.rb | |
# required in an initializer | |
class UserParameterSanitizer < Devise::ParameterSanitizer | |
private | |
def account_update | |
default_params.permit(:first_name, :last_name, :company_name, :company_domain, :email, :password, :password_confirmation, :current_password) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment