Merge pull request #1225 from beauby/nested-serializer-lookup

Add support for nested serializers
This commit is contained in:
L. Preston Sego III
2015-10-09 10:12:08 -04:00
7 changed files with 160 additions and 8 deletions

View File

@@ -125,6 +125,88 @@ module ActiveModel
assert expected_association_keys.include? :writer
assert expected_association_keys.include? :site
end
class NamespacedResourcesTest < Minitest::Test
class ResourceNamespace
Post = Class.new(::Model)
Comment = Class.new(::Model)
Author = Class.new(::Model)
Description = Class.new(::Model)
class PostSerializer < ActiveModel::Serializer
has_many :comments
belongs_to :author
has_one :description
end
CommentSerializer = Class.new(ActiveModel::Serializer)
AuthorSerializer = Class.new(ActiveModel::Serializer)
DescriptionSerializer = Class.new(ActiveModel::Serializer)
end
def setup
@comment = ResourceNamespace::Comment.new
@author = ResourceNamespace::Author.new
@description = ResourceNamespace::Description.new
@post = ResourceNamespace::Post.new(comments: [@comment],
author: @author,
description: @description)
@post_serializer = ResourceNamespace::PostSerializer.new(@post)
end
def test_associations_namespaced_resources
@post_serializer.associations.each do |association|
case association.key
when :comments
assert_instance_of(ResourceNamespace::CommentSerializer, association.serializer.first)
when :author
assert_instance_of(ResourceNamespace::AuthorSerializer, association.serializer)
when :description
assert_instance_of(ResourceNamespace::DescriptionSerializer, association.serializer)
else
flunk "Unknown association: #{key}"
end
end
end
end
class NestedSerializersTest < Minitest::Test
Post = Class.new(::Model)
Comment = Class.new(::Model)
Author = Class.new(::Model)
Description = Class.new(::Model)
class PostSerializer < ActiveModel::Serializer
has_many :comments
CommentSerializer = Class.new(ActiveModel::Serializer)
belongs_to :author
AuthorSerializer = Class.new(ActiveModel::Serializer)
has_one :description
DescriptionSerializer = Class.new(ActiveModel::Serializer)
end
def setup
@comment = Comment.new
@author = Author.new
@description = Description.new
@post = Post.new(comments: [@comment],
author: @author,
description: @description)
@post_serializer = PostSerializer.new(@post)
end
def test_associations_namespaced_resources
@post_serializer.associations.each do |association|
case association.key
when :comments
assert_instance_of(PostSerializer::CommentSerializer, association.serializer.first)
when :author
assert_instance_of(PostSerializer::AuthorSerializer, association.serializer)
when :description
assert_instance_of(PostSerializer::DescriptionSerializer, association.serializer)
else
flunk "Unknown association: #{key}"
end
end
end
end
end
end
end

View File

@@ -27,8 +27,19 @@ module ActiveModel
end
class SerializerTest < Minitest::Test
module ResourceNamespace
Post = Class.new(::Model)
Comment = Class.new(::Model)
class PostSerializer < ActiveModel::Serializer
class CommentSerializer < ActiveModel::Serializer
end
end
end
class MyProfile < Profile
end
class CustomProfile
def serializer_class; ProfileSerializer; end
end
@@ -59,6 +70,18 @@ module ActiveModel
serializer = ActiveModel::Serializer.serializer_for(@custom_profile)
assert_equal ProfileSerializer, serializer
end
def test_serializer_for_namespaced_resource
post = ResourceNamespace::Post.new
serializer = ActiveModel::Serializer.serializer_for(post)
assert_equal(ResourceNamespace::PostSerializer, serializer)
end
def test_serializer_for_nested_resource
comment = ResourceNamespace::Comment.new
serializer = ResourceNamespace::PostSerializer.serializer_for(comment)
assert_equal(ResourceNamespace::PostSerializer::CommentSerializer, serializer)
end
end
end
end