From a796dade7aab64f4994e47c9e0c3c6fb7e32db8f Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 26 Jan 2016 19:48:01 +0100 Subject: [PATCH] extracts methods into user.rb --- .../users/omniauth_callbacks_controller.rb | 16 +++------------- .../users/registrations_controller.rb | 10 +--------- app/models/user.rb | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index ca0dfc667..f3229bee1 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -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 diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 0d32e725a..8a4aca2a8 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index bf85babed..a79e8acc2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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?