mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Allow ArraySerializer to pass the options down to item serializers
This commit is contained in:
parent
6f3503c965
commit
8006529e20
@ -25,7 +25,7 @@ module ActiveModel
|
|||||||
def serializable_array
|
def serializable_array
|
||||||
@object.map do |item|
|
@object.map do |item|
|
||||||
serializer = @options[:each_serializer] || Serializer.serializer_for(item) || DefaultSerializer
|
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
|
||||||
end
|
end
|
||||||
alias serializable_object serializable_array
|
alias serializable_object serializable_array
|
||||||
|
|||||||
24
test/unit/active_model/array_serializer/scope_test.rb
Normal file
24
test/unit/active_model/array_serializer/scope_test.rb
Normal 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
|
||||||
Loading…
Reference in New Issue
Block a user