Allow ArraySerializer to pass the options down to item serializers

This commit is contained in:
Santiago Pastorino 2013-09-14 22:25:59 -03:00
parent 6f3503c965
commit 8006529e20
2 changed files with 25 additions and 1 deletions

View File

@ -25,7 +25,7 @@ module ActiveModel
def serializable_array
@object.map do |item|
serializer = @options[:each_serializer] || Serializer.serializer_for(item) || DefaultSerializer
serializer.new(item).serializable_object(@options.merge(root: nil))
serializer.new(item, @options.merge(root: nil)).serializable_object
end
end
alias serializable_object serializable_array

View File

@ -0,0 +1,24 @@
require 'test_helper'
module ActiveModel
class ArraySerializer
class ScopeTest < ActiveModel::TestCase
def test_array_serializer_pass_options_to_items_serializers
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
serializer = ArraySerializer.new(array, scope: current_user)
expected = [{'name' => 'Name 1', 'description' => 'Description 1 - user'},
{'name' => 'Name 2', 'description' => 'Description 2 - user'}]
assert_equal expected, serializer.serializable_array
end
private
def current_user
'user'
end
end
end
end