Skip to content

Commit

Permalink
Apply records_for_RELATIONSHIP method on polymorphic to many linkage
Browse files Browse the repository at this point in the history
Currently we can use `records_for_RELATIONSHIP` to control the included
records. But that change doesn't apply to the linkage data. So we'll get
a consistent result between included records and linkage data. This
commit fixes the issue.

Closes gh-1239
  • Loading branch information
st0012 authored and lgebhardt committed Mar 25, 2019
1 parent 87467eb commit 9695454
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/jsonapi/resource_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,16 @@ def to_one_linkage(source, relationship)

def to_many_linkage(source, relationship)
linkage = []
include_config = include_directives.include_config(relationship.name.to_sym) if include_directives
include_filters = include_config[:include_filters] if include_config
options = { filters: include_filters || {} }

linkage_types_and_values = if source.preloaded_fragments.has_key?(format_key(relationship.name))
source.preloaded_fragments[format_key(relationship.name)].map do |_, resource|
[relationship.type, resource.id]
end
elsif relationship.polymorphic?
assoc = source._model.public_send(relationship.name)
assoc = source.public_send("records_for_#{relationship.name}", options)
# Avoid hitting the database again for values already pre-loaded
if assoc.respond_to?(:loaded?) and assoc.loaded?
assoc.map do |obj|
Expand All @@ -411,9 +415,6 @@ def to_many_linkage(source, relationship)
end
end
else
include_config = include_directives.include_config(relationship.name.to_sym) if include_directives
include_filters = include_config[:include_filters] if include_config
options = { filters: include_filters || {} }
source.public_send(relationship.name, options).map do |value|
[relationship.type, value.id]
end
Expand Down
67 changes: 67 additions & 0 deletions test/unit/serializer/polymorphic_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,73 @@ def test_sti_polymorphic_to_many_serialization
)
end

def test_sti_polymorphic_to_many_serialization_with_custom_polymorphic_records
person_resource = PersonResource.new(@person, nil)
serializer = JSONAPI::ResourceSerializer.new(
PersonResource,
include: %w(vehicles)
)

def person_resource.records_for_vehicles(opts = {})
@model.vehicles.none
end

serialized_data = serializer.serialize_to_hash(person_resource)

assert_hash_equals(
{
data: {
id: '1',
type: 'people',
links: {
self: '/people/1'
},
attributes: {
name: 'Joe Author',
email: '[email protected]',
dateJoined: '2013-08-07 16:25:00 -0400'
},
relationships: {
comments: {
links: {
self: '/people/1/relationships/comments',
related: '/people/1/comments'
}
},
posts: {
links: {
self: '/people/1/relationships/posts',
related: '/people/1/posts'
}
},
vehicles: {
links: {
self: '/people/1/relationships/vehicles',
related: '/people/1/vehicles'
},
:data => []
},
preferences: {
links: {
self: '/people/1/relationships/preferences',
related: '/people/1/preferences'
}
},
hairCut: {
links: {
self: '/people/1/relationships/hairCut',
related: '/people/1/hairCut'
}
}
}
}
},
serialized_data
)
end



def test_polymorphic_belongs_to_serialization
serialized_data = JSONAPI::ResourceSerializer.new(
PictureResource,
Expand Down

0 comments on commit 9695454

Please sign in to comment.