aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMelanie Gilman <melanie@thoughtbot.com>2014-11-24 10:06:16 -0500
committerMelanie Gilman <melanie@thoughtbot.com>2014-11-24 21:16:33 -0500
commit4d84922840deb89754f5b5fb61ebea0aeadc52df (patch)
treed069733f7270c7e1f58432bb1d6661ab888cf55e
parent77fbc5358616c131884df4e0b622241ebf2a129b (diff)
downloadrails-4d84922840deb89754f5b5fb61ebea0aeadc52df.tar.gz
rails-4d84922840deb89754f5b5fb61ebea0aeadc52df.tar.bz2
rails-4d84922840deb89754f5b5fb61ebea0aeadc52df.zip
Deprecate string options in URL helpers
Fixes https://github.com/rails/rails/issues/16958 [Byron Bischoff & Melanie Gilman]
-rw-r--r--actionpack/CHANGELOG.md6
-rw-r--r--actionpack/lib/action_dispatch/routing/route_set.rb18
-rw-r--r--actionpack/test/dispatch/routing/route_set_test.rb20
3 files changed, 43 insertions, 1 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md
index 158b22c0cc..7bca373920 100644
--- a/actionpack/CHANGELOG.md
+++ b/actionpack/CHANGELOG.md
@@ -1,3 +1,9 @@
+* Deprecate use of string keys in URL helpers.
+
+ Use symbols instead. Fixes #16958.
+
+ *Byron Bischoff & Melanie Gilman*
+
* Deprecate the `only_path` option on `*_path` helpers.
In cases where this option is set to `true`, the option is redundant and can
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb
index a641ea3ea9..a8ef5531d5 100644
--- a/actionpack/lib/action_dispatch/routing/route_set.rb
+++ b/actionpack/lib/action_dispatch/routing/route_set.rb
@@ -271,7 +271,7 @@ module ActionDispatch
controller_options = t.url_options
options = controller_options.merge @options
hash = handle_positional_args(controller_options,
- inner_options || {},
+ deprecate_string_options(inner_options) || {},
args,
options,
@segment_keys)
@@ -293,6 +293,22 @@ module ActionDispatch
result.merge!(inner_options)
end
+
+ DEPRECATED_STRING_OPTIONS = %w[controller action]
+
+ def deprecate_string_options(options)
+ options ||= {}
+ deprecated_string_options = options.keys & DEPRECATED_STRING_OPTIONS
+ if deprecated_string_options.any?
+ msg = "Calling URL helpers with string keys #{deprecated_string_options.join(", ")} is deprecated. Use symbols instead."
+ ActiveSupport::Deprecation.warn(msg)
+ deprecated_string_options.each do |option|
+ value = options.delete(option)
+ options[option.to_sym] = value
+ end
+ end
+ options
+ end
end
private
diff --git a/actionpack/test/dispatch/routing/route_set_test.rb b/actionpack/test/dispatch/routing/route_set_test.rb
index a7acc0de41..5a39119446 100644
--- a/actionpack/test/dispatch/routing/route_set_test.rb
+++ b/actionpack/test/dispatch/routing/route_set_test.rb
@@ -160,6 +160,26 @@ module ActionDispatch
assert_equal '/foo/1/bar/2', url_helpers.foo_bar_path(2, foo_id: 1)
end
+ test "stringified controller and action keys are properly symbolized" do
+ draw do
+ root 'foo#bar'
+ end
+
+ assert_deprecated do
+ assert_equal '/', url_helpers.root_path('controller' => 'foo', 'action' => 'bar')
+ end
+ end
+
+ test "mix of string and symbol keys are properly symbolized" do
+ draw do
+ root 'foo#bar'
+ end
+
+ assert_deprecated do
+ assert_equal '/', url_helpers.root_path('controller' => 'foo', :action => 'bar')
+ end
+ end
+
private
def draw(&block)
@set.draw(&block)