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

Fixes nested has_many links in JSONAPI #730

Merged
merged 1 commit into from
Nov 17, 2014
Merged

Conversation

kurko
Copy link
Member

@kurko kurko commented Nov 13, 2014

When linked resource had has_many links, all of them would use the
association from the first resource, causing all of the items to build
links with the same associations.

This fixes it by iterating over the serializers, not just the
attributes array.

When linked resource had has_many links, all of them would use the
association from the first resource, causing all of the items to build
`links` with the same associations.

This fixes it by iterating over the serializers, not just the
attributes array.
body: "Body",
id: "3",
links: {
comments: [],
Copy link
Member Author

Choose a reason for hiding this comment

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

The bug was here. Before, comments was ["1", "2"], not [].

@kurko
Copy link
Member Author

kurko commented Nov 13, 2014

Need a pair of eyes here @guilleiguaran @ggordon

@@ -58,25 +58,30 @@ def add_link(resource, name, serializer)
end
end

def add_linked(resource_name, serializer, parent = nil)
def add_linked(resource_name, serializers, parent = nil)
serializers = Array(serializers) unless serializers.respond_to?(:each)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is 'unless ...' needed here? If serializers is already an Array, Array(serializers) would just retunr serilizers.

Copy link
Member Author

Choose a reason for hiding this comment

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

So, It can be Serializer or ArraySerializer. If it's Serializer, I convert to [Serializer] so I can treat them all equally, without the need for duplicating the logic, eg.

if serializer.respond_to?(:each)
  serializer.each  
    do something
  end
else
  do something
end

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, Array(array_serializer) would be wrong.

@ggordon
Copy link
Contributor

ggordon commented Nov 13, 2014

LGTM 👍

kurko added a commit that referenced this pull request Nov 17, 2014
@kurko kurko merged commit a933d44 into master Nov 17, 2014
@kurko kurko deleted the fixes-nested-has-many-links branch November 17, 2014 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants