mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Extract IncludeTree. (#1685)
This commit is contained in:
committed by
L. Preston Sego III
parent
f2cb497fe3
commit
f48fd2a327
@@ -226,19 +226,19 @@ module ActionController
|
||||
}
|
||||
end
|
||||
|
||||
def with_default_includes(include_tree)
|
||||
def with_default_includes(include_directive)
|
||||
original = ActiveModelSerializers.config.default_includes
|
||||
ActiveModelSerializers.config.default_includes = include_tree
|
||||
clear_include_tree_cache
|
||||
ActiveModelSerializers.config.default_includes = include_directive
|
||||
clear_include_directive_cache
|
||||
yield
|
||||
ensure
|
||||
ActiveModelSerializers.config.default_includes = original
|
||||
clear_include_tree_cache
|
||||
clear_include_directive_cache
|
||||
end
|
||||
|
||||
def clear_include_tree_cache
|
||||
def clear_include_directive_cache
|
||||
ActiveModelSerializers
|
||||
.instance_variable_set(:@default_include_tree, nil)
|
||||
.instance_variable_set(:@default_include_directive, nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -286,9 +286,9 @@ module ActiveModelSerializers
|
||||
|
||||
def test_object_cache_keys
|
||||
serializable = ActiveModelSerializers::SerializableResource.new([@comment, @comment])
|
||||
include_tree = ActiveModel::Serializer::IncludeTree.from_include_args('*')
|
||||
include_directive = JSONAPI::IncludeDirective.new('*', allow_wildcard: true)
|
||||
|
||||
actual = ActiveModel::Serializer.object_cache_keys(serializable.adapter.serializer, serializable.adapter, include_tree)
|
||||
actual = ActiveModel::Serializer.object_cache_keys(serializable.adapter.serializer, serializable.adapter, include_directive)
|
||||
|
||||
assert_equal 3, actual.size
|
||||
assert actual.any? { |key| key == "comment/1/#{serializable.adapter.cached_name}" }
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class IncludeTree
|
||||
class FromStringTest < ActiveSupport::TestCase
|
||||
def test_simple_array
|
||||
input = [:comments, :author]
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_include_args(input)
|
||||
assert(actual.key?(:author))
|
||||
assert(actual.key?(:comments))
|
||||
end
|
||||
|
||||
def test_nested_array
|
||||
input = [:comments, posts: [:author, comments: [:author]]]
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_include_args(input)
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:author))
|
||||
assert(actual[:posts].key?(:comments))
|
||||
assert(actual[:posts][:comments].key?(:author))
|
||||
assert(actual.key?(:comments))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,94 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class IncludeTree
|
||||
class FromStringTest < ActiveSupport::TestCase
|
||||
def test_single_string
|
||||
input = 'author'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:author))
|
||||
end
|
||||
|
||||
def test_multiple_strings
|
||||
input = 'author,comments'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:author))
|
||||
assert(actual.key?(:comments))
|
||||
end
|
||||
|
||||
def test_multiple_strings_with_space
|
||||
input = 'author, comments'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:author))
|
||||
assert(actual.key?(:comments))
|
||||
end
|
||||
|
||||
def test_nested_string
|
||||
input = 'posts.author'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:author))
|
||||
end
|
||||
|
||||
def test_multiple_nested_string
|
||||
input = 'posts.author,posts.comments.author,comments'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:author))
|
||||
assert(actual[:posts].key?(:comments))
|
||||
assert(actual[:posts][:comments].key?(:author))
|
||||
assert(actual.key?(:comments))
|
||||
end
|
||||
|
||||
def test_toplevel_star_string
|
||||
input = '*'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:comments))
|
||||
end
|
||||
|
||||
def test_nested_star_string
|
||||
input = 'posts.*'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:comments))
|
||||
end
|
||||
|
||||
def test_nested_star_middle_string
|
||||
input = 'posts.*.author'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:comments))
|
||||
assert(actual[:posts][:comments].key?(:author))
|
||||
refute(actual[:posts][:comments].key?(:unspecified))
|
||||
end
|
||||
|
||||
def test_nested_star_lower_precedence_string
|
||||
input = 'posts.comments.author,posts.*'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:comments))
|
||||
assert(actual[:posts][:comments].key?(:author))
|
||||
end
|
||||
|
||||
def test_toplevel_double_star_string
|
||||
input = '**'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:comments))
|
||||
assert(actual[:posts][:comments].key?(:posts))
|
||||
end
|
||||
|
||||
def test_nested_double_star_string
|
||||
input = 'comments, posts.**'
|
||||
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
||||
assert(actual.key?(:comments))
|
||||
refute(actual[:comments].key?(:author))
|
||||
assert(actual.key?(:posts))
|
||||
assert(actual[:posts].key?(:comments))
|
||||
assert(actual[:posts][:comments].key?(:posts))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,64 +0,0 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class IncludeTree
|
||||
module Parsing
|
||||
class IncludeArgsToHashTest < MiniTest::Test
|
||||
def test_include_args_to_hash_from_symbol
|
||||
expected = { author: {} }
|
||||
input = :author
|
||||
actual = Parsing.include_args_to_hash(input)
|
||||
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_include_args_to_hash_from_array
|
||||
expected = { author: {}, comments: {} }
|
||||
input = [:author, :comments]
|
||||
actual = Parsing.include_args_to_hash(input)
|
||||
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_include_args_to_hash_from_nested_array
|
||||
expected = { author: {}, comments: { author: {} } }
|
||||
input = [:author, comments: [:author]]
|
||||
actual = Parsing.include_args_to_hash(input)
|
||||
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_include_args_to_hash_from_array_of_hashes
|
||||
expected = {
|
||||
author: {},
|
||||
blogs: { posts: { contributors: {} } },
|
||||
comments: { author: { blogs: { posts: {} } } }
|
||||
}
|
||||
input = [
|
||||
:author,
|
||||
blogs: [posts: :contributors],
|
||||
comments: { author: { blogs: :posts } }
|
||||
]
|
||||
actual = Parsing.include_args_to_hash(input)
|
||||
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_array_of_string
|
||||
expected = {
|
||||
comments: { author: {}, attachment: {} }
|
||||
}
|
||||
input = [
|
||||
'comments.author',
|
||||
'comments.attachment'
|
||||
]
|
||||
actual = Parsing.include_args_to_hash(input)
|
||||
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user