Skip to content

Commit

Permalink
Axe "best fit" generation support
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Aug 15, 2009
1 parent df6617b commit 911acc1
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 39 deletions.
29 changes: 2 additions & 27 deletions actionpack/lib/action_controller/routing/route_set.rb
Expand Up @@ -407,22 +407,9 @@ def generate(options, recall = {}, method=:generate)
# don't use the recalled keys when determining which routes to check
routes = routes_by_controller[controller][action][options.reject {|k,v| !v}.keys.sort_by { |x| x.object_id }]

routes[1].each_with_index do |route, index|
routes.each_with_index do |route, index|
results = route.__send__(method, options, merged, expire_on)
if results && (!results.is_a?(Array) || results.first)

# Compare results with Rails 3.0 behavior
if routes[0][index] != route
routes[0].each do |route2|
new_results = route2.__send__(method, options, merged, expire_on)
if new_results && (!new_results.is_a?(Array) || new_results.first)
ActiveSupport::Deprecation.warn "The URL you generated will use the first matching route in routes.rb rather than the \"best\" match. " +
"In Rails 3.0 #{new_results} would of been generated instead of #{results}"
break
end
end
end

return results
end
end
Expand Down Expand Up @@ -463,10 +450,7 @@ def routes_by_controller
@routes_by_controller ||= Hash.new do |controller_hash, controller|
controller_hash[controller] = Hash.new do |action_hash, action|
action_hash[action] = Hash.new do |key_hash, keys|
key_hash[keys] = [
routes_for_controller_and_action_and_keys(controller, action, keys),
deprecated_routes_for_controller_and_action_and_keys(controller, action, keys)
]
key_hash[keys] = routes_for_controller_and_action_and_keys(controller, action, keys)
end
end
end
Expand All @@ -487,15 +471,6 @@ def routes_for_controller_and_action_and_keys(controller, action, keys)
end
end

def deprecated_routes_for_controller_and_action_and_keys(controller, action, keys)
selected = routes.select do |route|
route.matches_controller_and_action? controller, action
end
selected.sort_by do |route|
(keys - route.significant_keys).length
end
end

# Subclasses and plugins may override this method to extract further attributes
# from the request, for use by route conditions and such.
def extract_request_environment(request)
Expand Down
12 changes: 0 additions & 12 deletions actionpack/test/controller/routing_test.rb
Expand Up @@ -1411,18 +1411,6 @@ def test_namespace_with_blank_path_prefix
Object.send(:remove_const, :Api)
end

def test_generate_finds_best_fit
set.draw do |map|
map.connect "/people", :controller => "people", :action => "index"
map.connect "/ws/people", :controller => "people", :action => "index", :ws => true
end

assert_deprecated {
url = set.generate(:controller => "people", :action => "index", :ws => true)
assert_equal "/ws/people", url
}
end

def test_generate_changes_controller_module
set.draw { |map| map.connect ':controller/:action/:id' }
current = { :controller => "bling/bloop", :action => "bap", :id => 9 }
Expand Down

0 comments on commit 911acc1

Please sign in to comment.