1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

More transparent implementat'n of :format => false

Per josevalim, by setting {:format => false} in @scope[:options],
Rails will pick it up, without the need to alter each devise_*()
method individually.
This commit is contained in:
Brent J. Nordquist 2011-06-27 06:51:04 -05:00
parent 1c81a40a76
commit 7e711089a8
4 changed files with 48 additions and 2 deletions

View file

@ -22,7 +22,7 @@ module Devise
# # is the modules included in the class
#
class Mapping #:nodoc:
attr_reader :singular, :scoped_path, :path, :controllers, :path_names, :class_name, :sign_out_via
attr_reader :singular, :scoped_path, :path, :controllers, :path_names, :class_name, :sign_out_via, :format
alias :name :singular
# Receives an object and find a scope for it. If a scope cannot be found,
@ -71,6 +71,7 @@ module Devise
@defaults.merge!(options[:defaults] || {})
@sign_out_via = options[:sign_out_via] || Devise.sign_out_via
@format = options[:format]
end
# Return modules for the mapping.

View file

@ -103,6 +103,10 @@ module ActionDispatch::Routing
#
# devise_for :users, :only => :sessions
#
# * :format => include "(.:format)" in the generated routes? true by default, set to false to disable:
#
# devise_for :users, :format => false
#
#
# ==== Scoping
#
@ -162,6 +166,8 @@ module ActionDispatch::Routing
options[:constraints] = (@scope[:constraints] || {}).merge(options[:constraints] || {})
options[:defaults] = (@scope[:defaults] || {}).merge(options[:defaults] || {})
@scope[:options] = (@scope[:options] || {}).merge({:format => false}) if options[:format] == false
resources.map!(&:to_sym)
resources.each do |resource|

View file

@ -47,6 +47,10 @@ Rails.application.routes.draw do
devise_for :homebase_admin, :class_name => "Admin", :path => "homebase"
end
# Routes for format=false testing
devise_for :htmlonly_admin, :class_name => "Admin", :skip => [:confirmations, :unlocks], :path => "htmlonly_admin", :format => false
devise_for :htmlonly_users, :class_name => "User", :only => [:confirmations, :unlocks], :path => "htmlonly_users", :format => false
# Other routes for routing_test.rb
devise_for :reader, :class_name => "User", :only => :passwords
@ -73,4 +77,4 @@ Rails.application.routes.draw do
match "/set", :to => "home#set"
match "/unauthenticated", :to => "home#unauthenticated"
root :to => "home#index"
end
end

View file

@ -190,6 +190,41 @@ class CustomizedRoutingTest < ActionController::TestCase
assert_recognizes({:controller => 'devise/registrations', :action => 'new'}, {:path => 'http://10.0.0.100//homebase/sign_up', :method => :get})
end
end
test 'map with format false for sessions' do
assert_recognizes({:controller => 'devise/sessions', :action => 'new'}, {:path => '/htmlonly_admin/sign_in', :method => :get})
assert_raise ActionController::RoutingError do
assert_recognizes({:controller => 'devise/sessions', :action => 'new'}, {:path => '/htmlonly_admin/sign_in.xml', :method => :get})
end
end
test 'map with format false for passwords' do
assert_recognizes({:controller => 'devise/passwords', :action => 'create'}, {:path => '/htmlonly_admin/password', :method => :post})
assert_raise ActionController::RoutingError do
assert_recognizes({:controller => 'devise/passwords', :action => 'create'}, {:path => '/htmlonly_admin/password.xml', :method => :post})
end
end
test 'map with format false for registrations' do
assert_recognizes({:controller => 'devise/registrations', :action => 'new'}, {:path => '/htmlonly_admin/sign_up', :method => :get})
assert_raise ActionController::RoutingError do
assert_recognizes({:controller => 'devise/registrations', :action => 'new'}, {:path => '/htmlonly_admin/sign_up.xml', :method => :get})
end
end
test 'map with format false for confirmations' do
assert_recognizes({:controller => 'devise/confirmations', :action => 'show'}, {:path => '/htmlonly_users/confirmation', :method => :get})
assert_raise ActionController::RoutingError do
assert_recognizes({:controller => 'devise/confirmations', :action => 'show'}, {:path => '/htmlonly_users/confirmation.xml', :method => :get})
end
end
test 'map with format false for unlocks' do
assert_recognizes({:controller => 'devise/unlocks', :action => 'show'}, {:path => '/htmlonly_users/unlock', :method => :get})
assert_raise ActionController::RoutingError do
assert_recognizes({:controller => 'devise/unlocks', :action => 'show'}, {:path => '/htmlonly_users/unlock.xml', :method => :get})
end
end
end
class ScopedRoutingTest < ActionController::TestCase