From 6199252011d291b02910ae1ffd44dd9183c57d26 Mon Sep 17 00:00:00 2001 From: Rodrigo Flores Date: Wed, 22 Jun 2011 12:33:28 -0300 Subject: [PATCH] Refactored paranoid mode actions for it to appear once --- .../devise/confirmations_controller.rb | 15 +++++---------- app/controllers/devise/passwords_controller.rb | 14 ++++---------- app/controllers/devise/unlocks_controller.rb | 14 ++++---------- lib/devise/controllers/internal_helpers.rb | 13 +++++++++++++ 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/app/controllers/devise/confirmations_controller.rb b/app/controllers/devise/confirmations_controller.rb index 3d3b496b..e8809b2b 100644 --- a/app/controllers/devise/confirmations_controller.rb +++ b/app/controllers/devise/confirmations_controller.rb @@ -11,17 +11,12 @@ class Devise::ConfirmationsController < ApplicationController def create self.resource = resource_class.send_confirmation_instructions(params[resource_name]) - if Devise.paranoid - set_flash_message(:notice, :send_paranoid_instructions) if is_navigational_format? - resource.errors.clear - respond_with_navigational(resource){ render_with_scope :new } + + if successful_and_sane?(resource) + set_flash_message(:notice, :send_instructions) if is_navigational_format? + respond_with resource, :location => after_resending_confirmation_instructions_path_for(resource_name) else - if resource.errors.empty? - set_flash_message(:notice, :send_instructions) if is_navigational_format? - respond_with resource, :location => after_resending_confirmation_instructions_path_for(resource_name) - else - respond_with_navigational(resource){ render_with_scope :new } - end + respond_with_navigational(resource){ render_with_scope :new } end end diff --git a/app/controllers/devise/passwords_controller.rb b/app/controllers/devise/passwords_controller.rb index a28ec29e..cff79bc9 100644 --- a/app/controllers/devise/passwords_controller.rb +++ b/app/controllers/devise/passwords_controller.rb @@ -12,17 +12,11 @@ class Devise::PasswordsController < ApplicationController def create self.resource = resource_class.send_reset_password_instructions(params[resource_name]) - if Devise.paranoid - set_flash_message(:notice, :send_paranoid_instructions) if is_navigational_format? - resource.errors.clear - respond_with_navigational(resource) { render_with_scope :new } + if successful_and_sane?(resource) + set_flash_message(:notice, :send_instructions) if is_navigational_format? + respond_with resource, :location => new_session_path(resource_name) else - if resource.errors.empty? - set_flash_message(:notice, :send_instructions) if is_navigational_format? - respond_with resource, :location => new_session_path(resource_name) - else - respond_with_navigational(resource){ render_with_scope :new } - end + respond_with_navigational(resource){ render_with_scope :new } end end diff --git a/app/controllers/devise/unlocks_controller.rb b/app/controllers/devise/unlocks_controller.rb index 0f5585a9..73532c76 100644 --- a/app/controllers/devise/unlocks_controller.rb +++ b/app/controllers/devise/unlocks_controller.rb @@ -12,17 +12,11 @@ class Devise::UnlocksController < ApplicationController def create self.resource = resource_class.send_unlock_instructions(params[resource_name]) - if Devise.paranoid - set_flash_message :notice, :send_paranoid_instructions if is_navigational_format? - resource.errors.clear - respond_with_navigational(resource){ render_with_scope :new } + if successful_and_sane?(resource) + set_flash_message :notice, :send_instructions if is_navigational_format? + respond_with resource, :location => new_session_path(resource_name) else - if resource.errors.empty? - set_flash_message :notice, :send_instructions if is_navigational_format? - respond_with resource, :location => new_session_path(resource_name) - else - respond_with_navigational(resource){ render_with_scope :new } - end + respond_with_navigational(resource){ render_with_scope :new } end end diff --git a/lib/devise/controllers/internal_helpers.rb b/lib/devise/controllers/internal_helpers.rb index 410678a7..098e4136 100644 --- a/lib/devise/controllers/internal_helpers.rb +++ b/lib/devise/controllers/internal_helpers.rb @@ -99,6 +99,19 @@ MESSAGE end end + # Helper for use to validate if an resource is errorless. If we are on paranoid mode, we always should assume it is + # and return false. + + def successful_and_sane?(resource) + if Devise.paranoid + set_flash_message :notice, :send_paranoid_instructions if is_navigational_format? + resource.errors.clear + false + else + resource.errors.empty? + end + end + # Sets the flash message with :key, using I18n. By default you are able # to setup your messages using specific resource scope, and if no one is # found we look to default scope.