Merge pull request #897 from imanel/patch-1

Allow to define custom serializer for given class
This commit is contained in:
João Moura 2015-05-10 03:28:14 -03:00
commit 46ae776175
2 changed files with 12 additions and 1 deletions

View File

@ -115,7 +115,9 @@ module ActiveModel
end
def self.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)
config.array_serializer
else
options

View File

@ -29,10 +29,14 @@ module ActiveModel
class SerializerTest < Minitest::Test
class MyProfile < Profile
end
class CustomProfile
def serializer_class; ProfileSerializer; end
end
def setup
@profile = Profile.new
@my_profile = MyProfile.new
@custom_profile = CustomProfile.new
@model = ::Model.new
end
@ -50,6 +54,11 @@ module ActiveModel
serializer = ActiveModel::Serializer.serializer_for(@my_profile)
assert_equal ProfileSerializer, serializer
end
def test_serializer_custom_serializer
serializer = ActiveModel::Serializer.serializer_for(@custom_profile)
assert_equal ProfileSerializer, serializer
end
end
end
end