mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
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:
parent
73aeba4177
commit
bcd3844e58
@ -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]
|
||||
|
||||
@ -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
|
||||
|
||||
4
test/fixtures/poro.rb
vendored
4
test/fixtures/poro.rb
vendored
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user