Ensure that when UrlWriter is included in multiple classes, the default_url_options of one don't affect the other. [#1277 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
Nathan de Vries 2009-01-28 19:31:48 +00:00 committed by Pratik Naik
parent 74871961ec
commit 32eeb3e521
2 changed files with 19 additions and 7 deletions

@ -92,15 +92,12 @@ module ActionController
# end
# end
module UrlWriter
# The default options for urls written by this writer. Typically a <tt>:host</tt>
# pair is provided.
mattr_accessor :default_url_options
self.default_url_options = {}
def self.included(base) #:nodoc:
ActionController::Routing::Routes.install_helpers(base)
base.mattr_accessor :default_url_options
base.default_url_options ||= default_url_options
# The default options for urls written by this writer. Typically a <tt>:host</tt> pair is provided.
base.default_url_options ||= {}
end
# Generate a url based on the options provided, default_url_options and the

@ -303,7 +303,6 @@ def test_path_generation_for_symbol_parameter_keys
def test_named_routes_with_nil_keys
ActionController::Routing::Routes.clear!
add_host!
ActionController::Routing::Routes.draw do |map|
map.main '', :controller => 'posts'
map.resources :posts
@ -311,6 +310,8 @@ def test_named_routes_with_nil_keys
end
# We need to create a new class in order to install the new named route.
kls = Class.new { include ActionController::UrlWriter }
kls.default_url_options[:host] = 'www.basecamphq.com'
controller = kls.new
params = {:action => :index, :controller => :posts, :format => :xml}
assert_equal("http://www.basecamphq.com/posts.xml", controller.send(:url_for, params))
@ -337,6 +338,20 @@ def test_formatted_url_methods_are_deprecated
ensure
ActionController::Routing::Routes.load!
end
def test_multiple_includes_maintain_distinct_options
first_class = Class.new { include ActionController::UrlWriter }
second_class = Class.new { include ActionController::UrlWriter }
first_host, second_host = 'firsthost.com', 'secondhost.com'
first_class.default_url_options[:host] = first_host
second_class.default_url_options[:host] = second_host
assert_equal first_class.default_url_options[:host], first_host
assert_equal second_class.default_url_options[:host], second_host
end
private
def extract_params(url)
url.split('?', 2).last.split('&')