Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for ArraySerializer json_key #1007

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/active_model/serializer/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def json_key
if @objects.first
@objects.first.json_key.pluralize
else
@resource.name.downcase.pluralize if @resource.try(:name)
@resource.name.underscore.pluralize if @resource.try(:name)
end
end

Expand Down
11 changes: 11 additions & 0 deletions test/array_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ def test_meta_and_meta_key_attr_readers
assert_equal @serializer.meta, "the meta"
assert_equal @serializer.meta_key, "the meta key"
end

def test_json_key_when_resource_is_empty
Array.class_eval do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like it's trying to be a fake AR Relation

def name
'PostComment'
end
end
@post_comments = []
@serializer = ArraySerializer.new(@post_comments)
assert_equal @serializer.json_key, "post_comments"
end
end
end
end