diff --git a/lib/devise/mapping.rb b/lib/devise/mapping.rb index cf8fd468..af5499c5 100644 --- a/lib/devise/mapping.rb +++ b/lib/devise/mapping.rb @@ -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 diff --git a/lib/devise/rails/routes.rb b/lib/devise/rails/routes.rb index 969b6616..688291f8 100644 --- a/lib/devise/rails/routes.rb +++ b/lib/devise/rails/routes.rb @@ -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 diff --git a/test/mapping_test.rb b/test/mapping_test.rb index 45cebea9..848caf85 100644 --- a/test/mapping_test.rb +++ b/test/mapping_test.rb @@ -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? diff --git a/test/rails_app/config/routes.rb b/test/rails_app/config/routes.rb index 00d75f28..7ff39fa6 100644 --- a/test/rails_app/config/routes.rb +++ b/test/rails_app/config/routes.rb @@ -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 diff --git a/test/routes_test.rb b/test/routes_test.rb index 0b1ce6cc..accf8b8a 100644 --- a/test/routes_test.rb +++ b/test/routes_test.rb @@ -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