Skip to content

Commit

Permalink
Fix to_query with empty arrays too
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Feb 6, 2014
1 parent 7aa4b7d commit c82dbc6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion activesupport/lib/active_support/core_ext/object/to_query.rb
Expand Up @@ -18,7 +18,12 @@ class Array
# ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding" # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
def to_query(key) def to_query(key)
prefix = "#{key}[]" prefix = "#{key}[]"
collect { |value| value.to_query(prefix) }.join '&'
if empty?
nil.to_query(prefix)
else
collect { |value| value.to_query(prefix) }.join '&'
end
end end
end end


Expand Down
2 changes: 2 additions & 0 deletions activesupport/test/core_ext/object/to_query_test.rb
Expand Up @@ -55,6 +55,8 @@ def test_nested_empty_hash
{ p: 12, b: { c: false, e: nil, f: '' } } { p: 12, b: { c: false, e: nil, f: '' } }
assert_query_equal 'b%5Bc%5D=3&b%5Bf%5D=&b%5Bk%5D=', assert_query_equal 'b%5Bc%5D=3&b%5Bf%5D=&b%5Bk%5D=',
{ b: { c: 3, k: {}, f: '' } } { b: { c: 3, k: {}, f: '' } }
assert_query_equal 'a%5B%5D=&b=3',
{a: [], b: 3}
end end


private private
Expand Down

0 comments on commit c82dbc6

Please sign in to comment.