Skip to content
This repository
Browse code

Ensure that default_url_options, if defined, are used in named routes.

Signed-off-by: Michael Koziarski <michael@koziarski.com>

[#22 state:resolved]
  • Loading branch information...
commit 6a6b4392c16c665eb713705f2b38e959a658eeef 1 parent 437f918
Cheah Chu Yeow authored April 20, 2008 NZKoz committed May 04, 2008
4  actionpack/lib/action_controller/base.rb
@@ -532,9 +532,9 @@ def process(request, response, method = :perform_action, *arguments) #:nodoc:
532 532
 
533 533
       # Returns a URL that has been rewritten according to the options hash and the defined Routes.
534 534
       # (For doing a complete redirect, use redirect_to).
535  
-      #  
  535
+      #
536 536
       # <tt>url_for</tt> is used to:
537  
-      #  
  537
+      #
538 538
       # All keys given to url_for are forwarded to the Route module, save for the following:
539 539
       # * <tt>:anchor</tt> -- specifies the anchor name to be appended to the path. For example,
540 540
       #   <tt>url_for :controller => 'posts', :action => 'show', :id => 10, :anchor => 'comments'</tt>
6  actionpack/lib/action_controller/routing/optimisations.rb
@@ -61,9 +61,9 @@ def guard_condition
61 61
           # if they're using foo_url(:id=>2) it's one 
62 62
           # argument, but we don't want to generate /foos/id2
63 63
           if number_of_arguments == 1
64  
-            "defined?(request) && request && args.size == 1 && !args.first.is_a?(Hash)"
  64
+            "(!defined?(default_url_options) || default_url_options(nil).blank?) && defined?(request) && request && args.size == 1 && !args.first.is_a?(Hash)"
65 65
           else
66  
-            "defined?(request) && request && args.size == #{number_of_arguments}"
  66
+            "(!defined?(default_url_options) || default_url_options(nil).blank?) && defined?(request) && request && args.size == #{number_of_arguments}"
67 67
           end
68 68
         end
69 69
 
@@ -98,7 +98,7 @@ def generation_code
98 98
       # argument
99 99
       class PositionalArgumentsWithAdditionalParams < PositionalArguments
100 100
         def guard_condition
101  
-          "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)"
  101
+          "(!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)"
102 102
         end
103 103
 
104 104
         # This case uses almost the same code as positional arguments, 
34  actionpack/test/controller/base_test.rb
@@ -48,6 +48,15 @@ def method_missing(selector)
48 48
   
49 49
 end
50 50
 
  51
+class DefaultUrlOptionsController < ActionController::Base
  52
+  def default_url_options_action
  53
+  end
  54
+
  55
+  def default_url_options(options)
  56
+    { :host => 'www.override.com', :action => 'new', :bacon => 'chunky' }
  57
+  end
  58
+end
  59
+
51 60
 class ControllerClassTests < Test::Unit::TestCase
52 61
   def test_controller_path
53 62
     assert_equal 'empty', EmptyController.controller_path
@@ -134,3 +143,28 @@ def test_get_on_hidden_should_fail
134 143
     assert_response 404
135 144
   end
136 145
 end
  146
+
  147
+class DefaultUrlOptionsTest < Test::Unit::TestCase
  148
+  def setup
  149
+    @controller = DefaultUrlOptionsController.new
  150
+
  151
+    @request    = ActionController::TestRequest.new
  152
+    @response   = ActionController::TestResponse.new
  153
+
  154
+    @request.host = 'www.example.com'
  155
+  end
  156
+
  157
+  def test_default_url_options_are_used_if_set
  158
+    ActionController::Routing::Routes.draw do |map|
  159
+      map.default_url_options 'default_url_options', :controller => 'default_url_options'
  160
+      map.connect ':controller/:action/:id'
  161
+    end
  162
+
  163
+    get :default_url_options_action # Make a dummy request so that the controller is initialized properly.
  164
+
  165
+    assert_equal 'http://www.override.com/default_url_options/new?bacon=chunky', @controller.url_for(:controller => 'default_url_options')
  166
+    assert_equal 'http://www.override.com/default_url_options?bacon=chunky', @controller.send(:default_url_options_url)
  167
+  ensure
  168
+    ActionController::Routing::Routes.load!
  169
+  end
  170
+end

0 notes on commit 6a6b439

Please sign in to comment.
Something went wrong with that request. Please try again.