mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Support customer array serializer
This commit is contained in:
parent
be6ff586a2
commit
c389ae2207
@ -58,7 +58,11 @@ end
|
||||
if RUBY_VERSION >= '2.0'
|
||||
def serializer_for(resource)
|
||||
if resource.respond_to?(:to_ary)
|
||||
ArraySerializer
|
||||
if Object.constants.include?(:ArraySerializer)
|
||||
::ArraySerializer
|
||||
else
|
||||
ArraySerializer
|
||||
end
|
||||
else
|
||||
begin
|
||||
Object.const_get "#{resource.class.name}Serializer"
|
||||
@ -70,7 +74,11 @@ end
|
||||
else
|
||||
def serializer_for(resource)
|
||||
if resource.respond_to?(:to_ary)
|
||||
ArraySerializer
|
||||
if Object.constants.include?(:ArraySerializer)
|
||||
::ArraySerializer
|
||||
else
|
||||
ArraySerializer
|
||||
end
|
||||
else
|
||||
"#{resource.class.name}Serializer".safe_constantize
|
||||
end
|
||||
|
||||
@ -9,7 +9,7 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_serializer_for_array_returns_appropriate_type
|
||||
assert_kind_of ArraySerializer, @serializer
|
||||
assert_kind_of ActiveModel::ArraySerializer, @serializer
|
||||
end
|
||||
|
||||
def test_array_serializer_serializes_simple_objects
|
||||
@ -18,6 +18,23 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
class CustomArraySerializerSupport < Minitest::Test
|
||||
def setup
|
||||
Object.const_set(:ArraySerializer, Class.new{ def initialize(*); end })
|
||||
|
||||
array = [1, 2, 3]
|
||||
@serializer = Serializer.serializer_for(array).new(array)
|
||||
end
|
||||
|
||||
def teardown
|
||||
Object.send(:remove_const, :ArraySerializer)
|
||||
end
|
||||
|
||||
def test_serializer_for_array_returns_appropriate_type
|
||||
assert_kind_of ::ArraySerializer, @serializer
|
||||
end
|
||||
end
|
||||
|
||||
class ModelSerializationTest < Minitest::Test
|
||||
def test_array_serializer_serializes_models
|
||||
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user