Skip to content

Commit

Permalink
Added support to config.default_url_options.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Nov 6, 2009
1 parent 2c33d0e commit 05678e7
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions generators/devise_install/templates/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions lib/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions lib/devise/controllers/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 11 additions & 4 deletions lib/devise/mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 8 additions & 3 deletions lib/devise/rails/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions test/controllers/filters_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 7 additions & 2 deletions test/mapping_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 05678e7

Please sign in to comment.