Refatoring the method to avoid shot-circuit return

上级 d6e3fd77
......@@ -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
......@@ -47,6 +47,8 @@ def test_array_values_are_not_sorted
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',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册