Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
chuyeow authored and NZKoz committed May 4, 2008
1 parent 437f918 commit 6a6b439
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
4 changes: 2 additions & 2 deletions actionpack/lib/action_controller/base.rb
Expand Up @@ -532,9 +532,9 @@ def process(request, response, method = :perform_action, *arguments) #: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).
#  
#
# <tt>url_for</tt> is used to:
#  
#
# All keys given to url_for are forwarded to the Route module, save for the following:
# * <tt>:anchor</tt> -- specifies the anchor name to be appended to the path. For example,
# <tt>url_for :controller => 'posts', :action => 'show', :id => 10, :anchor => 'comments'</tt>
Expand Down
6 changes: 3 additions & 3 deletions actionpack/lib/action_controller/routing/optimisations.rb
Expand Up @@ -61,9 +61,9 @@ def guard_condition
# 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

Expand Down Expand Up @@ -98,7 +98,7 @@ def generation_code
# 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,
Expand Down
34 changes: 34 additions & 0 deletions actionpack/test/controller/base_test.rb
Expand Up @@ -48,6 +48,15 @@ def method_missing(selector)

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
Expand Down Expand Up @@ -134,3 +143,28 @@ def test_get_on_hidden_should_fail
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

0 comments on commit 6a6b439

Please sign in to comment.