diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-12-28 20:49:08 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-28 20:49:08 -0500 |
commit | 09cdf425d70cfec541e0008c28055155d70c6f37 (patch) | |
tree | 3e2fb4694230c63bc5cbbbb9ead82c25e1466249 | |
parent | 47cda2e1f655d38a204c4df88d780500c1e99316 (diff) | |
parent | f1525dac115cea40ec41b4f9e267e011be8da22e (diff) | |
download | rails-09cdf425d70cfec541e0008c28055155d70c6f37.tar.gz rails-09cdf425d70cfec541e0008c28055155d70c6f37.tar.bz2 rails-09cdf425d70cfec541e0008c28055155d70c6f37.zip |
Merge pull request #27486 from schleyfox/optimize-journey-route-score-url_for
Optimize Journey::Route#score for url_for
-rw-r--r-- | actionpack/lib/action_dispatch/journey/formatter.rb | 6 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/journey/route.rb | 13 | ||||
-rw-r--r-- | actionpack/test/journey/route_test.rb | 2 |
3 files changed, 15 insertions, 6 deletions
diff --git a/actionpack/lib/action_dispatch/journey/formatter.rb b/actionpack/lib/action_dispatch/journey/formatter.rb index 20ff4441a0..1d239addf8 100644 --- a/actionpack/lib/action_dispatch/journey/formatter.rb +++ b/actionpack/lib/action_dispatch/journey/formatter.rb @@ -92,7 +92,11 @@ module ActionDispatch else routes = non_recursive(cache, options) - hash = routes.group_by { |_, r| r.score(options) } + supplied_keys = options.each_with_object({}) do |(k, v), h| + h[k.to_s] = true if v + end + + hash = routes.group_by { |_, r| r.score(supplied_keys) } hash.keys.sort.reverse_each do |score| break if score < 0 diff --git a/actionpack/lib/action_dispatch/journey/route.rb b/actionpack/lib/action_dispatch/journey/route.rb index 0cc8d83ac8..f2ac4818d8 100644 --- a/actionpack/lib/action_dispatch/journey/route.rb +++ b/actionpack/lib/action_dispatch/journey/route.rb @@ -96,13 +96,18 @@ module ActionDispatch required_parts + required_defaults.keys end - def score(constraints) + def score(supplied_keys) required_keys = path.required_names - supplied_keys = constraints.map { |k, v| v && k.to_s }.compact - return -1 unless (required_keys - supplied_keys).empty? + required_keys.each do |k| + return -1 unless supplied_keys.include?(k) + end + + score = 0 + path.names.each do |k| + score += 1 if supplied_keys.include?(k) + end - score = (supplied_keys & path.names).length score + (required_defaults.length * 2) end diff --git a/actionpack/test/journey/route_test.rb b/actionpack/test/journey/route_test.rb index d2a8163ffb..8fd73970b8 100644 --- a/actionpack/test/journey/route_test.rb +++ b/actionpack/test/journey/route_test.rb @@ -96,7 +96,7 @@ module ActionDispatch path = Path::Pattern.from_string "/:controller(/:action(/:id))(.:format)" generic = Route.build "name", nil, path, constraints, [], {} - knowledge = { id: 20, controller: "pages", action: "show" } + knowledge = { "id" => true, "controller" => true, "action" => true } routes = [specific, generic] |