From ab51b285e2cccdc0cbdcd2daa04a7fd2fbb661ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 5 Feb 2014 23:55:17 -0200 Subject: Refatoring the method to avoid shot-circuit return --- activesupport/lib/active_support/core_ext/object/to_param.rb | 11 +++++++---- activesupport/test/core_ext/object/to_query_test.rb | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/object/to_param.rb b/activesupport/lib/active_support/core_ext/object/to_param.rb index e40846e7d6..13be0038c2 100644 --- a/activesupport/lib/active_support/core_ext/object/to_param.rb +++ b/activesupport/lib/active_support/core_ext/object/to_param.rb @@ -51,9 +51,12 @@ class Hash # # This method is also aliased as +to_query+. def to_param(namespace = nil) - return (namespace ? nil.to_query(namespace) : '') if empty? - collect do |key, value| - value.to_query(namespace ? "#{namespace}[#{key}]" : key) - end.sort! * '&' + if empty? + namespace ? nil.to_query(namespace) : '' + else + collect do |key, value| + value.to_query(namespace ? "#{namespace}[#{key}]" : key) + end.sort! * '&' + end end end diff --git a/activesupport/test/core_ext/object/to_query_test.rb b/activesupport/test/core_ext/object/to_query_test.rb index 51275e36bf..a892471e0f 100644 --- a/activesupport/test/core_ext/object/to_query_test.rb +++ b/activesupport/test/core_ext/object/to_query_test.rb @@ -47,6 +47,8 @@ class ToQueryTest < ActiveSupport::TestCase end def test_nested_empty_hash + assert_equal '', + {}.to_query assert_query_equal 'a=1&b%5Bc%5D=3&b%5Bd%5D=', { a: 1, b: { c: 3, d: {} } } assert_query_equal 'b%5Bc%5D=false&b%5Be%5D=&b%5Bf%5D=&p=12', -- cgit v1.2.3