@@ -92,6 +92,9 @@ def find_to_populate_by_keys(keys, options = {})
92
92
def find_fragments ( filters , options = { } )
93
93
include_directives = options [ :include_directives ] ? options [ :include_directives ] . include_directives : { }
94
94
resource_klass = self
95
+
96
+ fragments = { }
97
+
95
98
linkage_relationships = to_one_relationships_for_linkage ( include_directives [ :include_related ] )
96
99
97
100
sort_criteria = options . fetch ( :sort_criteria ) { [ ] }
@@ -129,19 +132,33 @@ def find_fragments(filters, options = {})
129
132
if linkage_relationship . polymorphic? && linkage_relationship . belongs_to?
130
133
linkage_relationship . resource_types . each do |resource_type |
131
134
klass = resource_klass_for ( resource_type )
132
- linkage_fields << { relationship_name : name , resource_klass : klass }
133
-
134
135
linkage_table_alias = join_manager . join_details_by_polymorphic_relationship ( linkage_relationship , resource_type ) [ :alias ]
135
136
primary_key = klass . _primary_key
137
+
138
+ linkage_fields << { relationship_name : name ,
139
+ linkage_relationship : linkage_relationship ,
140
+ resource_klass : klass ,
141
+ field : "#{ concat_table_field ( linkage_table_alias , primary_key ) } AS #{ linkage_table_alias } _#{ primary_key } " ,
142
+ alias : "#{ linkage_table_alias } _#{ primary_key } " }
143
+
136
144
pluck_fields << Arel . sql ( "#{ concat_table_field ( linkage_table_alias , primary_key ) } AS #{ linkage_table_alias } _#{ primary_key } " )
137
145
end
138
146
else
139
147
klass = linkage_relationship . resource_klass
140
- linkage_fields << { relationship_name : name , resource_klass : klass }
141
-
142
148
linkage_table_alias = join_manager . join_details_by_relationship ( linkage_relationship ) [ :alias ]
143
149
primary_key = klass . _primary_key
150
+
151
+ linkage_fields << { relationship_name : name ,
152
+ linkage_relationship : linkage_relationship ,
153
+ resource_klass : klass ,
154
+ field : "#{ concat_table_field ( linkage_table_alias , primary_key ) } AS #{ linkage_table_alias } _#{ primary_key } " ,
155
+ alias : "#{ linkage_table_alias } _#{ primary_key } " }
156
+
144
157
pluck_fields << Arel . sql ( "#{ concat_table_field ( linkage_table_alias , primary_key ) } AS #{ linkage_table_alias } _#{ primary_key } " )
158
+
159
+ if linkage_relationship . sti?
160
+ pluck_fields << Arel . sql ( "#{ concat_table_field ( linkage_table_alias , 'type' ) } AS #{ linkage_table_alias } _type" )
Has a conversation. Original line has a conversation.
161
+ end
145
162
end
146
163
end
147
164
@@ -158,7 +175,6 @@ def find_fragments(filters, options = {})
158
175
pluck_fields << Arel . sql ( field )
159
176
end
160
177
161
- fragments = { }
162
178
rows = records . pluck ( *pluck_fields )
163
179
rows . each do |row |
164
180
rid = JSONAPI ::ResourceIdentity . new ( resource_klass , pluck_fields . length == 1 ? row : row [ 0 ] )
@@ -175,7 +191,14 @@ def find_fragments(filters, options = {})
175
191
fragments [ rid ] . initialize_related ( linkage_field_details [ :relationship_name ] )
176
192
related_id = row [ attributes_offset ]
177
193
if related_id
178
- related_rid = JSONAPI ::ResourceIdentity . new ( linkage_field_details [ :resource_klass ] , related_id )
194
+ if linkage_field_details [ :linkage_relationship ] . sti?
195
+ type = row [ 2 ]
196
+ related_rid = JSONAPI ::ResourceIdentity . new ( resource_klass_for ( type ) , related_id )
197
+ attributes_offset += 1
198
+ else
199
+ related_rid = JSONAPI ::ResourceIdentity . new ( linkage_field_details [ :resource_klass ] , related_id )
200
+ end
201
+
179
202
fragments [ rid ] . add_related_identity ( linkage_field_details [ :relationship_name ] , related_rid )
180
203
end
181
204
attributes_offset += 1
@@ -413,6 +436,10 @@ def find_related_monomorphic_fragments(source_rids, relationship, options, conne
413
436
Arel . sql ( "#{ concat_table_field ( resource_table_alias , resource_klass . _primary_key ) } AS #{ resource_table_alias } _#{ resource_klass . _primary_key } " )
414
437
]
415
438
439
+ if relationship . sti?
440
+ pluck_fields << Arel . sql ( "#{ concat_table_field ( resource_table_alias , 'type' ) } AS #{ resource_table_alias } _type" )
441
+ end
442
+
416
443
cache_field = resource_klass . attribute_to_model_field ( :_cache_field ) if options [ :cache ]
417
444
if cache_field
418
445
pluck_fields << Arel . sql ( "#{ concat_table_field ( resource_table_alias , cache_field [ :name ] ) } AS #{ resource_table_alias } _#{ cache_field [ :name ] } " )
@@ -458,12 +485,17 @@ def find_related_monomorphic_fragments(source_rids, relationship, options, conne
458
485
fragments = { }
459
486
rows = records . distinct . pluck ( *pluck_fields )
460
487
rows . each do |row |
461
- rid = JSONAPI ::ResourceIdentity . new ( resource_klass , row [ 1 ] )
488
+ if relationship . sti?
489
+ type = row [ 2 ]
490
+ rid = JSONAPI ::ResourceIdentity . new ( resource_klass_for ( type ) , row [ 1 ] )
491
+ attributes_offset = 3
492
+ else
493
+ rid = JSONAPI ::ResourceIdentity . new ( resource_klass , row [ 1 ] )
494
+ attributes_offset = 2
495
+ end
462
496
463
497
fragments [ rid ] ||= JSONAPI ::ResourceFragment . new ( rid )
464
498
465
- attributes_offset = 2
466
-
467
499
if cache_field
468
500
fragments [ rid ] . cache = cast_to_attribute_type ( row [ attributes_offset ] , cache_field [ :type ] )
469
501
attributes_offset += 1