Stores passed in options in array serializers

This is supported in single serializers. This adds support for passing
options from array serializers to each serializer in it.
This commit is contained in:
Alexandre de Oliveira 2015-03-11 16:14:02 -03:00
parent 73aeba4177
commit bcd3844e58
3 changed files with 10 additions and 2 deletions

View File

@ -7,12 +7,14 @@ module ActiveModel
attr_reader :meta, :meta_key
def initialize(objects, options = {})
options.merge!(root: nil)
@objects = objects.map do |object|
serializer_class = options.fetch(
:serializer,
ActiveModel::Serializer.serializer_for(object)
)
serializer_class.new(object)
serializer_class.new(object, options)
end
@meta = options[:meta]
@meta_key = options[:meta_key]

View File

@ -6,7 +6,7 @@ module ActiveModel
def setup
@comment = Comment.new
@post = Post.new
@serializer = ArraySerializer.new([@comment, @post])
@serializer = ArraySerializer.new([@comment, @post], {some: :options})
end
def test_respond_to_each
@ -21,6 +21,8 @@ module ActiveModel
assert_kind_of PostSerializer, serializers.last
assert_kind_of Post, serializers.last.object
assert_equal serializers.last.custom_options[:some], :options
end
end
end

View File

@ -78,6 +78,10 @@ PostSerializer = Class.new(ActiveModel::Serializer) do
def blog
Blog.new(id: 999, name: "Custom blog")
end
def custom_options
options
end
end
SpammyPostSerializer = Class.new(ActiveModel::Serializer) do