Added support to config.default_url_options.

This commit is contained in:
José Valim 2009-11-06 20:59:36 -02:00
parent 2c33d0e191
commit 05678e773f
8 changed files with 55 additions and 10 deletions

View File

@ -7,7 +7,8 @@
* skip_before_filter added in Devise controllers
* Use home_or_root_path on require_no_authentication as well
* Added devise_controller?, useful to select or reject filters in ApplicationController
* Allow :path_prefix to be given to devise_for (:path_prefix => "/:locale" is supported)
* Allow :path_prefix to be given to devise_for
* Allow default_url_options to be configured through devise (:path_prefix => "/:locale" is now supported)
== 0.4.1

View File

@ -30,4 +30,11 @@ Devise.setup do |config|
# end
# manager.default_strategies.unshift :twitter_oauth
# end
# Configure default_url_options if you are using dynamic segments in :path_prefix
# for devise_for.
#
# config.default_url_options do
# { :locale => I18n.locale }
# end
end

View File

@ -57,6 +57,11 @@ module Devise
@warden_config = block
end
# Configure default url options to be used within Devise and ActionController.
def default_url_options(&block)
Devise::Mapping.metaclass.send :define_method, :default_url_options, &block
end
# A method used internally to setup warden manager from the Rails initialize
# block.
def configure_warden_manager(manager) #:nodoc:

View File

@ -8,6 +8,12 @@ module Devise
base.class_eval do
helper_method :warden, :signed_in?, :devise_controller?,
*Devise.mappings.keys.map { |m| [:"current_#{m}", :"#{m}_signed_in?"] }.flatten
# Use devise default_url_options. We have to declare it here to overwrite
# default definitions.
def default_url_options(options=nil)
Devise::Mapping.default_url_options
end
end
end

View File

@ -34,7 +34,12 @@ module Devise
nil
end
def initialize(name, options)
# Default url options which can be used as prefix.
def self.default_url_options
{}
end
def initialize(name, options) #:nodoc:
options.assert_valid_keys(:class_name, :as, :path_names, :singular, :path_prefix)
@as = (options[:as] || name).to_sym
@ -79,10 +84,12 @@ module Devise
# you should overwrite this method to use it. The only information supported
# by default is I18n.locale.
#
# TODO This is a hack. Setting default_url_options that are shared by
# controllers and devise seems to be the best solution.
def parsed_path
raw_path.gsub(":locale", I18n.locale.to_s)
returning raw_path do |path|
self.class.default_url_options.each do |key, value|
path.gsub!(key.inspect, value.to_s)
end
end
end
# Create magic predicates for verifying what module is activated by this map.

View File

@ -60,11 +60,16 @@ module ActionController::Routing
#
# map.devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
#
# * :path_prefix => the path prefix to be used in all routes. Only :locale is supported as dynamic prefix:
# * :path_prefix => the path prefix to be used in all routes.
#
# map.devise_for :users, :path_prefix => "/:locale"
#
# When setting a dynamic path prefix, be sure to set default_url_options with the locale on your ApplicationController as well.
# 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
# { :locale => I18n.locale }
# end
#
def devise_for(*resources)
options = resources.extract_options!
@ -98,7 +103,7 @@ module ActionController::Routing
def confirmable(routes, mapping)
routes.resource :confirmation, :only => [:new, :create, :show], :as => mapping.path_names[:confirmation]
end
end
end
end
end

View File

@ -91,4 +91,13 @@ class ControllerAuthenticableTest < ActionController::TestCase
test 'is not a devise controller' do
assert_not @controller.devise_controller?
end
test 'default url options are retrieved from devise' do
begin
Devise.default_url_options {{ :locale => I18n.locale }}
assert_equal({ :locale => :en }, @controller.send(:default_url_options))
ensure
Devise.default_url_options {{ }}
end
end
end

View File

@ -76,8 +76,13 @@ class MappingTest < ActiveSupport::TestCase
end
test 'parsed path is returned' do
assert_equal '/account', Devise.mappings[:account].parsed_path
assert_equal '/en/organizers', Devise.mappings[:manager].parsed_path
begin
Devise.default_url_options {{ :locale => I18n.locale }}
assert_equal '/account', Devise.mappings[:account].parsed_path
assert_equal '/en/organizers', Devise.mappings[:manager].parsed_path
ensure
Devise.default_url_options {{ }}
end
end
test 'magic predicates' do