Skip to content

Commit 7d8fb16

Browse files
committed
Cleanup
1 parent ee69293 commit 7d8fb16

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

lib/active_model/serializer.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ def associations(include_directive = ActiveModelSerializers.default_include_dire
341341
next if reflection.excluded?(self)
342342
next unless include_directive.key?(key)
343343

344-
y.yield reflection.build_association(self, instance_options, include_slice)
344+
association = reflection.build_association(self, instance_options, include_slice)
345+
y.yield association
345346
end
346347
end
347348
end
@@ -390,14 +391,12 @@ def attributes_hash(_adapter_options, options, adapter_instance)
390391

391392
# @api private
392393
def associations_hash(adapter_options, options, adapter_instance)
393-
relationships = {}
394394
include_directive = options.fetch(:include_directive)
395-
associations(include_directive).each do |association|
396-
adapter_opts = adapter_options.merge(include_directive: include_directive[association.key])
397-
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)
398399
end
399-
400-
relationships
401400
end
402401

403402
protected

lib/active_model/serializer/reflection.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ class Serializer
3737
# 1) as 'comments' and named 'comments'.
3838
# 2) as 'object.comments.last(1)' and named 'last_comments'.
3939
#
40-
# PostSerializer._reflections #=>
41-
# # [
42-
# # HasOneReflection.new(:author, serializer: AuthorSerializer),
43-
# # HasManyReflection.new(:comments)
44-
# # HasManyReflection.new(:comments, { key: :last_comments }, #<Block>)
45-
# # HasManyReflection.new(:secret_meta_data, { if: :is_admin? })
46-
# # ]
40+
# PostSerializer._reflections # =>
41+
# # {
42+
# # author: HasOneReflection.new(:author, serializer: AuthorSerializer),
43+
# # comments: HasManyReflection.new(:comments)
44+
# # last_comments: HasManyReflection.new(:comments, { key: :last_comments }, #<Block>)
45+
# # secret_meta_data: HasManyReflection.new(:secret_meta_data, { if: :is_admin? })
46+
# # }
4747
#
4848
# So you can inspect reflections in your Adapters.
4949
class Reflection < Field

0 commit comments

Comments
 (0)