Skip to content

Commit

Permalink
Make optimized named routes respect all reserved options and tie it i…
Browse files Browse the repository at this point in the history
…nto UrlRewriter::RESERVED_OPTIONS so it's DRY

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information
gtd authored and NZKoz committed Nov 18, 2008
1 parent afc7fce commit af57ccb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
8 changes: 2 additions & 6 deletions actionpack/lib/action_controller/routing/optimisations.rb
Expand Up @@ -106,12 +106,8 @@ def generation_code
# argument
class PositionalArgumentsWithAdditionalParams < PositionalArguments
def guard_conditions
[
"args.size == #{route.segment_keys.size + 1}",
"!args.last.has_key?(:anchor)",
"!args.last.has_key?(:port)",
"!args.last.has_key?(:host)"
]
["args.size == #{route.segment_keys.size + 1}"] +
UrlRewriter::RESERVED_OPTIONS.collect{ |key| "!args.last.has_key?(:#{key})" }
end

# This case uses almost the same code as positional arguments,
Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_controller/routing/route_set.rb
Expand Up @@ -168,6 +168,7 @@ def define_url_helper(route, name, kind, options)
#
@module.module_eval <<-end_eval # We use module_eval to avoid leaks
def #{selector}(*args)
#{generate_optimisation_block(route, kind)}
opts = if args.empty? || Hash === args.first
Expand Down
12 changes: 9 additions & 3 deletions actionpack/test/controller/routing_test.rb
Expand Up @@ -706,12 +706,13 @@ def url_for(options)
port = options.delete(:port) || 80
port_string = port == 80 ? '' : ":#{port}"

host = options.delete(:host) || "named.route.test"
anchor = "##{options.delete(:anchor)}" if options.key?(:anchor)
protocol = options.delete(:protocol) || "http"
host = options.delete(:host) || "named.route.test"
anchor = "##{options.delete(:anchor)}" if options.key?(:anchor)

path = routes.generate(options)

only_path ? "#{path}#{anchor}" : "http://#{host}#{port_string}#{path}#{anchor}"
only_path ? "#{path}#{anchor}" : "#{protocol}://#{host}#{port_string}#{path}#{anchor}"
end

def request
Expand Down Expand Up @@ -1726,6 +1727,11 @@ def test_named_route_url_method_with_host
assert_equal "http://some.example.com/people/5", controller.send(:show_url, 5, :host=>"some.example.com")
end

def test_named_route_url_method_with_protocol
controller = setup_named_route_test
assert_equal "https://named.route.test/people/5", controller.send(:show_url, 5, :protocol => "https")
end

def test_named_route_url_method_with_ordered_parameters
controller = setup_named_route_test
assert_equal "http://named.route.test/people/go/7/hello/joe/5",
Expand Down

0 comments on commit af57ccb

Please sign in to comment.