From 3bb863d63198080e26afbdc72e483315c8bc1be8 Mon Sep 17 00:00:00 2001 From: Cheah Chu Yeow Date: Sun, 20 Apr 2008 12:57:36 +0800 Subject: [PATCH] Ensure that default_url_options, if defined, are used in named routes. --- actionpack/lib/action_controller/base.rb | 4 +- .../lib/action_controller/routing/optimisations.rb | 6 ++-- actionpack/test/controller/base_test.rb | 34 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index c0f3122..664fdf8 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -534,9 +534,9 @@ module ActionController #:nodoc: # Returns a URL that has been rewritten according to the options hash and the defined Routes. # (For doing a complete redirect, use redirect_to). - #   + # # url_for is used to: - #   + # # All keys given to url_for are forwarded to the Route module, save for the following: # * :anchor -- specifies the anchor name to be appended to the path. For example, # url_for :controller => 'posts', :action => 'show', :id => 10, :anchor => 'comments' diff --git a/actionpack/lib/action_controller/routing/optimisations.rb b/actionpack/lib/action_controller/routing/optimisations.rb index ba4aeb4..942c8d5 100644 --- a/actionpack/lib/action_controller/routing/optimisations.rb +++ b/actionpack/lib/action_controller/routing/optimisations.rb @@ -60,9 +60,9 @@ module ActionController # if they're using foo_url(:id=>2) it's one # argument, but we don't want to generate /foos/id2 if number_of_arguments == 1 - "defined?(request) && request && args.size == 1 && !args.first.is_a?(Hash)" + "(!defined?(default_url_options) || default_url_options(nil).blank?) && defined?(request) && request && args.size == 1 && !args.first.is_a?(Hash)" else - "defined?(request) && request && args.size == #{number_of_arguments}" + "(!defined?(default_url_options) || default_url_options(nil).blank?) && defined?(request) && request && args.size == #{number_of_arguments}" end end @@ -97,7 +97,7 @@ module ActionController # argument class PositionalArgumentsWithAdditionalParams < PositionalArguments def guard_condition - "defined?(request) && request && args.size == #{route.segment_keys.size + 1} && !args.last.has_key?(:anchor) && !args.last.has_key?(:port) && !args.last.has_key?(:host)" + "(!defined?(default_url_options) || default_url_options(nil).blank?) && defined?(request) && request && args.size == #{route.segment_keys.size + 1} && !args.last.has_key?(:anchor) && !args.last.has_key?(:port) && !args.last.has_key?(:host)" end # This case uses almost the same code as positional arguments, diff --git a/actionpack/test/controller/base_test.rb b/actionpack/test/controller/base_test.rb index 1a8bd6d..8416754 100644 --- a/actionpack/test/controller/base_test.rb +++ b/actionpack/test/controller/base_test.rb @@ -48,6 +48,15 @@ protected end +class DefaultUrlOptionsController < ActionController::Base + def default_url_options_action + end + + def default_url_options(options) + { :host => 'www.override.com', :action => 'new', :bacon => 'chunky' } + end +end + class ControllerClassTests < Test::Unit::TestCase def test_controller_path assert_equal 'empty', EmptyController.controller_path @@ -134,3 +143,28 @@ class PerformActionTest < Test::Unit::TestCase assert_response 404 end end + +class DefaultUrlOptionsTest < Test::Unit::TestCase + def setup + @controller = DefaultUrlOptionsController.new + + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @request.host = 'www.example.com' + end + + def test_default_url_options_are_used_if_set + ActionController::Routing::Routes.draw do |map| + map.default_url_options 'default_url_options', :controller => 'default_url_options' + map.connect ':controller/:action/:id' + end + + get :default_url_options_action # Make a dummy request so that the controller is initialized properly. + + assert_equal 'http://www.override.com/default_url_options/new?bacon=chunky', @controller.url_for(:controller => 'default_url_options') + assert_equal 'http://www.override.com/default_url_options?bacon=chunky', @controller.send(:default_url_options_url) + ensure + ActionController::Routing::Routes.load! + end +end \ No newline at end of file -- 1.5.5.1