@@ -92,6 +92,9 @@ def find_to_populate_by_keys(keys, options = {})
9292 def find_fragments ( filters , options = { } )
9393 include_directives = options [ :include_directives ] ? options [ :include_directives ] . include_directives : { }
9494 resource_klass = self
95+
96+ fragments = { }
97+
9598 linkage_relationships = to_one_relationships_for_linkage ( include_directives [ :include_related ] )
9699
97100 sort_criteria = options . fetch ( :sort_criteria ) { [ ] }
@@ -129,19 +132,33 @@ def find_fragments(filters, options = {})
129132 if linkage_relationship . polymorphic? && linkage_relationship . belongs_to?
130133 linkage_relationship . resource_types . each do |resource_type |
131134 klass = resource_klass_for ( resource_type )
132- linkage_fields << { relationship_name : name , resource_klass : klass }
133-
134135 linkage_table_alias = join_manager . join_details_by_polymorphic_relationship ( linkage_relationship , resource_type ) [ :alias ]
135136 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+
136144 pluck_fields << Arel . sql ( "#{ concat_table_field ( linkage_table_alias , primary_key ) } AS #{ linkage_table_alias } _#{ primary_key } " )
137145 end
138146 else
139147 klass = linkage_relationship . resource_klass
140- linkage_fields << { relationship_name : name , resource_klass : klass }
141-
142148 linkage_table_alias = join_manager . join_details_by_relationship ( linkage_relationship ) [ :alias ]
143149 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+
144157 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" )
161+ end
145162 end
146163 end
147164
@@ -158,7 +175,6 @@ def find_fragments(filters, options = {})
158175 pluck_fields << Arel . sql ( field )
159176 end
160177
161- fragments = { }
162178 rows = records . pluck ( *pluck_fields )
163179 rows . each do |row |
164180 rid = JSONAPI ::ResourceIdentity . new ( resource_klass , pluck_fields . length == 1 ? row : row [ 0 ] )
@@ -175,7 +191,14 @@ def find_fragments(filters, options = {})
175191 fragments [ rid ] . initialize_related ( linkage_field_details [ :relationship_name ] )
176192 related_id = row [ attributes_offset ]
177193 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+
179202 fragments [ rid ] . add_related_identity ( linkage_field_details [ :relationship_name ] , related_rid )
180203 end
181204 attributes_offset += 1
@@ -413,6 +436,10 @@ def find_related_monomorphic_fragments(source_rids, relationship, options, conne
413436 Arel . sql ( "#{ concat_table_field ( resource_table_alias , resource_klass . _primary_key ) } AS #{ resource_table_alias } _#{ resource_klass . _primary_key } " )
414437 ]
415438
439+ if relationship . sti?
440+ pluck_fields << Arel . sql ( "#{ concat_table_field ( resource_table_alias , 'type' ) } AS #{ resource_table_alias } _type" )
441+ end
442+
416443 cache_field = resource_klass . attribute_to_model_field ( :_cache_field ) if options [ :cache ]
417444 if cache_field
418445 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
458485 fragments = { }
459486 rows = records . distinct . pluck ( *pluck_fields )
460487 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
462496
463497 fragments [ rid ] ||= JSONAPI ::ResourceFragment . new ( rid )
464498
465- attributes_offset = 2
466-
467499 if cache_field
468500 fragments [ rid ] . cache = cast_to_attribute_type ( row [ attributes_offset ] , cache_field [ :type ] )
469501 attributes_offset += 1
0 commit comments