From a5b2ee51710aadeebe73c76f60ea431c130e8c44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 16 Jan 2010 10:56:35 +0100 Subject: [PATCH] Improve documentation for after_sign_in_path_for. --- README.rdoc | 4 ++- app/controllers/sessions_controller.rb | 4 +-- lib/devise/controllers/helpers.rb | 35 ++++++++++++++++++-------- test/controllers/helpers_test.rb | 5 ++-- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/README.rdoc b/README.rdoc index bbfdbeb2..df916b12 100644 --- a/README.rdoc +++ b/README.rdoc @@ -48,7 +48,9 @@ Run the generator: ruby script/generate devise_install -And you're ready to go. The generator will install an initializer which describes Devise's configuration options. Be sure to take a look. +And you're ready to go. The generator will install an initializer which describes ALL Devise's configuration options, so be sure to take a look at it and the documentation as well: + + http://rdoc.info/projects/plataformatec/devise == Basic Usage diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 11bc19ba..022aa28f 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -14,9 +14,9 @@ class SessionsController < ApplicationController # POST /resource/sign_in def create - if authenticate(resource_name) + if resource = authenticate(resource_name) set_flash_message :success, :signed_in - sign_in_and_redirect(resource_name) + sign_in_and_redirect(resource_name, resource, true) else set_now_flash_message :failure, warden.message || :invalid build_resource diff --git a/lib/devise/controllers/helpers.rb b/lib/devise/controllers/helpers.rb index 90958c2b..83ae74d5 100644 --- a/lib/devise/controllers/helpers.rb +++ b/lib/devise/controllers/helpers.rb @@ -57,7 +57,7 @@ module Devise # sign_in @user # sign_in(resource) # def sign_in(resource_or_scope, resource=nil) - scope ||= Devise::Mapping.find_scope!(resource_or_scope) + scope = Devise::Mapping.find_scope!(resource_or_scope) resource ||= resource_or_scope warden.set_user(resource, :scope => scope) end @@ -103,16 +103,28 @@ module Devise # users.root # creates user_root_path # end # - # If none of these are defined, root_path is used. + # + # If none of these are 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? + # redirect_to publisher_url + # else + # super + # 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 end - # The default to be used after signing out. This is used by all Devise - # controllers and you can overwrite it in your ApplicationController to - # provide a custom hook for a custom resource. + # Method used by sessions controller to sign out an user. You can overwrite + # it in your ApplicationController to provide a custom hook for a custom + # scope. Notice that differently from +after_sign_in_path_for+ this method + # receives a symbol with the scope, and not the resource. # # By default is the root_path. def after_sign_out_path_for(resource_or_scope) @@ -124,16 +136,19 @@ module Devise # # If just a symbol is given, consider that the user was already signed in # through other means and just perform the redirection. - def sign_in_and_redirect(*args) - sign_in(*args) unless args.size == 1 && args.first.is_a?(Symbol) - redirect_to stored_location_for(args.first) || after_sign_in_path_for(args.first) + def sign_in_and_redirect(resource_or_scope, resource=nil, skip=false) + scope = Devise::Mapping.find_scope!(resource_or_scope) + resource ||= resource_or_scope + sign_in(scope, resource) unless skip + redirect_to stored_location_for(scope) || after_sign_in_path_for(resource) end # Sign out an user and tries to redirect to the url specified by # after_sign_out_path_for. def sign_out_and_redirect(resource_or_scope) - sign_out(resource_or_scope) - redirect_to after_sign_out_path_for(resource_or_scope) + scope = Devise::Mapping.find_scope!(resource_or_scope) + sign_out(scope) + redirect_to after_sign_out_path_for(scope) end # Define authentication filters and accessor helpers based on mappings. diff --git a/test/controllers/helpers_test.rb b/test/controllers/helpers_test.rb index 9b51360e..80d397b2 100644 --- a/test/controllers/helpers_test.rb +++ b/test/controllers/helpers_test.rb @@ -148,9 +148,10 @@ class ControllerAuthenticableTest < ActionController::TestCase @controller.sign_in_and_redirect(admin) end - test 'only redirect if just a symbol is given' do + test 'only redirect if skip is given' do + admin = Admin.new @controller.expects(:redirect_to).with(admin_root_path) - @controller.sign_in_and_redirect(:admin) + @controller.sign_in_and_redirect(:admin, admin, true) end test 'sign out and redirect uses the configured after sign out path' do