@@ -98,6 +98,7 @@ def self.get_serializer_for(klass)
98
98
end
99
99
end
100
100
101
+ # @api private
101
102
def self . include_directive_from_options ( options )
102
103
if options [ :include_directive ]
103
104
options [ :include_directive ]
@@ -161,9 +162,9 @@ def success?
161
162
# serializer.as_json(include: { posts: { include: { comments: { only: :body } }, only: :title } })
162
163
def serializable_hash ( adapter_opts = nil )
163
164
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 )
167
168
end
168
169
alias to_hash serializable_hash
169
170
alias to_h serializable_hash
@@ -195,33 +196,38 @@ def read_attribute_for_serialization(attr)
195
196
end
196
197
end
197
198
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 )
199
202
cached_attributes = adapter_options [ :cached_attributes ] ||= { }
200
203
resource = cached_attributes ( options [ :fields ] , cached_attributes , adapter_instance )
201
- relationships = resource_relationships ( options )
204
+ relationships = resource_relationships ( adapter_options , options , adapter_instance )
202
205
resource . merge ( relationships )
203
206
end
204
207
205
- def resource_relationships ( options )
208
+ # @api private
209
+ def resource_relationships ( adapter_options , options , adapter_instance )
206
210
relationships = { }
207
211
include_directive = options . fetch ( :include_directive )
208
212
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 )
210
215
end
211
216
212
217
relationships
213
218
end
214
219
215
- def relationship_value_for ( association , options )
220
+ # @api private
221
+ def relationship_value_for ( association , adapter_options , adapter_instance )
216
222
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
218
226
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 )
222
228
223
229
if association . options [ :polymorphic ] && relationship_value
224
- polymorphic_type = association . serializer . object . class . name . underscore
230
+ polymorphic_type = association_object . class . name . underscore
225
231
relationship_value = { type : polymorphic_type , polymorphic_type . to_sym => relationship_value }
226
232
end
227
233
0 commit comments