mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge branch 'Hirurg103-0-10-stable' into 0-10-stable
This commit is contained in:
commit
12724807e9
@ -76,6 +76,7 @@ module ActiveModel
|
|||||||
serializer_options[:serializer_context_class] = association_options.fetch(:parent_serializer).class
|
serializer_options[:serializer_context_class] = association_options.fetch(:parent_serializer).class
|
||||||
serializer = reflection_options.fetch(:serializer, nil)
|
serializer = reflection_options.fetch(:serializer, nil)
|
||||||
serializer_options[:serializer] = serializer if serializer
|
serializer_options[:serializer] = serializer if serializer
|
||||||
|
serializer_options[:namespace] = reflection_options[:namespace] if reflection_options[:namespace]
|
||||||
serializer_class.new(object, serializer_options)
|
serializer_class.new(object, serializer_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -285,6 +285,56 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class AssociationsNamespacedSerializersTest < ActiveSupport::TestCase
|
||||||
|
class Post < ::Model
|
||||||
|
associations :comments, :author, :description
|
||||||
|
|
||||||
|
def latest_comments
|
||||||
|
comments[0..3]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
class Comment < ::Model; end
|
||||||
|
class Author < ::Model; end
|
||||||
|
class Description < ::Model; end
|
||||||
|
|
||||||
|
class ResourceNamespace
|
||||||
|
class PostSerializer < ActiveModel::Serializer
|
||||||
|
has_many :comments, namespace: ResourceNamespace
|
||||||
|
has_many :latest_comments, namespace: ResourceNamespace
|
||||||
|
belongs_to :author, namespace: ResourceNamespace
|
||||||
|
has_one :description, namespace: ResourceNamespace
|
||||||
|
end
|
||||||
|
class CommentSerializer < ActiveModel::Serializer; end
|
||||||
|
class AuthorSerializer < ActiveModel::Serializer; end
|
||||||
|
class DescriptionSerializer < ActiveModel::Serializer; end
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup
|
||||||
|
@comment = Comment.new
|
||||||
|
@author = Author.new
|
||||||
|
@description = Description.new
|
||||||
|
@post = Post.new(comments: [@comment],
|
||||||
|
author: @author,
|
||||||
|
description: @description)
|
||||||
|
@post_serializer = ResourceNamespace::PostSerializer.new(@post)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_associations_namespaced_serializers
|
||||||
|
@post_serializer.associations.each do |association|
|
||||||
|
case association.key
|
||||||
|
when :comments, :latest_comments
|
||||||
|
assert_instance_of(ResourceNamespace::CommentSerializer, association.lazy_association.serializer.first)
|
||||||
|
when :author
|
||||||
|
assert_instance_of(ResourceNamespace::AuthorSerializer, association.lazy_association.serializer)
|
||||||
|
when :description
|
||||||
|
assert_instance_of(ResourceNamespace::DescriptionSerializer, association.lazy_association.serializer)
|
||||||
|
else
|
||||||
|
flunk "Unknown association: #{key}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class NestedSerializersTest < ActiveSupport::TestCase
|
class NestedSerializersTest < ActiveSupport::TestCase
|
||||||
class Post < ::Model
|
class Post < ::Model
|
||||||
associations :comments, :author, :description
|
associations :comments, :author, :description
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user