Deprecate string options in URL helpers

Fixes https://github.com/rails/rails/issues/16958

[Byron Bischoff & Melanie Gilman]
This commit is contained in:
Melanie Gilman 2014-11-24 10:06:16 -05:00
parent 77fbc53586
commit 4d84922840
3 changed files with 43 additions and 1 deletions

@ -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. * Deprecate the `only_path` option on `*_path` helpers.
In cases where this option is set to `true`, the option is redundant and can In cases where this option is set to `true`, the option is redundant and can

@ -271,7 +271,7 @@ def call(t, args, inner_options)
controller_options = t.url_options controller_options = t.url_options
options = controller_options.merge @options options = controller_options.merge @options
hash = handle_positional_args(controller_options, hash = handle_positional_args(controller_options,
inner_options || {}, deprecate_string_options(inner_options) || {},
args, args,
options, options,
@segment_keys) @segment_keys)
@ -293,6 +293,22 @@ def handle_positional_args(controller_options, inner_options, args, result, path
result.merge!(inner_options) result.merge!(inner_options)
end 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 end
private private

@ -160,6 +160,26 @@ def call(env)
assert_equal '/foo/1/bar/2', url_helpers.foo_bar_path(2, foo_id: 1) assert_equal '/foo/1/bar/2', url_helpers.foo_bar_path(2, foo_id: 1)
end 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 private
def draw(&block) def draw(&block)
@set.draw(&block) @set.draw(&block)