@@ -98,6 +98,7 @@ def self.get_serializer_for(klass)
9898 end
9999 end
100100
101+ # @api private
101102 def self . include_directive_from_options ( options )
102103 if options [ :include_directive ]
103104 options [ :include_directive ]
@@ -161,9 +162,9 @@ def success?
161162 # serializer.as_json(include: { posts: { include: { comments: { only: :body } }, only: :title } })
162163 def serializable_hash ( adapter_opts = nil )
163164 adapter_opts ||= { }
164- adapter_opts = { include : '*' , adapter : :attributes } . merge! ( adapter_opts )
165- adapter = ActiveModelSerializers ::Adapter . create ( self , adapter_opts )
166- adapter . serializable_hash ( adapter_opts )
165+ adapter_opts = { include : '*' } . merge! ( adapter_opts )
166+ adapter_instance = ActiveModelSerializers ::Adapter :: Attributes . new ( self , adapter_opts )
167+ serialize ( adapter_opts , { } , adapter_instance )
167168 end
168169 alias to_hash serializable_hash
169170 alias to_h serializable_hash
@@ -195,33 +196,38 @@ def read_attribute_for_serialization(attr)
195196 end
196197 end
197198
198- def serializable_hash_for_single_resource ( adapter_options , options , adapter_instance )
199+ # @api private
200+ def serialize ( adapter_options , options , adapter_instance )
201+ options [ :include_directive ] ||= ActiveModel ::Serializer . include_directive_from_options ( adapter_options )
199202 cached_attributes = adapter_options [ :cached_attributes ] ||= { }
200203 resource = cached_attributes ( options [ :fields ] , cached_attributes , adapter_instance )
201- relationships = resource_relationships ( options )
204+ relationships = resource_relationships ( adapter_options , options , adapter_instance )
202205 resource . merge ( relationships )
203206 end
204207
205- def resource_relationships ( options )
208+ # @api private
209+ def resource_relationships ( adapter_options , options , adapter_instance )
206210 relationships = { }
207211 include_directive = options . fetch ( :include_directive )
208212 associations ( include_directive ) . each do |association |
209- relationships [ association . key ] ||= relationship_value_for ( association , options )
213+ adapter_opts = adapter_options . merge ( include_directive : include_directive [ association . key ] )
214+ relationships [ association . key ] ||= relationship_value_for ( association , adapter_opts , adapter_instance )
210215 end
211216
212217 relationships
213218 end
214219
215- def relationship_value_for ( association , options )
220+ # @api private
221+ def relationship_value_for ( association , adapter_options , adapter_instance )
216222 return association . options [ :virtual_value ] if association . options [ :virtual_value ]
217- return unless association . serializer && association . serializer . object
223+ association_serializer = association . serializer
224+ association_object = association_serializer && association_serializer . object
225+ return unless association_object
218226
219- include_directive = options . fetch ( :include_directive )
220- opts = instance_options . merge ( include_directive : include_directive [ association . key ] )
221- relationship_value = ActiveModelSerializers ::Adapter ::Attributes . new ( association . serializer , opts ) . serializable_hash ( options )
227+ relationship_value = association_serializer . serialize ( adapter_options , { } , adapter_instance )
222228
223229 if association . options [ :polymorphic ] && relationship_value
224- polymorphic_type = association . serializer . object . class . name . underscore
230+ polymorphic_type = association_object . class . name . underscore
225231 relationship_value = { type : polymorphic_type , polymorphic_type . to_sym => relationship_value }
226232 end
227233
0 commit comments