@@ -332,50 +332,24 @@ def attributes(requested_attrs = nil, reload = false)
332
332
# @param [JSONAPI::IncludeDirective] include_directive (defaults to the
333
333
# +default_include_directive+ config value when not provided)
334
334
# @return [Enumerator<Association>]
335
- #
336
335
def associations ( include_directive = ActiveModelSerializers . default_include_directive , include_slice = nil )
337
336
include_slice ||= include_directive
338
- return unless object
337
+ return Enumerator . new unless object
339
338
340
339
Enumerator . new do |y |
341
- self . class . _reflections . values . each do |reflection |
340
+ self . class . _reflections . each do |key , reflection |
342
341
next if reflection . excluded? ( self )
343
- key = reflection . options . fetch ( :key , reflection . name )
344
342
next unless include_directive . key? ( key )
345
343
346
- y . yield reflection . build_association ( self , instance_options , include_slice )
344
+ association = reflection . build_association ( self , instance_options , include_slice )
345
+ y . yield association
347
346
end
348
347
end
349
348
end
350
349
351
350
# @return [Hash] containing the attributes and first level
352
351
# associations, similar to how ActiveModel::Serializers::JSON is used
353
352
# in ActiveRecord::Base.
354
- #
355
- # TODO: Include <tt>ActiveModel::Serializers::JSON</tt>.
356
- # So that the below is true:
357
- # @param options [nil, Hash] The same valid options passed to `serializable_hash`
358
- # (:only, :except, :methods, and :include).
359
- #
360
- # See
361
- # https://github.com/rails/rails/blob/v5.0.0.beta2/activemodel/lib/active_model/serializers/json.rb#L17-L101
362
- # https://github.com/rails/rails/blob/v5.0.0.beta2/activemodel/lib/active_model/serialization.rb#L85-L123
363
- # https://github.com/rails/rails/blob/v5.0.0.beta2/activerecord/lib/active_record/serialization.rb#L11-L17
364
- # https://github.com/rails/rails/blob/v5.0.0.beta2/activesupport/lib/active_support/core_ext/object/json.rb#L147-L162
365
- #
366
- # @example
367
- # # The :only and :except options can be used to limit the attributes included, and work
368
- # # similar to the attributes method.
369
- # serializer.as_json(only: [:id, :name])
370
- # serializer.as_json(except: [:id, :created_at, :age])
371
- #
372
- # # To include the result of some method calls on the model use :methods:
373
- # serializer.as_json(methods: :permalink)
374
- #
375
- # # To include associations use :include:
376
- # serializer.as_json(include: :posts)
377
- # # Second level and higher order associations work as well:
378
- # serializer.as_json(include: { posts: { include: { comments: { only: :body } }, only: :title } })
379
353
def serializable_hash ( adapter_options = nil , options = { } , adapter_instance = self . class . serialization_adapter_instance )
380
354
adapter_options ||= { }
381
355
options [ :include_directive ] ||= ActiveModel ::Serializer . include_directive_from_options ( adapter_options )
@@ -387,13 +361,6 @@ def serializable_hash(adapter_options = nil, options = {}, adapter_instance = se
387
361
alias to_h serializable_hash
388
362
389
363
# @see #serializable_hash
390
- # TODO: When moving attributes adapter logic here, @see #serializable_hash
391
- # So that the below is true:
392
- # @param options [nil, Hash] The same valid options passed to `as_json`
393
- # (:root, :only, :except, :methods, and :include).
394
- # The default for `root` is nil.
395
- # The default value for include_root is false. You can change it to true if the given
396
- # JSON string includes a single root node.
397
364
def as_json ( adapter_opts = nil )
398
365
serializable_hash ( adapter_opts )
399
366
end
@@ -424,14 +391,12 @@ def attributes_hash(_adapter_options, options, adapter_instance)
424
391
425
392
# @api private
426
393
def associations_hash ( adapter_options , options , adapter_instance )
427
- relationships = { }
428
394
include_directive = options . fetch ( :include_directive )
429
- associations ( include_directive ) . each do |association |
430
- adapter_opts = adapter_options . merge ( include_directive : include_directive [ association . key ] )
431
- relationships [ association . key ] ||= association . serializable_hash ( adapter_opts , adapter_instance )
395
+ include_slice = options [ :include_slice ]
396
+ associations ( include_directive , include_slice ) . each_with_object ( { } ) do |association , relationships |
397
+ adapter_opts = adapter_options . merge ( include_directive : include_directive [ association . key ] , adapter_instance : adapter_instance )
398
+ relationships [ association . key ] = association . serializable_hash ( adapter_opts , adapter_instance )
432
399
end
433
-
434
- relationships
435
400
end
436
401
437
402
protected
0 commit comments