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

Allow passing options from devise_for to route definitions, useful on subdomain scenarios.

This commit is contained in:
Carlos Antonio da Silva 2009-11-13 08:49:22 -02:00
parent 8628cf0d6b
commit 836770a9f2
5 changed files with 28 additions and 10 deletions

View file

@ -22,7 +22,7 @@ module Devise
# # is the modules included in the class
#
class Mapping #:nodoc:
attr_reader :name, :as, :path_names, :path_prefix
attr_reader :name, :as, :path_names, :path_prefix, :route_options
# Loop through all mappings looking for a map that matches with the requested
# path (ie /users/sign_in). If a path prefix is given, it's taken into account.
@ -40,12 +40,13 @@ module Devise
end
def initialize(name, options) #:nodoc:
@as = (options[:as] || name).to_sym
@klass = (options[:class_name] || name.to_s.classify).to_s
@name = (options[:singular] || name.to_s.singularize).to_sym
@path_names = options[:path_names] || {}
@path_prefix = options[:path_prefix] || ""
@as = (options.delete(:as) || name).to_sym
@klass = (options.delete(:class_name) || name.to_s.classify).to_s
@name = (options.delete(:singular) || name.to_s.singularize).to_sym
@path_names = options.delete(:path_names) || {}
@path_prefix = options.delete(:path_prefix) || ""
@path_prefix << "/" unless @path_prefix[-1] == ?/
@route_options = options || {}
setup_path_names
end

View file

@ -64,6 +64,10 @@ module ActionController::Routing
#
# map.devise_for :users, :path_prefix => "/:locale"
#
# Any other options will be passed to route definition. If you need conditions for your routes, just map:
#
# map.devise_for :users, :conditions => { :subdomain => /.+/ }
#
# If you are using a dynamic prefix, like :locale above, you need to configure default_url_options through Devise. You can do that in config/initializers/devise.rb or setting a Devise.default_url_options:
#
# Devise.default_url_options do
@ -78,7 +82,7 @@ module ActionController::Routing
mapping = Devise::Mapping.new(resource, options)
Devise.mappings[mapping.name] = mapping
with_options(:path_prefix => mapping.raw_path, :name_prefix => "#{mapping.name}_") do |routes|
with_options(mapping.route_options.dup.update(:path_prefix => mapping.raw_path, :name_prefix => "#{mapping.name}_")) do |routes|
mapping.for.each do |strategy|
send(strategy, routes, mapping) if self.respond_to?(strategy, true)
end

View file

@ -85,6 +85,14 @@ class MappingTest < ActiveSupport::TestCase
end
end
test 'should have default route options' do
assert_equal({}, Devise.mappings[:user].route_options)
end
test 'should allow passing route options to devise routes' do
assert_equal({ :requirements => { :extra => 'value' } }, Devise.mappings[:manager].route_options)
end
test 'magic predicates' do
mapping = Devise.mappings[:user]
assert mapping.authenticatable?

View file

@ -4,7 +4,9 @@ ActionController::Routing::Routes.draw do |map|
map.devise_for :account, :path_names => {
:sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification'
}
map.devise_for :organizers, :singular => 'manager', :path_prefix => '/:locale'
map.devise_for :organizers, :singular => 'manager',
:path_prefix => '/:locale',
:requirements => { :extra => 'value' }
map.resources :users, :only => :index
map.resources :admins, :only => :index

View file

@ -69,11 +69,14 @@ class MapRoutingTest < ActionController::TestCase
end
test 'map organizer with custom singular name' do
assert_recognizes({:controller => 'passwords', :action => 'new', :locale => "en"}, '/en/organizers/password/new')
assert_recognizes({:controller => 'passwords', :action => 'new', :locale => "en", :extra => 'value'}, '/en/organizers/password/new')
end
test 'map organizer with path prefix' do
assert_recognizes({:controller => 'sessions', :action => 'new', :locale => "en"}, '/en/organizers/sign_in')
assert_recognizes({:controller => 'sessions', :action => 'new', :locale => "en", :extra => 'value'}, '/en/organizers/sign_in')
end
test 'map organizer with additional route options' do
assert_recognizes({:controller => 'passwords', :action => 'new', :locale => "en", :extra => 'value'}, '/en/organizers/password/new')
end
end