mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge pull request #813 from jtomaszewski/feature/custom-serializer-class
Allow to define custom serializer for given class
This commit is contained in:
commit
55210ab61f
@ -56,7 +56,9 @@ end
|
|||||||
attr_reader :key_format
|
attr_reader :key_format
|
||||||
|
|
||||||
def serializer_for(resource, options = {})
|
def serializer_for(resource, options = {})
|
||||||
if resource.respond_to?(:to_ary)
|
if resource.respond_to?(:serializer_class)
|
||||||
|
resource.serializer_class
|
||||||
|
elsif resource.respond_to?(:to_ary)
|
||||||
if Object.constants.include?(:ArraySerializer)
|
if Object.constants.include?(:ArraySerializer)
|
||||||
::ArraySerializer
|
::ArraySerializer
|
||||||
else
|
else
|
||||||
|
|||||||
@ -35,6 +35,23 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class CustomSerializerClassTest < Minitest::Test
|
||||||
|
def setup
|
||||||
|
Object.const_set(:CustomSerializer, Class.new)
|
||||||
|
end
|
||||||
|
|
||||||
|
def teardown
|
||||||
|
Object.send(:remove_const, :CustomSerializer)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_serializer_for_array_returns_appropriate_type
|
||||||
|
object = {}
|
||||||
|
def object.serializer_class; CustomSerializer; end
|
||||||
|
|
||||||
|
assert_equal CustomSerializer, Serializer.serializer_for(object)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class ModelSerializationTest < Minitest::Test
|
class ModelSerializationTest < Minitest::Test
|
||||||
def test_array_serializer_serializes_models
|
def test_array_serializer_serializes_models
|
||||||
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user