mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Array serializer pass except and only options to item serializers
This commit is contained in:
parent
be6ff586a2
commit
e3ab34dd74
@ -19,6 +19,8 @@ module ActiveModel
|
|||||||
@meta = options[@meta_key]
|
@meta = options[@meta_key]
|
||||||
@each_serializer = options[:each_serializer]
|
@each_serializer = options[:each_serializer]
|
||||||
@resource_name = options[:resource_name]
|
@resource_name = options[:resource_name]
|
||||||
|
@only = options[:only] ? Array(options[:only]) : nil
|
||||||
|
@except = options[:except] ? Array(options[:except]) : nil
|
||||||
@key_format = options[:key_format] || options[:each_serializer].try(:key_format)
|
@key_format = options[:key_format] || options[:each_serializer].try(:key_format)
|
||||||
end
|
end
|
||||||
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format
|
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format
|
||||||
@ -30,7 +32,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def serializer_for(item)
|
def serializer_for(item)
|
||||||
serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
||||||
serializer_class.new(item, scope: scope, key_format: key_format)
|
serializer_class.new(item, scope: scope, key_format: key_format, only: @only, except: @except)
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_object
|
def serializable_object
|
||||||
|
|||||||
18
test/unit/active_model/array_serializer/except_test.rb
Normal file
18
test/unit/active_model/array_serializer/except_test.rb
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class ArraySerializer
|
||||||
|
class ExceptTest < Minitest::Test
|
||||||
|
def test_array_serializer_pass_except_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, except: [:description])
|
||||||
|
|
||||||
|
expected = [{ name: 'Name 1' },
|
||||||
|
{ name: 'Name 2' }]
|
||||||
|
|
||||||
|
assert_equal expected, serializer.serializable_array
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
18
test/unit/active_model/array_serializer/only_test.rb
Normal file
18
test/unit/active_model/array_serializer/only_test.rb
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class ArraySerializer
|
||||||
|
class OnlyTest < Minitest::Test
|
||||||
|
def test_array_serializer_pass_only_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, only: [:name])
|
||||||
|
|
||||||
|
expected = [{ name: 'Name 1' },
|
||||||
|
{ name: 'Name 2' }]
|
||||||
|
|
||||||
|
assert_equal expected, serializer.serializable_array
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user