From bd0e2a31804a8cf0400e8b174bcbedf1d3f37673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 7 Jul 2010 10:51:14 +0200 Subject: [PATCH] devise_for now accepts a block. All routes inside the block uses the scope defined by devise_for. You are now allowed to do: devise_for :users do # Non conventional sign_in route get "/sign_in" => "devise/sessions#new" end And it should work as expected. --- lib/devise/rails/routes.rb | 5 ++-- test/integration/authenticatable_test.rb | 18 ++++++++++--- test/rails_app/config/routes.rb | 32 ++++++++++++++++-------- test/support/integration.rb | 16 +++++++++--- 4 files changed, 52 insertions(+), 19 deletions(-) diff --git a/lib/devise/rails/routes.rb b/lib/devise/rails/routes.rb index 6abc2bc6..550fe1d1 100644 --- a/lib/devise/rails/routes.rb +++ b/lib/devise/rails/routes.rb @@ -151,8 +151,9 @@ module ActionDispatch::Routing routes = mapping.routes routes -= Array(options.delete(:skip)).map { |s| s.to_s.singularize.to_sym } - with_devise_exclusive_scope mapping.fullpath, mapping.name do - devise_scope mapping.name do + devise_scope mapping.name do + yield if block_given? + with_devise_exclusive_scope mapping.fullpath, mapping.name do routes.each { |mod| send(:"devise_#{mod}", mapping, mapping.controllers) } end end diff --git a/test/integration/authenticatable_test.rb b/test/integration/authenticatable_test.rb index 4d67232e..069144f9 100644 --- a/test/integration/authenticatable_test.rb +++ b/test/integration/authenticatable_test.rb @@ -189,7 +189,7 @@ class AuthenticationRedirectTest < ActionController::IntegrationTest follow_redirect! sign_in_as_user :visit => false - assert_template 'users/index' + assert_current_url '/users' assert_nil session[:"user_return_to"] end @@ -205,7 +205,7 @@ class AuthenticationRedirectTest < ActionController::IntegrationTest follow_redirect! sign_in_as_user :visit => false - assert_template 'users/index' + assert_current_url '/users' assert_nil session[:"user_return_to"] end @@ -231,7 +231,7 @@ class AuthenticationSessionTest < ActionController::IntegrationTest assert_redirected_to new_user_session_path end - test 'allows session to be set by a given scope' do + test 'allows session to be set for a given scope' do sign_in_as_user get '/users' assert_equal "Cart", @controller.user_session[:cart] @@ -277,6 +277,18 @@ class AuthenticationWithScopesTest < ActionController::IntegrationTest end end end + + test 'uses the mapping from router' do + sign_in_as_user :visit => "/as/sign_in" + assert warden.authenticated?(:user) + assert_not warden.authenticated?(:admin) + end + + test 'uses the mapping from nested devise_for call' do + sign_in_as_user :visit => "/devise_for/sign_in" + assert warden.authenticated?(:user) + assert_not warden.authenticated?(:admin) + end end class AuthenticationOthersTest < ActionController::IntegrationTest diff --git a/test/rails_app/config/routes.rb b/test/rails_app/config/routes.rb index 739dcaa2..b2566cb7 100644 --- a/test/rails_app/config/routes.rb +++ b/test/rails_app/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + # Resources for testing resources :users, :only => [:index] do get :expire, :on => :member get :accept, :on => :member @@ -6,9 +7,28 @@ Rails.application.routes.draw do resources :admins, :only => [:index] - devise_for :users + # Users scope + devise_for :users do + match "/devise_for/sign_in", :to => "devise/sessions#new" + end + + as :user do + match "/as/sign_in", :to => "devise/sessions#new" + end + + match "/sign_in", :to => "devise/sessions#new" + + # Admin scope devise_for :admin, :path => "admin_area", :controllers => { :sessions => "sessions" }, :skip => :passwords + match "/admin_area/home", :to => "admins#index", :as => :admin_root + match "/anywhere", :to => "foo#bar", :as => :new_admin_password + + authenticate(:admin) do + match "/private", :to => "home#private", :as => :private + end + + # Other routes for routing_test.rb namespace :publisher, :path_names => { :sign_in => "i_don_care", :sign_out => "get_out" } do devise_for :accounts, :class_name => "User", :path_names => { :sign_in => "get_in" } end @@ -23,15 +43,5 @@ Rails.application.routes.draw do } end - match "/admin_area/home", :to => "admins#index", :as => :admin_root - match "/sign_in", :to => "devise/sessions#new" - - # Dummy route for new admin pasword - match "/anywhere", :to => "foo#bar", :as => :new_admin_password - root :to => "home#index" - - authenticate(:admin) do - match "/private", :to => "home#private", :as => :private - end end \ No newline at end of file diff --git a/test/support/integration.rb b/test/support/integration.rb index 42044ae0..be7d4023 100644 --- a/test/support/integration.rb +++ b/test/support/integration.rb @@ -31,7 +31,7 @@ class ActionDispatch::IntegrationTest def sign_in_as_user(options={}, &block) user = create_user(options) - get new_user_session_path unless options[:visit] == false + visit_with_option options[:visit], new_user_session_path fill_in 'email', :with => 'user@test.com' fill_in 'password', :with => options[:password] || '123456' check 'remember me' if options[:remember_me] == true @@ -42,7 +42,7 @@ class ActionDispatch::IntegrationTest def sign_in_as_admin(options={}, &block) admin = create_admin(options) - get new_admin_session_path unless options[:visit] == false + visit_with_option options[:visit], new_admin_session_path fill_in 'email', :with => 'admin@test.com' fill_in 'password', :with => '123456' yield if block_given? @@ -70,9 +70,19 @@ class ActionDispatch::IntegrationTest protected + def visit_with_option(given, default) + case given + when String + visit given + when FalseClass + # Do nothing + else + visit default + end + end + def prepend_host(url) url = "http://#{request.host}#{url}" if url[0] == ?/ url end - end