Skip to content

Commit 9695454

Browse files
st0012lgebhardt
authored andcommitted
Apply records_for_RELATIONSHIP method on polymorphic to many linkage
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
1 parent 87467eb commit 9695454

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

lib/jsonapi/resource_serializer.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,16 @@ def to_one_linkage(source, relationship)
394394

395395
def to_many_linkage(source, relationship)
396396
linkage = []
397+
include_config = include_directives.include_config(relationship.name.to_sym) if include_directives
398+
include_filters = include_config[:include_filters] if include_config
399+
options = { filters: include_filters || {} }
400+
397401
linkage_types_and_values = if source.preloaded_fragments.has_key?(format_key(relationship.name))
398402
source.preloaded_fragments[format_key(relationship.name)].map do |_, resource|
399403
[relationship.type, resource.id]
400404
end
401405
elsif relationship.polymorphic?
402-
assoc = source._model.public_send(relationship.name)
406+
assoc = source.public_send("records_for_#{relationship.name}", options)
403407
# Avoid hitting the database again for values already pre-loaded
404408
if assoc.respond_to?(:loaded?) and assoc.loaded?
405409
assoc.map do |obj|
@@ -411,9 +415,6 @@ def to_many_linkage(source, relationship)
411415
end
412416
end
413417
else
414-
include_config = include_directives.include_config(relationship.name.to_sym) if include_directives
415-
include_filters = include_config[:include_filters] if include_config
416-
options = { filters: include_filters || {} }
417418
source.public_send(relationship.name, options).map do |value|
418419
[relationship.type, value.id]
419420
end

test/unit/serializer/polymorphic_serializer_test.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,73 @@ def test_sti_polymorphic_to_many_serialization
130130
)
131131
end
132132

133+
def test_sti_polymorphic_to_many_serialization_with_custom_polymorphic_records
134+
person_resource = PersonResource.new(@person, nil)
135+
serializer = JSONAPI::ResourceSerializer.new(
136+
PersonResource,
137+
include: %w(vehicles)
138+
)
139+
140+
def person_resource.records_for_vehicles(opts = {})
141+
@model.vehicles.none
142+
end
143+
144+
serialized_data = serializer.serialize_to_hash(person_resource)
145+
146+
assert_hash_equals(
147+
{
148+
data: {
149+
id: '1',
150+
type: 'people',
151+
links: {
152+
self: '/people/1'
153+
},
154+
attributes: {
155+
name: 'Joe Author',
156+
email: 'joe@xyz.fake',
157+
dateJoined: '2013-08-07 16:25:00 -0400'
158+
},
159+
relationships: {
160+
comments: {
161+
links: {
162+
self: '/people/1/relationships/comments',
163+
related: '/people/1/comments'
164+
}
165+
},
166+
posts: {
167+
links: {
168+
self: '/people/1/relationships/posts',
169+
related: '/people/1/posts'
170+
}
171+
},
172+
vehicles: {
173+
links: {
174+
self: '/people/1/relationships/vehicles',
175+
related: '/people/1/vehicles'
176+
},
177+
:data => []
178+
},
179+
preferences: {
180+
links: {
181+
self: '/people/1/relationships/preferences',
182+
related: '/people/1/preferences'
183+
}
184+
},
185+
hairCut: {
186+
links: {
187+
self: '/people/1/relationships/hairCut',
188+
related: '/people/1/hairCut'
189+
}
190+
}
191+
}
192+
}
193+
},
194+
serialized_data
195+
)
196+
end
197+
198+
199+
133200
def test_polymorphic_belongs_to_serialization
134201
serialized_data = JSONAPI::ResourceSerializer.new(
135202
PictureResource,

0 commit comments

Comments
 (0)