Skip to content

Commit

Permalink
Merge pull request #96 from jramos/issue_86
Browse files Browse the repository at this point in the history
Fixing Issue 86
  • Loading branch information
rafaelfranca committed Nov 30, 2015
2 parents 439049b + 91a59a3 commit f3115bc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_resource/formats.rb
Expand Up @@ -12,7 +12,7 @@ def self.[](mime_type_reference)
end

def self.remove_root(data)
if data.is_a?(Hash) && data.keys.size == 1
if data.is_a?(Hash) && data.keys.size == 1 && data.values.first.is_a?(Enumerable)
data.values.first
else
data
Expand Down
17 changes: 17 additions & 0 deletions test/cases/format_test.rb
Expand Up @@ -106,6 +106,23 @@ def test_serialization_of_nested_resource
end
end

def test_removing_root
matz = { :name => 'Matz' }
matz_with_root = { :person => matz }

# On Array
people = [ matz ]
assert_equal ActiveResource::Formats.remove_root(people), [ matz ]

# On Hash with no root
person = matz
assert_equal ActiveResource::Formats.remove_root(person), matz

# On Hash with root
person = matz_with_root
assert_equal ActiveResource::Formats.remove_root(person), matz
end

private
def using_format(klass, mime_type_reference)
previous_format = klass.format
Expand Down

0 comments on commit f3115bc

Please sign in to comment.