@@ -82,17 +82,17 @@ def find_fragments(filters, options = {})
82
82
fragments = { }
83
83
records . pluck ( *pluck_fields ) . collect do |row |
84
84
rid = JSONAPI ::ResourceIdentity . new ( self , pluck_fields . length == 1 ? row : row [ 0 ] )
85
- fragments [ rid ] = { identity : rid }
85
+
86
+ fragments [ rid ] ||= JSONAPI ::ResourceFragment . new ( rid )
86
87
attributes_offset = 1
87
88
88
89
if cache_field
89
- fragments [ rid ] [ : cache] = cast_to_attribute_type ( row [ 1 ] , cache_field [ :type ] )
90
+ fragments [ rid ] . cache = cast_to_attribute_type ( row [ 1 ] , cache_field [ :type ] )
90
91
attributes_offset += 1
91
92
end
92
93
93
- fragments [ rid ] [ :attributes ] = { } unless model_fields . empty?
94
94
model_fields . each_with_index do |k , idx |
95
- fragments [ rid ] [ : attributes] [ k [ 0 ] ] = cast_to_attribute_type ( row [ idx + attributes_offset ] , k [ 1 ] [ :type ] )
95
+ fragments [ rid ] . attributes [ k [ 0 ] ] = cast_to_attribute_type ( row [ idx + attributes_offset ] , k [ 1 ] [ :type ] )
96
96
end
97
97
end
98
98
@@ -110,13 +110,23 @@ def find_fragments(filters, options = {})
110
110
# @return [Hash{ResourceIdentity => {identity: => ResourceIdentity, cache: cache_field, attributes: => {name => value}, related: {relationship_name: [] }}}]
111
111
# the ResourceInstances matching the filters, sorting, and pagination rules along with any request
112
112
# additional_field values
113
- def find_related_fragments ( source_rids , relationship_name , options = { } , included_key = nil )
113
+ def find_related_fragments ( source_rids , relationship_name , options = { } )
114
+ relationship = _relationship ( relationship_name )
115
+
116
+ if relationship . polymorphic? && relationship . foreign_key_on == :self
117
+ find_related_polymorphic_fragments ( source_rids , relationship , options , false )
118
+ else
119
+ find_related_monomorphic_fragments ( source_rids , relationship , options , false )
120
+ end
121
+ end
122
+
123
+ def find_included_fragments ( source_rids , relationship_name , options = { } )
114
124
relationship = _relationship ( relationship_name )
115
125
116
126
if relationship . polymorphic? && relationship . foreign_key_on == :self
117
- find_related_polymorphic_fragments ( source_rids , relationship , options )
127
+ find_related_polymorphic_fragments ( source_rids , relationship , options , true )
118
128
else
119
- find_related_monomorphic_fragments ( source_rids , relationship , included_key , options )
129
+ find_related_monomorphic_fragments ( source_rids , relationship , options , true )
120
130
end
121
131
end
122
132
@@ -215,7 +225,7 @@ def find_records_by_keys(keys, options = {})
215
225
records ( options ) . where ( { _primary_key => keys } )
216
226
end
217
227
218
- def find_related_monomorphic_fragments ( source_rids , relationship , included_key , options = { } )
228
+ def find_related_monomorphic_fragments ( source_rids , relationship , options , connect_source_identity )
219
229
opts = options . dup
220
230
221
231
source_ids = source_rids . collect { |rid | rid . id }
@@ -260,8 +270,7 @@ def find_related_monomorphic_fragments(source_rids, relationship, included_key,
260
270
261
271
# ToDO: Remove count check. Currently pagination isn't working with multiple source_rids (i.e. it only works
262
272
# for show relationships, not related includes).
263
- # Check included_key to not paginate included resources but ensure that nested resources can be paginated
264
- if paginator && source_rids . count == 1 && !included_key
273
+ if paginator && source_rids . count == 1
265
274
records = related_klass . apply_pagination ( records , paginator , order_options )
266
275
end
267
276
@@ -287,28 +296,35 @@ def find_related_monomorphic_fragments(source_rids, relationship, included_key,
287
296
288
297
rows = records . pluck ( *pluck_fields )
289
298
290
- relation_name = relationship . name . to_sym
291
-
292
299
related_fragments = { }
293
300
294
301
rows . each do |row |
295
302
unless row [ 1 ] . nil?
296
303
rid = JSONAPI ::ResourceIdentity . new ( related_klass , row [ 1 ] )
297
- related_fragments [ rid ] ||= { identity : rid , related : { relation_name => [ ] } }
304
+
305
+ related_fragments [ rid ] ||= JSONAPI ::ResourceFragment . new ( rid )
298
306
299
307
attributes_offset = 2
300
308
301
309
if cache_field
302
- related_fragments [ rid ] [ : cache] = cast_to_attribute_type ( row [ attributes_offset ] , cache_field [ :type ] )
310
+ related_fragments [ rid ] . cache = cast_to_attribute_type ( row [ attributes_offset ] , cache_field [ :type ] )
303
311
attributes_offset += 1
304
312
end
305
313
306
- related_fragments [ rid ] [ :attributes ] = { } unless model_fields . empty?
307
314
model_fields . each_with_index do |k , idx |
308
- related_fragments [ rid ] [ : attributes] [ k [ 0 ] ] = cast_to_attribute_type ( row [ idx + attributes_offset ] , k [ 1 ] [ :type ] )
315
+ related_fragments [ rid ] . attributes [ k [ 0 ] ] = cast_to_attribute_type ( row [ idx + attributes_offset ] , k [ 1 ] [ :type ] )
309
316
end
310
317
311
- related_fragments [ rid ] [ :related ] [ relation_name ] << JSONAPI ::ResourceIdentity . new ( self , row [ 0 ] )
318
+ source_rid = JSONAPI ::ResourceIdentity . new ( self , row [ 0 ] )
319
+
320
+ related_fragments [ rid ] . add_related_from ( source_rid )
321
+
322
+ if connect_source_identity
323
+ related_relationship = related_klass . _relationships [ relationship . inverse_relationship ]
324
+ if related_relationship
325
+ related_fragments [ rid ] . add_related_identity ( related_relationship . name , source_rid )
326
+ end
327
+ end
312
328
end
313
329
end
314
330
@@ -317,7 +333,7 @@ def find_related_monomorphic_fragments(source_rids, relationship, included_key,
317
333
318
334
# Gets resource identities where the related resource is polymorphic and the resource type and id
319
335
# are stored on the primary resources. Cache fields will always be on the related resources.
320
- def find_related_polymorphic_fragments ( source_rids , relationship , options = { } )
336
+ def find_related_polymorphic_fragments ( source_rids , relationship , options , connect_source_identity )
321
337
source_ids = source_rids . collect { |rid | rid . id }
322
338
323
339
context = options [ :context ]
@@ -393,17 +409,24 @@ def find_related_polymorphic_fragments(source_rids, relationship, options = {})
393
409
394
410
rows = records . pluck ( *pluck_fields )
395
411
396
- relation_name = relationship . name . to_sym
397
-
398
412
related_fragments = { }
399
413
400
414
rows . each do |row |
401
415
unless row [ 1 ] . nil? || row [ 2 ] . nil?
402
416
related_klass = resource_klass_for ( row [ 2 ] )
403
417
404
418
rid = JSONAPI ::ResourceIdentity . new ( related_klass , row [ 1 ] )
405
- related_fragments [ rid ] ||= { identity : rid , related : { relation_name => [ ] } }
406
- related_fragments [ rid ] [ :related ] [ relation_name ] << JSONAPI ::ResourceIdentity . new ( self , row [ 0 ] )
419
+ related_fragments [ rid ] ||= JSONAPI ::ResourceFragment . new ( rid )
420
+
421
+ source_rid = JSONAPI ::ResourceIdentity . new ( self , row [ 0 ] )
422
+ related_fragments [ rid ] . add_related_from ( source_rid )
423
+
424
+ if connect_source_identity
425
+ related_relationship = related_klass . _relationships [ relationship . inverse_relationship ]
426
+ if related_relationship
427
+ related_fragments [ rid ] . add_related_identity ( related_relationship . name , source_rid )
428
+ end
429
+ end
407
430
408
431
relation_position = relation_positions [ row [ 2 ] ]
409
432
model_fields = relation_position [ :model_fields ]
@@ -413,14 +436,13 @@ def find_related_polymorphic_fragments(source_rids, relationship, options = {})
413
436
attributes_offset = 0
414
437
415
438
if cache_field
416
- related_fragments [ rid ] [ : cache] = cast_to_attribute_type ( row [ field_offset ] , cache_field [ :type ] )
439
+ related_fragments [ rid ] . cache = cast_to_attribute_type ( row [ field_offset ] , cache_field [ :type ] )
417
440
attributes_offset += 1
418
441
end
419
442
420
443
if attributes . length > 0
421
- related_fragments [ rid ] [ :attributes ] = { }
422
444
model_fields . each_with_index do |k , idx |
423
- related_fragments [ rid ] [ :attributes ] [ k [ 0 ] ] = cast_to_attribute_type ( row [ idx + field_offset + attributes_offset ] , k [ 1 ] [ :type ] )
445
+ related_fragments [ rid ] . add_attribute ( k [ 0 ] , cast_to_attribute_type ( row [ idx + field_offset + attributes_offset ] , k [ 1 ] [ :type ] ) )
424
446
end
425
447
end
426
448
end
@@ -615,4 +637,4 @@ def apply_filter(records, filter, value, options = {})
615
637
end
616
638
end
617
639
end
618
- end
640
+ end
0 commit comments