From c23bb156fe29481c3c900e9a4bd3c1f84e71c6d0 Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Sun, 23 Nov 2014 01:18:33 -0800 Subject: Deprecate passing an invalid name to `Formatter#generate` The internal tests that (incorrectly) relied on this were already fixed in 938d130. However, we cannot simply fix this bug because the guides prior to b7b9e92 recommended a workaround that relies on this buggy behavior. Reference #17453 --- actionpack/lib/action_dispatch/journey/formatter.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'actionpack/lib/action_dispatch/journey/formatter.rb') diff --git a/actionpack/lib/action_dispatch/journey/formatter.rb b/actionpack/lib/action_dispatch/journey/formatter.rb index 992c1a9efe..62bd4a33f2 100644 --- a/actionpack/lib/action_dispatch/journey/formatter.rb +++ b/actionpack/lib/action_dispatch/journey/formatter.rb @@ -1,4 +1,5 @@ require 'action_controller/metal/exceptions' +require 'active_support/deprecation' module ActionDispatch module Journey @@ -80,6 +81,15 @@ module ActionDispatch if named_routes.key?(name) yield named_routes[name] else + if name + ActiveSupport::Deprecation.warn <<-MSG.squish + You are trying to generate the URL for a named route called + #{name.inspect} but no such route was found. In the future, + this will result in an `ActionController::UrlGenerationError` + exception. + MSG + end + routes = non_recursive(cache, options) hash = routes.group_by { |_, r| r.score(options) } -- cgit v1.2.3 From 23b21f6182e36c05c6b2a240c0fb824ae828465e Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Sun, 23 Nov 2014 01:46:51 -0800 Subject: Don't show the warning if we're already raising the error anyway If the route set is empty, or if none of the routes matches with a score > 0, there is no point showing the deprecation message because we are already be raising the `ActionController::UrlGenerationError` mentioned in the warning. In this case it is the expected behavior and the user wouldn't have to take any actions. --- actionpack/lib/action_dispatch/journey/formatter.rb | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'actionpack/lib/action_dispatch/journey/formatter.rb') diff --git a/actionpack/lib/action_dispatch/journey/formatter.rb b/actionpack/lib/action_dispatch/journey/formatter.rb index 62bd4a33f2..177f586c0e 100644 --- a/actionpack/lib/action_dispatch/journey/formatter.rb +++ b/actionpack/lib/action_dispatch/journey/formatter.rb @@ -81,14 +81,8 @@ module ActionDispatch if named_routes.key?(name) yield named_routes[name] else - if name - ActiveSupport::Deprecation.warn <<-MSG.squish - You are trying to generate the URL for a named route called - #{name.inspect} but no such route was found. In the future, - this will result in an `ActionController::UrlGenerationError` - exception. - MSG - end + # Make sure we don't show the deprecation warning more than once + warned = false routes = non_recursive(cache, options) @@ -98,6 +92,17 @@ module ActionDispatch break if score < 0 hash[score].sort_by { |i, _| i }.each do |_, route| + if name && !warned + ActiveSupport::Deprecation.warn <<-MSG.squish + You are trying to generate the URL for a named route called + #{name.inspect} but no such route was found. In the future, + this will result in an `ActionController::UrlGenerationError` + exception. + MSG + + warned = true + end + yield route end end -- cgit v1.2.3