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.
This commit is contained in:
José Valim 2010-07-07 10:51:14 +02:00
parent 750560ae87
commit bd0e2a3180
4 changed files with 52 additions and 19 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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