mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Associations refactoring
* Move all associations related code from Serializer class to Associations module * Introduce Reflection class hierarchy * Introduce Association class * Rid off Serializer#each_association * Introduce Serializer#associations enumerator
This commit is contained in:
36
test/serializers/association_macros_test.rb
Normal file
36
test/serializers/association_macros_test.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class AssociationMacrosTest < Minitest::Test
|
||||
AuthorSummarySerializer = Class.new
|
||||
class AssociationsTestSerializer < Serializer
|
||||
belongs_to :author, serializer: AuthorSummarySerializer
|
||||
has_many :comments, embed: :ids
|
||||
has_one :category
|
||||
end
|
||||
|
||||
def before_setup
|
||||
@reflections = AssociationsTestSerializer._reflections
|
||||
end
|
||||
|
||||
def test_has_one_defines_reflection
|
||||
has_one_reflection = HasOneReflection.new(:category, {})
|
||||
|
||||
assert_includes(@reflections, has_one_reflection)
|
||||
end
|
||||
|
||||
def test_has_many_defines_reflection
|
||||
has_many_reflection = HasManyReflection.new(:comments, embed: :ids)
|
||||
|
||||
assert_includes(@reflections, has_many_reflection)
|
||||
end
|
||||
|
||||
def test_belongs_to_defines_reflection
|
||||
belongs_to_reflection = BelongsToReflection.new(:author, serializer: AuthorSummarySerializer)
|
||||
|
||||
assert_includes(@reflections, belongs_to_reflection)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -45,21 +45,20 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_has_many_and_has_one
|
||||
assert_equal(
|
||||
{ posts: { type: :has_many, association_options: { embed: :ids } },
|
||||
roles: { type: :has_many, association_options: { embed: :ids } },
|
||||
bio: { type: :has_one, association_options: {} } },
|
||||
@author_serializer.class._associations
|
||||
)
|
||||
@author_serializer.each_association do |key, serializer, options|
|
||||
if key == :posts
|
||||
assert_equal({embed: :ids}, options)
|
||||
@author_serializer.associations.each do |association|
|
||||
key = association.key
|
||||
serializer = association.serializer
|
||||
options = association.options
|
||||
|
||||
case key
|
||||
when :posts
|
||||
assert_equal({ embed: :ids }, options)
|
||||
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
||||
elsif key == :bio
|
||||
when :bio
|
||||
assert_equal({}, options)
|
||||
assert_nil serializer
|
||||
elsif key == :roles
|
||||
assert_equal({embed: :ids}, options)
|
||||
when :roles
|
||||
assert_equal({ embed: :ids }, options)
|
||||
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
|
||||
else
|
||||
flunk "Unknown association: #{key}"
|
||||
@@ -68,7 +67,11 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_has_many_with_no_serializer
|
||||
PostWithTagsSerializer.new(@post).each_association do |key, serializer, options|
|
||||
PostWithTagsSerializer.new(@post).associations.each do |association|
|
||||
key = association.key
|
||||
serializer = association.serializer
|
||||
options = association.options
|
||||
|
||||
assert_equal key, :tags
|
||||
assert_equal serializer, nil
|
||||
assert_equal [{ attributes: { name: "#hashtagged" }}].to_json, options[:virtual_value].to_json
|
||||
@@ -76,70 +79,67 @@ module ActiveModel
|
||||
end
|
||||
|
||||
def test_serializer_options_are_passed_into_associations_serializers
|
||||
@post_serializer.each_association do |key, association|
|
||||
if key == :comments
|
||||
assert association.first.custom_options[:custom_options]
|
||||
end
|
||||
end
|
||||
association = @post_serializer
|
||||
.associations
|
||||
.detect { |association| association.key == :comments }
|
||||
|
||||
assert association.serializer.first.custom_options[:custom_options]
|
||||
end
|
||||
|
||||
def test_belongs_to
|
||||
assert_equal(
|
||||
{ post: { type: :belongs_to, association_options: {} },
|
||||
author: { type: :belongs_to, association_options: {} } },
|
||||
@comment_serializer.class._associations
|
||||
)
|
||||
@comment_serializer.each_association do |key, serializer, options|
|
||||
if key == :post
|
||||
assert_equal({}, options)
|
||||
@comment_serializer.associations.each do |association|
|
||||
key = association.key
|
||||
serializer = association.serializer
|
||||
|
||||
case key
|
||||
when :post
|
||||
assert_kind_of(PostSerializer, serializer)
|
||||
elsif key == :author
|
||||
assert_equal({}, options)
|
||||
when :author
|
||||
assert_nil serializer
|
||||
else
|
||||
flunk "Unknown association: #{key}"
|
||||
end
|
||||
|
||||
assert_equal({}, association.options)
|
||||
end
|
||||
end
|
||||
|
||||
def test_belongs_to_with_custom_method
|
||||
blog_is_present = false
|
||||
|
||||
@post_serializer.each_association do |key, serializer, options|
|
||||
blog_is_present = true if key == :blog
|
||||
end
|
||||
|
||||
assert blog_is_present
|
||||
assert(
|
||||
@post_serializer.associations.any? do |association|
|
||||
association.key == :blog
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
def test_associations_inheritance
|
||||
inherited_klass = Class.new(PostSerializer)
|
||||
|
||||
assert_equal(PostSerializer._associations, inherited_klass._associations)
|
||||
assert_equal(PostSerializer._reflections, inherited_klass._reflections)
|
||||
end
|
||||
|
||||
def test_associations_inheritance_with_new_association
|
||||
inherited_klass = Class.new(PostSerializer) do
|
||||
has_many :top_comments, serializer: CommentSerializer
|
||||
end
|
||||
expected_associations = PostSerializer._associations.merge(
|
||||
top_comments: {
|
||||
type: :has_many,
|
||||
association_options: {
|
||||
serializer: CommentSerializer
|
||||
}
|
||||
}
|
||||
|
||||
assert(
|
||||
PostSerializer._reflections.all? do |reflection|
|
||||
inherited_klass._reflections.include?(reflection)
|
||||
end
|
||||
)
|
||||
|
||||
assert(
|
||||
inherited_klass._reflections.any? do |reflection|
|
||||
reflection.name == :top_comments
|
||||
end
|
||||
)
|
||||
assert_equal(inherited_klass._associations, expected_associations)
|
||||
end
|
||||
|
||||
def test_associations_custom_keys
|
||||
serializer = PostWithCustomKeysSerializer.new(@post)
|
||||
|
||||
expected_association_keys = []
|
||||
serializer.each_association do |key, serializer, options|
|
||||
expected_association_keys << key
|
||||
end
|
||||
expected_association_keys = serializer.associations.map(&:key)
|
||||
|
||||
assert expected_association_keys.include? :reviews
|
||||
assert expected_association_keys.include? :writer
|
||||
|
||||
Reference in New Issue
Block a user