Merge pull request #167 from GateGuru/feature/specify-association-serializers-as-strings

Add support for specifying the serializer for an association as a String...
This commit is contained in:
Steve Klabnik 2013-03-05 15:04:29 -08:00
commit 95937c6fc7
2 changed files with 24 additions and 1 deletions

View File

@ -39,7 +39,8 @@ module ActiveModel
end
def target_serializer
option(:serializer)
serializer = option(:serializer)
serializer.is_a?(String) ? serializer.constantize : serializer
end
def source_serializer

View File

@ -392,4 +392,26 @@ class AssociationTest < ActiveModel::TestCase
}, json)
end
end
class StringSerializerOption < AssociationTest
class StringSerializer < ActiveModel::Serializer
attributes :id, :body
end
def test_specifying_serializer_class_as_string
@post_serializer_class.class_eval do
has_many :comments, :embed => :objects
end
include_bare! :comments, :serializer => "AssociationTest::StringSerializerOption::StringSerializer"
assert_equal({
:comments => [
{ :id => 1, :body => "ZOMG A COMMENT" }
]
}, @hash)
assert_equal({}, @root_hash)
end
end
end