Skip to content

Commit 4b32e39

Browse files
committed
Merge pull request #837 from rails-api/store-options-in-array-serializers
Store options in array serializers
2 parents 73aeba4 + af81a40 commit 4b32e39

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
lines changed

lib/active_model/serializer.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,20 @@ def attributes(options = {})
176176
end
177177

178178
def each_association(&block)
179-
self.class._associations.dup.each do |name, options|
179+
self.class._associations.dup.each do |name, association_options|
180180
next unless object
181181

182182
association = object.send(name)
183183
association_value = send(name)
184-
serializer_class = ActiveModel::Serializer.serializer_for(association, options)
184+
serializer_class = ActiveModel::Serializer.serializer_for(association, association_options)
185+
185186
serializer = serializer_class.new(
186187
association_value,
187-
serializer_from_options(options)
188+
serializer_from_options(association_options).merge(options)
188189
) if serializer_class
189190

190191
if block_given?
191-
block.call(name, serializer, options[:association_options])
192+
block.call(name, serializer, association_options[:association_options])
192193
end
193194
end
194195
end

lib/active_model/serializer/array_serializer.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ class ArraySerializer
77
attr_reader :meta, :meta_key
88

99
def initialize(objects, options = {})
10+
options.merge!(root: nil)
11+
1012
@objects = objects.map do |object|
1113
serializer_class = options.fetch(
1214
:serializer,
1315
ActiveModel::Serializer.serializer_for(object)
1416
)
15-
serializer_class.new(object)
17+
serializer_class.new(object, options)
1618
end
1719
@meta = options[:meta]
1820
@meta_key = options[:meta_key]

test/array_serializer_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ArraySerializerTest < Minitest::Test
66
def setup
77
@comment = Comment.new
88
@post = Post.new
9-
@serializer = ArraySerializer.new([@comment, @post])
9+
@serializer = ArraySerializer.new([@comment, @post], {some: :options})
1010
end
1111

1212
def test_respond_to_each
@@ -21,6 +21,8 @@ def test_each_object_should_be_serialized_with_appropriate_serializer
2121

2222
assert_kind_of PostSerializer, serializers.last
2323
assert_kind_of Post, serializers.last.object
24+
25+
assert_equal serializers.last.custom_options[:some], :options
2426
end
2527
end
2628
end

test/fixtures/poro.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ module Spam; end
7878
def blog
7979
Blog.new(id: 999, name: "Custom blog")
8080
end
81+
82+
def custom_options
83+
options
84+
end
8185
end
8286

8387
SpammyPostSerializer = Class.new(ActiveModel::Serializer) do
@@ -95,6 +99,10 @@ def self.root_name
9599

96100
belongs_to :post
97101
belongs_to :author
102+
103+
def custom_options
104+
options
105+
end
98106
end
99107

100108
AuthorSerializer = Class.new(ActiveModel::Serializer) do

test/serializers/associations_test.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def setup
3737
@post.author = @author
3838
@author.posts = [@post]
3939

40-
@post_serializer = PostSerializer.new(@post)
40+
@post_serializer = PostSerializer.new(@post, {custom_options: true})
4141
@author_serializer = AuthorSerializer.new(@author)
4242
@comment_serializer = CommentSerializer.new(@comment)
4343
end
@@ -65,6 +65,14 @@ def test_has_many_and_has_one
6565
end
6666
end
6767

68+
def test_serializer_options_are_passed_into_associations_serializers
69+
@post_serializer.each_association do |name, association|
70+
if name == :comments
71+
assert association.first.custom_options[:custom_options]
72+
end
73+
end
74+
end
75+
6876
def test_belongs_to
6977
assert_equal(
7078
{ post: { type: :belongs_to, association_options: {} },

0 commit comments

Comments
 (0)