extracts methods into user.rb

This commit is contained in:
kikito
2016-01-26 19:48:01 +01:00
parent a23d0ebbd8
commit a796dade7a
3 changed files with 20 additions and 22 deletions

View File

@@ -42,19 +42,9 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
end
def save_user(user)
# If there are no problems with the email/username, then they were provided by oauth or they
# correspond to an existing user. Associate the identity and sign in
return true if @user.save
# If either the username or email have provoked a failure, we save the user anyway (but marked for revision)
# This mark will be detected by applicationcontroller and the user will be redirected to finish_signup
@user.registering_with_oauth = true
return true if @user.save
# If we still can't save the user, the email might be invalidating devise's validatable "unique"
# constraint. Set email to nil and try again (we'll reset later using oauth_email)
@user.email = nil
@user.save
@user.save ||
@user.save_requiring_finish_signup ||
@user.save_requiring_finish_signup_without_email
end
end

View File

@@ -32,15 +32,7 @@ class Users::RegistrationsController < Devise::RegistrationsController
def do_finish_signup
current_user.registering_with_oauth = false
if current_user.update(sign_up_params)
if current_user.oauth_email != current_user.email
current_user.update(confirmed_at: nil)
current_user.send_confirmation_instructions
end
if current_user.oauth_email.present?
current_user.update(oauth_email: nil)
end
current_user.send_oauth_confirmation_instructions
sign_in_and_redirect current_user, event: :authentication
else
render :finish_signup

View File

@@ -187,6 +187,22 @@ class User < ActiveRecord::Base
super && !registering_with_oauth
end
def send_oauth_confirmation_instructions
if oauth_email != email
self.update(confirmed_at: nil)
self.send_confirmation_instructions
end
self.update(oauth_email: nil) if oauth_email.present?
end
def save_requiring_finish_signup
self.update(registering_with_oauth: true)
end
def save_requiring_finish_signup_without_email
self.update(registering_with_oauth: true, email: nil)
end
private
def clean_document_number
self.document_number = self.document_number.gsub(/[^a-z0-9]+/i, "").upcase unless self.document_number.blank?