active_model_serializers 0.10.0 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +31 -2
- data/Gemfile +1 -1
- data/README.md +21 -24
- data/active_model_serializers.gemspec +4 -8
- data/docs/general/adapters.md +4 -2
- data/docs/general/configuration_options.md +6 -1
- data/docs/general/deserialization.md +1 -1
- data/docs/general/serializers.md +30 -3
- data/docs/jsonapi/schema.md +1 -1
- data/lib/active_model/serializer.rb +54 -11
- data/lib/active_model/serializer/adapter/base.rb +2 -0
- data/lib/active_model/serializer/associations.rb +4 -5
- data/lib/active_model/serializer/belongs_to_reflection.rb +0 -3
- data/lib/active_model/serializer/caching.rb +62 -110
- data/lib/active_model/serializer/collection_serializer.rb +30 -10
- data/lib/active_model/serializer/configuration.rb +1 -0
- data/lib/active_model/serializer/has_many_reflection.rb +0 -3
- data/lib/active_model/serializer/has_one_reflection.rb +0 -3
- data/lib/active_model/serializer/reflection.rb +3 -3
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +6 -0
- data/lib/active_model_serializers/adapter.rb +6 -0
- data/lib/active_model_serializers/adapter/attributes.rb +2 -67
- data/lib/active_model_serializers/adapter/base.rb +38 -38
- data/lib/active_model_serializers/adapter/json_api.rb +36 -28
- data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
- data/lib/active_model_serializers/deprecate.rb +1 -2
- data/lib/active_model_serializers/deserialization.rb +2 -0
- data/lib/active_model_serializers/model.rb +2 -0
- data/lib/active_model_serializers/railtie.rb +2 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +34 -23
- data/lib/active_model_serializers/serialization_context.rb +10 -3
- data/lib/grape/formatters/active_model_serializers.rb +19 -2
- data/lib/grape/helpers/active_model_serializers.rb +1 -0
- data/test/action_controller/adapter_selector_test.rb +1 -1
- data/test/action_controller/explicit_serializer_test.rb +5 -4
- data/test/action_controller/json/include_test.rb +106 -27
- data/test/action_controller/json_api/errors_test.rb +2 -2
- data/test/action_controller/json_api/linked_test.rb +26 -21
- data/test/action_controller/serialization_test.rb +9 -6
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +143 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
- data/test/adapter/json/collection_test.rb +14 -0
- data/test/adapter/json_api/collection_test.rb +4 -3
- data/test/adapter/json_api/errors_test.rb +13 -15
- data/test/adapter/json_api/linked_test.rb +8 -5
- data/test/adapter/json_api/links_test.rb +3 -1
- data/test/adapter/json_api/pagination_links_test.rb +13 -1
- data/test/adapter/json_api/relationships_test.rb +9 -4
- data/test/adapter/json_api/resource_identifier_test.rb +7 -2
- data/test/adapter/json_api/transform_test.rb +76 -75
- data/test/adapter/json_test.rb +4 -3
- data/test/benchmark/app.rb +1 -1
- data/test/benchmark/bm_caching.rb +14 -14
- data/test/benchmark/bm_transform.rb +16 -5
- data/test/benchmark/controllers.rb +16 -17
- data/test/benchmark/fixtures.rb +72 -72
- data/test/cache_test.rb +73 -45
- data/test/fixtures/poro.rb +6 -5
- data/test/grape_test.rb +96 -2
- data/test/serializable_resource_test.rb +12 -12
- data/test/serializers/meta_test.rb +12 -6
- data/test/support/isolated_unit.rb +1 -0
- data/test/support/rails5_shims.rb +8 -2
- data/test/support/rails_app.rb +0 -9
- metadata +53 -23
- data/lib/active_model/serializer/include_tree.rb +0 -111
- data/test/include_tree/from_include_args_test.rb +0 -26
- data/test/include_tree/from_string_test.rb +0 -94
- data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -1,111 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Serializer
|
3
|
-
# TODO: description of this class, and overview of how it's used
|
4
|
-
class IncludeTree
|
5
|
-
module Parsing
|
6
|
-
module_function
|
7
|
-
|
8
|
-
# Translates a comma separated list of dot separated paths (JSON API format) into a Hash.
|
9
|
-
#
|
10
|
-
# @example
|
11
|
-
# `'posts.author, posts.comments.upvotes, posts.comments.author'`
|
12
|
-
#
|
13
|
-
# would become
|
14
|
-
#
|
15
|
-
# `{ posts: { author: {}, comments: { author: {}, upvotes: {} } } }`.
|
16
|
-
#
|
17
|
-
# @param [String] included
|
18
|
-
# @return [Hash] a Hash representing the same tree structure
|
19
|
-
def include_string_to_hash(included)
|
20
|
-
# TODO: Needs comment walking through the process of what all this is doing.
|
21
|
-
included.delete(' ').split(',').reduce({}) do |hash, path|
|
22
|
-
include_tree = path.split('.').reverse_each.reduce({}) { |a, e| { e.to_sym => a } }
|
23
|
-
hash.deep_merge!(include_tree)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
# Translates the arguments passed to the include option into a Hash. The format can be either
|
28
|
-
# a String (see #include_string_to_hash), an Array of Symbols and Hashes, or a mix of both.
|
29
|
-
#
|
30
|
-
# @example
|
31
|
-
# `posts: [:author, comments: [:author, :upvotes]]`
|
32
|
-
#
|
33
|
-
# would become
|
34
|
-
#
|
35
|
-
# `{ posts: { author: {}, comments: { author: {}, upvotes: {} } } }`.
|
36
|
-
#
|
37
|
-
# @example
|
38
|
-
# `[:author, :comments => [:author]]`
|
39
|
-
#
|
40
|
-
# would become
|
41
|
-
#
|
42
|
-
# `{:author => {}, :comments => { author: {} } }`
|
43
|
-
#
|
44
|
-
# @param [Symbol, Hash, Array, String] included
|
45
|
-
# @return [Hash] a Hash representing the same tree structure
|
46
|
-
def include_args_to_hash(included)
|
47
|
-
case included
|
48
|
-
when Symbol
|
49
|
-
{ included => {} }
|
50
|
-
when Hash
|
51
|
-
included.each_with_object({}) do |(key, value), hash|
|
52
|
-
hash[key] = include_args_to_hash(value)
|
53
|
-
end
|
54
|
-
when Array
|
55
|
-
included.reduce({}) { |a, e| a.deep_merge!(include_args_to_hash(e)) }
|
56
|
-
when String
|
57
|
-
include_string_to_hash(included)
|
58
|
-
else
|
59
|
-
{}
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
# Builds an IncludeTree from a comma separated list of dot separated paths (JSON API format).
|
65
|
-
# @example `'posts.author, posts.comments.upvotes, posts.comments.author'`
|
66
|
-
#
|
67
|
-
# @param [String] included
|
68
|
-
# @return [IncludeTree]
|
69
|
-
#
|
70
|
-
def self.from_string(included)
|
71
|
-
new(Parsing.include_string_to_hash(included))
|
72
|
-
end
|
73
|
-
|
74
|
-
# Translates the arguments passed to the include option into an IncludeTree.
|
75
|
-
# The format can be either a String (see #from_string), an Array of Symbols and Hashes, or a mix of both.
|
76
|
-
# @example `posts: [:author, comments: [:author, :upvotes]]`
|
77
|
-
#
|
78
|
-
# @param [Symbol, Hash, Array, String] included
|
79
|
-
# @return [IncludeTree]
|
80
|
-
#
|
81
|
-
def self.from_include_args(included)
|
82
|
-
return included if included.is_a?(IncludeTree)
|
83
|
-
|
84
|
-
new(Parsing.include_args_to_hash(included))
|
85
|
-
end
|
86
|
-
|
87
|
-
# @param [Hash] hash
|
88
|
-
def initialize(hash = {})
|
89
|
-
@hash = hash
|
90
|
-
end
|
91
|
-
|
92
|
-
def key?(key)
|
93
|
-
@hash.key?(key) || @hash.key?(:*) || @hash.key?(:**)
|
94
|
-
end
|
95
|
-
|
96
|
-
def [](key)
|
97
|
-
# TODO(beauby): Adopt a lazy caching strategy for generating subtrees.
|
98
|
-
case
|
99
|
-
when @hash.key?(key)
|
100
|
-
self.class.new(@hash[key])
|
101
|
-
when @hash.key?(:*)
|
102
|
-
self.class.new(@hash[:*])
|
103
|
-
when @hash.key?(:**)
|
104
|
-
self.class.new(:** => {})
|
105
|
-
else
|
106
|
-
nil
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class IncludeTree
|
6
|
-
class FromStringTest < ActiveSupport::TestCase
|
7
|
-
def test_simple_array
|
8
|
-
input = [:comments, :author]
|
9
|
-
actual = ActiveModel::Serializer::IncludeTree.from_include_args(input)
|
10
|
-
assert(actual.key?(:author))
|
11
|
-
assert(actual.key?(:comments))
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_nested_array
|
15
|
-
input = [:comments, posts: [:author, comments: [:author]]]
|
16
|
-
actual = ActiveModel::Serializer::IncludeTree.from_include_args(input)
|
17
|
-
assert(actual.key?(:posts))
|
18
|
-
assert(actual[:posts].key?(:author))
|
19
|
-
assert(actual[:posts].key?(:comments))
|
20
|
-
assert(actual[:posts][:comments].key?(:author))
|
21
|
-
assert(actual.key?(:comments))
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,94 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class IncludeTree
|
6
|
-
class FromStringTest < ActiveSupport::TestCase
|
7
|
-
def test_single_string
|
8
|
-
input = 'author'
|
9
|
-
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
10
|
-
assert(actual.key?(:author))
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_multiple_strings
|
14
|
-
input = 'author,comments'
|
15
|
-
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
16
|
-
assert(actual.key?(:author))
|
17
|
-
assert(actual.key?(:comments))
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_multiple_strings_with_space
|
21
|
-
input = 'author, comments'
|
22
|
-
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
23
|
-
assert(actual.key?(:author))
|
24
|
-
assert(actual.key?(:comments))
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_nested_string
|
28
|
-
input = 'posts.author'
|
29
|
-
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
30
|
-
assert(actual.key?(:posts))
|
31
|
-
assert(actual[:posts].key?(:author))
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_multiple_nested_string
|
35
|
-
input = 'posts.author,posts.comments.author,comments'
|
36
|
-
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
37
|
-
assert(actual.key?(:posts))
|
38
|
-
assert(actual[:posts].key?(:author))
|
39
|
-
assert(actual[:posts].key?(:comments))
|
40
|
-
assert(actual[:posts][:comments].key?(:author))
|
41
|
-
assert(actual.key?(:comments))
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_toplevel_star_string
|
45
|
-
input = '*'
|
46
|
-
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
47
|
-
assert(actual.key?(:comments))
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_nested_star_string
|
51
|
-
input = 'posts.*'
|
52
|
-
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
53
|
-
assert(actual.key?(:posts))
|
54
|
-
assert(actual[:posts].key?(:comments))
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_nested_star_middle_string
|
58
|
-
input = 'posts.*.author'
|
59
|
-
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
60
|
-
assert(actual.key?(:posts))
|
61
|
-
assert(actual[:posts].key?(:comments))
|
62
|
-
assert(actual[:posts][:comments].key?(:author))
|
63
|
-
refute(actual[:posts][:comments].key?(:unspecified))
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_nested_star_lower_precedence_string
|
67
|
-
input = 'posts.comments.author,posts.*'
|
68
|
-
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
69
|
-
assert(actual.key?(:posts))
|
70
|
-
assert(actual[:posts].key?(:comments))
|
71
|
-
assert(actual[:posts][:comments].key?(:author))
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_toplevel_double_star_string
|
75
|
-
input = '**'
|
76
|
-
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
77
|
-
assert(actual.key?(:posts))
|
78
|
-
assert(actual[:posts].key?(:comments))
|
79
|
-
assert(actual[:posts][:comments].key?(:posts))
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_nested_double_star_string
|
83
|
-
input = 'comments, posts.**'
|
84
|
-
actual = ActiveModel::Serializer::IncludeTree.from_string(input)
|
85
|
-
assert(actual.key?(:comments))
|
86
|
-
refute(actual[:comments].key?(:author))
|
87
|
-
assert(actual.key?(:posts))
|
88
|
-
assert(actual[:posts].key?(:comments))
|
89
|
-
assert(actual[:posts][:comments].key?(:posts))
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
@@ -1,64 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class IncludeTree
|
6
|
-
module Parsing
|
7
|
-
class IncludeArgsToHashTest < MiniTest::Test
|
8
|
-
def test_include_args_to_hash_from_symbol
|
9
|
-
expected = { author: {} }
|
10
|
-
input = :author
|
11
|
-
actual = Parsing.include_args_to_hash(input)
|
12
|
-
|
13
|
-
assert_equal(expected, actual)
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_include_args_to_hash_from_array
|
17
|
-
expected = { author: {}, comments: {} }
|
18
|
-
input = [:author, :comments]
|
19
|
-
actual = Parsing.include_args_to_hash(input)
|
20
|
-
|
21
|
-
assert_equal(expected, actual)
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_include_args_to_hash_from_nested_array
|
25
|
-
expected = { author: {}, comments: { author: {} } }
|
26
|
-
input = [:author, comments: [:author]]
|
27
|
-
actual = Parsing.include_args_to_hash(input)
|
28
|
-
|
29
|
-
assert_equal(expected, actual)
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_include_args_to_hash_from_array_of_hashes
|
33
|
-
expected = {
|
34
|
-
author: {},
|
35
|
-
blogs: { posts: { contributors: {} } },
|
36
|
-
comments: { author: { blogs: { posts: {} } } }
|
37
|
-
}
|
38
|
-
input = [
|
39
|
-
:author,
|
40
|
-
blogs: [posts: :contributors],
|
41
|
-
comments: { author: { blogs: :posts } }
|
42
|
-
]
|
43
|
-
actual = Parsing.include_args_to_hash(input)
|
44
|
-
|
45
|
-
assert_equal(expected, actual)
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_array_of_string
|
49
|
-
expected = {
|
50
|
-
comments: { author: {}, attachment: {} }
|
51
|
-
}
|
52
|
-
input = [
|
53
|
-
'comments.author',
|
54
|
-
'comments.attachment'
|
55
|
-
]
|
56
|
-
actual = Parsing.include_args_to_hash(input)
|
57
|
-
|
58
|
-
assert_equal(expected, actual)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|