@@ -332,50 +332,24 @@ def attributes(requested_attrs = nil, reload = false)
332332 # @param [JSONAPI::IncludeDirective] include_directive (defaults to the
333333 # +default_include_directive+ config value when not provided)
334334 # @return [Enumerator<Association>]
335- #
336335 def associations ( include_directive = ActiveModelSerializers . default_include_directive , include_slice = nil )
337336 include_slice ||= include_directive
338- return unless object
337+ return Enumerator . new unless object
339338
340339 Enumerator . new do |y |
341- self . class . _reflections . values . each do |reflection |
340+ self . class . _reflections . each do |key , reflection |
342341 next if reflection . excluded? ( self )
343- key = reflection . options . fetch ( :key , reflection . name )
344342 next unless include_directive . key? ( key )
345343
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
347346 end
348347 end
349348 end
350349
351350 # @return [Hash] containing the attributes and first level
352351 # associations, similar to how ActiveModel::Serializers::JSON is used
353352 # 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 } })
379353 def serializable_hash ( adapter_options = nil , options = { } , adapter_instance = self . class . serialization_adapter_instance )
380354 adapter_options ||= { }
381355 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
387361 alias to_h serializable_hash
388362
389363 # @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.
397364 def as_json ( adapter_opts = nil )
398365 serializable_hash ( adapter_opts )
399366 end
@@ -424,14 +391,12 @@ def attributes_hash(_adapter_options, options, adapter_instance)
424391
425392 # @api private
426393 def associations_hash ( adapter_options , options , adapter_instance )
427- relationships = { }
428394 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 )
432399 end
433-
434- relationships
435400 end
436401
437402 protected
0 commit comments