mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
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:
commit
95937c6fc7
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user