From bba6562dccbb18a727e92c39668546084410ea75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 29 Sep 2011 13:07:13 +0200 Subject: [PATCH] after_sign_in_path_for now redirects to session[scope_return_to] if any value is stored in it --- CHANGELOG.rdoc | 4 +++ .../devise/confirmations_controller.rb | 2 +- .../devise/passwords_controller.rb | 2 +- .../devise/registrations_controller.rb | 15 ++------ app/controllers/devise/sessions_controller.rb | 2 +- app/controllers/devise/unlocks_controller.rb | 2 +- lib/devise/controllers/helpers.rb | 34 ++++++++++++------- 7 files changed, 31 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index ce89146c..a928874c 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -4,6 +4,10 @@ * Allow idempotent API requests * Fix bug where logs did not show 401 as status code +* deprecation + * redirect_location is deprecated, please use after_sign_in_path_for + * after_sign_in_path_for now redirects to session[scope_return_to] if any value is stored in it + == 1.4.7 * bug fix diff --git a/app/controllers/devise/confirmations_controller.rb b/app/controllers/devise/confirmations_controller.rb index 54ab92b0..6443ee86 100644 --- a/app/controllers/devise/confirmations_controller.rb +++ b/app/controllers/devise/confirmations_controller.rb @@ -41,7 +41,7 @@ class Devise::ConfirmationsController < ApplicationController # The path used after confirmation. def after_confirmation_path_for(resource_name, resource) - redirect_location(resource_name, resource) + after_sign_in_path_for(resource) end end diff --git a/app/controllers/devise/passwords_controller.rb b/app/controllers/devise/passwords_controller.rb index 857b23b0..74b589ca 100644 --- a/app/controllers/devise/passwords_controller.rb +++ b/app/controllers/devise/passwords_controller.rb @@ -35,7 +35,7 @@ class Devise::PasswordsController < ApplicationController flash_message = resource.active_for_authentication? ? :updated : :updated_not_active set_flash_message(:notice, flash_message) if is_navigational_format? sign_in(resource_name, resource) - respond_with resource, :location => redirect_location(resource_name, resource) + respond_with resource, :location => after_sign_in_path_for(resource) else respond_with_navigational(resource){ render_with_scope :edit } end diff --git a/app/controllers/devise/registrations_controller.rb b/app/controllers/devise/registrations_controller.rb index d66073fc..591b253e 100644 --- a/app/controllers/devise/registrations_controller.rb +++ b/app/controllers/devise/registrations_controller.rb @@ -17,7 +17,7 @@ class Devise::RegistrationsController < ApplicationController if resource.active_for_authentication? set_flash_message :notice, :signed_up if is_navigational_format? sign_in(resource_name, resource) - respond_with resource, :location => redirect_location(resource_name, resource) + respond_with resource, :location => after_sign_up_path_for(resource) else set_flash_message :notice, :inactive_signed_up, :reason => inactive_reason(resource) if is_navigational_format? expire_session_data_after_sign_in! @@ -83,11 +83,6 @@ class Devise::RegistrationsController < ApplicationController after_sign_in_path_for(resource) end - # Overwrite redirect_for_sign_in so it takes uses after_sign_up_path_for. - def redirect_location(scope, resource) - stored_location_for(scope) || after_sign_up_path_for(resource) - end - # Returns the inactive reason translated. def inactive_reason(resource) reason = resource.inactive_message.to_s @@ -103,13 +98,7 @@ class Devise::RegistrationsController < ApplicationController # The default url to be used after updating a resource. You need to overwrite # this method in your own RegistrationsController. def after_update_path_for(resource) - if defined?(super) - ActiveSupport::Deprecation.warn "Defining after_update_path_for in ApplicationController " << - "is deprecated. Please add a RegistrationsController to your application and define it there." - super - else - after_sign_in_path_for(resource) - end + signed_in_root_path(resource) end # Authenticates the current scope and gets the current resource from the session. diff --git a/app/controllers/devise/sessions_controller.rb b/app/controllers/devise/sessions_controller.rb index 83c590ce..128f2ee9 100644 --- a/app/controllers/devise/sessions_controller.rb +++ b/app/controllers/devise/sessions_controller.rb @@ -15,7 +15,7 @@ class Devise::SessionsController < ApplicationController resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new") set_flash_message(:notice, :signed_in) if is_navigational_format? sign_in(resource_name, resource) - respond_with resource, :location => redirect_location(resource_name, resource) + respond_with resource, :location => after_sign_in_path_for(resource) end # DELETE /resource/sign_out diff --git a/app/controllers/devise/unlocks_controller.rb b/app/controllers/devise/unlocks_controller.rb index 15933cd6..b9151602 100644 --- a/app/controllers/devise/unlocks_controller.rb +++ b/app/controllers/devise/unlocks_controller.rb @@ -27,7 +27,7 @@ class Devise::UnlocksController < ApplicationController if resource.errors.empty? set_flash_message :notice, :unlocked if is_navigational_format? sign_in(resource_name, resource) - respond_with_navigational(resource){ redirect_to redirect_location(resource_name, resource) } + respond_with_navigational(resource){ redirect_to after_sign_in_path_for(resource) } else respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render_with_scope :new } end diff --git a/lib/devise/controllers/helpers.rb b/lib/devise/controllers/helpers.rb index 8dd60c3a..fc6b632c 100644 --- a/lib/devise/controllers/helpers.rb +++ b/lib/devise/controllers/helpers.rb @@ -166,12 +166,21 @@ module Devise session.delete("#{scope}_return_to") end + # The scope root url to be used when he's signed in. By default, it first + # tries to find a resource_root_path, otherwise it uses the root_path. + def signed_in_root_path(resource_or_scope) + scope = Devise::Mapping.find_scope!(resource_or_scope) + home_path = "#{scope}_root_path" + respond_to?(home_path, true) ? send(home_path) : root_path + end + # The default url to be used after signing in. This is used by all Devise # controllers and you can overwrite it in your ApplicationController to # provide a custom hook for a custom resource. # - # By default, it first tries to find a resource_root_path, otherwise it - # uses the root path. For a user scope, you can define the default url in + # By default, it first tries to find a valid resource_return_to key in the + # session, then it fallbacks to resource_root_path, otherwise it uses the + # root path. For a user scope, you can define the default url in # the following way: # # map.user_root '/users', :controller => 'users' # creates user_root_path @@ -180,22 +189,20 @@ module Devise # user.root :controller => 'users' # creates user_root_path # end # - # # If the resource root path is not defined, root_path is used. However, # if this default is not enough, you can customize it, for example: # # def after_sign_in_path_for(resource) - # if resource.is_a?(User) && resource.can_publish? - # publisher_url - # else - # super - # end + # store_location_for(resource) || + # if resource.is_a?(User) && resource.can_publish? + # publisher_url + # else + # signed_in_root_path(resource) + # end # end # def after_sign_in_path_for(resource_or_scope) - scope = Devise::Mapping.find_scope!(resource_or_scope) - home_path = "#{scope}_root_path" - respond_to?(home_path, true) ? send(home_path) : root_path + stored_location_for(resource_or_scope) || signed_in_root_path(resource_or_scope) end # Method used by sessions controller to sign out a user. You can overwrite @@ -216,11 +223,12 @@ module Devise scope = Devise::Mapping.find_scope!(resource_or_scope) resource = args.last || resource_or_scope sign_in(scope, resource, options) - redirect_to redirect_location(scope, resource) + redirect_to after_sign_in_path_for(resource) end def redirect_location(scope, resource) #:nodoc: - stored_location_for(scope) || after_sign_in_path_for(resource) + ActiveSupport::Deprecation.warn "redirect_location in Devise is deprecated. Please use after_sign_in_path_for instead.", caller + after_sign_in_path_for(resource) end # Sign out a user and tries to redirect to the url specified by