Permalink
Browse files

Added support to config.default_url_options.

  • Loading branch information...
1 parent 2c33d0e commit 05678e773fbeca9a0b251e1aad72a573d8ac5a75 @josevalim josevalim committed Nov 6, 2009
View
@@ -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
@@ -30,4 +30,11 @@
# 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
@@ -57,6 +57,11 @@ def warden(&block)
@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:
@@ -8,6 +8,12 @@ def self.included(base)
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
@@ -34,7 +34,12 @@ def self.find_by_path(path)
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 @@ def raw_path
# 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
@@ -60,11 +60,16 @@ class Mapper #:doc:
#
# 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 @@ def recoverable(routes, mapping)
def confirmable(routes, mapping)
routes.resource :confirmation, :only => [:new, :create, :show], :as => mapping.path_names[:confirmation]
end
- end
+ end
end
end
@@ -91,4 +91,13 @@ def setup
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
@@ -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

0 comments on commit 05678e7

Please sign in to comment.