mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Added support to config.default_url_options.
This commit is contained in:
parent
2c33d0e191
commit
05678e773f
8 changed files with 55 additions and 10 deletions
|
@ -7,7 +7,8 @@
|
||||||
* skip_before_filter added in Devise controllers
|
* skip_before_filter added in Devise controllers
|
||||||
* Use home_or_root_path on require_no_authentication as well
|
* Use home_or_root_path on require_no_authentication as well
|
||||||
* Added devise_controller?, useful to select or reject filters in ApplicationController
|
* 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
|
== 0.4.1
|
||||||
|
|
||||||
|
|
|
@ -30,4 +30,11 @@ Devise.setup do |config|
|
||||||
# end
|
# end
|
||||||
# manager.default_strategies.unshift :twitter_oauth
|
# manager.default_strategies.unshift :twitter_oauth
|
||||||
# end
|
# 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
|
end
|
||||||
|
|
|
@ -57,6 +57,11 @@ module Devise
|
||||||
@warden_config = block
|
@warden_config = block
|
||||||
end
|
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
|
# A method used internally to setup warden manager from the Rails initialize
|
||||||
# block.
|
# block.
|
||||||
def configure_warden_manager(manager) #:nodoc:
|
def configure_warden_manager(manager) #:nodoc:
|
||||||
|
|
|
@ -8,6 +8,12 @@ module Devise
|
||||||
base.class_eval do
|
base.class_eval do
|
||||||
helper_method :warden, :signed_in?, :devise_controller?,
|
helper_method :warden, :signed_in?, :devise_controller?,
|
||||||
*Devise.mappings.keys.map { |m| [:"current_#{m}", :"#{m}_signed_in?"] }.flatten
|
*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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,12 @@ module Devise
|
||||||
nil
|
nil
|
||||||
end
|
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)
|
options.assert_valid_keys(:class_name, :as, :path_names, :singular, :path_prefix)
|
||||||
|
|
||||||
@as = (options[:as] || name).to_sym
|
@as = (options[:as] || name).to_sym
|
||||||
|
@ -79,10 +84,12 @@ module Devise
|
||||||
# you should overwrite this method to use it. The only information supported
|
# you should overwrite this method to use it. The only information supported
|
||||||
# by default is I18n.locale.
|
# 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
|
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
|
end
|
||||||
|
|
||||||
# Create magic predicates for verifying what module is activated by this map.
|
# Create magic predicates for verifying what module is activated by this map.
|
||||||
|
|
|
@ -60,11 +60,16 @@ module ActionController::Routing
|
||||||
#
|
#
|
||||||
# map.devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
|
# 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"
|
# 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)
|
def devise_for(*resources)
|
||||||
options = resources.extract_options!
|
options = resources.extract_options!
|
||||||
|
|
||||||
|
@ -98,7 +103,7 @@ module ActionController::Routing
|
||||||
def confirmable(routes, mapping)
|
def confirmable(routes, mapping)
|
||||||
routes.resource :confirmation, :only => [:new, :create, :show], :as => mapping.path_names[:confirmation]
|
routes.resource :confirmation, :only => [:new, :create, :show], :as => mapping.path_names[:confirmation]
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -91,4 +91,13 @@ class ControllerAuthenticableTest < ActionController::TestCase
|
||||||
test 'is not a devise controller' do
|
test 'is not a devise controller' do
|
||||||
assert_not @controller.devise_controller?
|
assert_not @controller.devise_controller?
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -76,8 +76,13 @@ class MappingTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'parsed path is returned' do
|
test 'parsed path is returned' do
|
||||||
|
begin
|
||||||
|
Devise.default_url_options {{ :locale => I18n.locale }}
|
||||||
assert_equal '/account', Devise.mappings[:account].parsed_path
|
assert_equal '/account', Devise.mappings[:account].parsed_path
|
||||||
assert_equal '/en/organizers', Devise.mappings[:manager].parsed_path
|
assert_equal '/en/organizers', Devise.mappings[:manager].parsed_path
|
||||||
|
ensure
|
||||||
|
Devise.default_url_options {{ }}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'magic predicates' do
|
test 'magic predicates' do
|
||||||
|
|
Loading…
Reference in a new issue