active_model_serializers 0.10.0 → 0.10.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -4
  3. data/.travis.yml +9 -1
  4. data/CHANGELOG.md +81 -2
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +5 -2
  7. data/README.md +24 -24
  8. data/Rakefile +3 -3
  9. data/active_model_serializers.gemspec +20 -24
  10. data/docs/ARCHITECTURE.md +6 -7
  11. data/docs/README.md +2 -0
  12. data/docs/general/adapters.md +4 -2
  13. data/docs/general/caching.md +7 -1
  14. data/docs/general/configuration_options.md +70 -1
  15. data/docs/general/deserialization.md +1 -1
  16. data/docs/general/fields.md +31 -0
  17. data/docs/general/rendering.md +42 -3
  18. data/docs/general/serializers.md +97 -8
  19. data/docs/howto/add_pagination_links.md +4 -5
  20. data/docs/howto/add_relationship_links.md +137 -0
  21. data/docs/howto/add_root_key.md +4 -0
  22. data/docs/howto/grape_integration.md +42 -0
  23. data/docs/howto/outside_controller_use.md +9 -2
  24. data/docs/howto/passing_arbitrary_options.md +2 -2
  25. data/docs/howto/test.md +2 -0
  26. data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
  27. data/docs/integrations/ember-and-json-api.md +64 -32
  28. data/docs/jsonapi/schema.md +1 -1
  29. data/lib/action_controller/serialization.rb +13 -3
  30. data/lib/active_model/serializer/adapter/base.rb +2 -0
  31. data/lib/active_model/serializer/array_serializer.rb +8 -5
  32. data/lib/active_model/serializer/association.rb +19 -4
  33. data/lib/active_model/serializer/belongs_to_reflection.rb +0 -3
  34. data/lib/active_model/serializer/collection_serializer.rb +35 -12
  35. data/lib/active_model/serializer/{associations.rb → concerns/associations.rb} +13 -11
  36. data/lib/active_model/serializer/{attributes.rb → concerns/attributes.rb} +0 -0
  37. data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +72 -113
  38. data/lib/active_model/serializer/{configuration.rb → concerns/configuration.rb} +25 -1
  39. data/lib/active_model/serializer/{links.rb → concerns/links.rb} +0 -0
  40. data/lib/active_model/serializer/{meta.rb → concerns/meta.rb} +0 -0
  41. data/lib/active_model/serializer/{type.rb → concerns/type.rb} +0 -0
  42. data/lib/active_model/serializer/error_serializer.rb +11 -7
  43. data/lib/active_model/serializer/errors_serializer.rb +25 -20
  44. data/lib/active_model/serializer/has_many_reflection.rb +0 -3
  45. data/lib/active_model/serializer/has_one_reflection.rb +0 -3
  46. data/lib/active_model/serializer/lint.rb +134 -130
  47. data/lib/active_model/serializer/reflection.rb +37 -21
  48. data/lib/active_model/serializer/version.rb +1 -1
  49. data/lib/active_model/serializer.rb +76 -37
  50. data/lib/active_model_serializers/adapter/attributes.rb +3 -66
  51. data/lib/active_model_serializers/adapter/base.rb +38 -38
  52. data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
  53. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
  54. data/lib/active_model_serializers/adapter/json_api/relationship.rb +30 -19
  55. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +23 -9
  56. data/lib/active_model_serializers/adapter/json_api.rb +44 -43
  57. data/lib/active_model_serializers/adapter.rb +6 -0
  58. data/lib/active_model_serializers/deprecate.rb +1 -2
  59. data/lib/active_model_serializers/deserialization.rb +2 -0
  60. data/lib/active_model_serializers/key_transform.rb +4 -0
  61. data/lib/active_model_serializers/lookup_chain.rb +80 -0
  62. data/lib/active_model_serializers/model.rb +4 -2
  63. data/lib/active_model_serializers/railtie.rb +3 -1
  64. data/lib/active_model_serializers/register_jsonapi_renderer.rb +44 -31
  65. data/lib/active_model_serializers/serializable_resource.rb +6 -5
  66. data/lib/active_model_serializers/serialization_context.rb +10 -3
  67. data/lib/active_model_serializers.rb +7 -0
  68. data/lib/generators/rails/serializer_generator.rb +4 -4
  69. data/lib/grape/active_model_serializers.rb +7 -5
  70. data/lib/grape/formatters/active_model_serializers.rb +19 -2
  71. data/lib/grape/helpers/active_model_serializers.rb +1 -0
  72. data/test/action_controller/adapter_selector_test.rb +4 -4
  73. data/test/action_controller/explicit_serializer_test.rb +5 -4
  74. data/test/action_controller/json/include_test.rb +106 -27
  75. data/test/action_controller/json_api/errors_test.rb +6 -7
  76. data/test/action_controller/json_api/fields_test.rb +57 -0
  77. data/test/action_controller/json_api/linked_test.rb +29 -24
  78. data/test/action_controller/json_api/pagination_test.rb +19 -19
  79. data/test/action_controller/json_api/transform_test.rb +3 -3
  80. data/test/action_controller/lookup_proc_test.rb +49 -0
  81. data/test/action_controller/namespace_lookup_test.rb +226 -0
  82. data/test/action_controller/serialization_test.rb +10 -7
  83. data/test/active_model_serializers/json_pointer_test.rb +15 -13
  84. data/test/active_model_serializers/key_transform_test.rb +286 -252
  85. data/test/active_model_serializers/model_test.rb +17 -4
  86. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +143 -0
  87. data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
  88. data/test/adapter/attributes_test.rb +43 -0
  89. data/test/adapter/json/collection_test.rb +14 -0
  90. data/test/adapter/json/transform_test.rb +15 -15
  91. data/test/adapter/json_api/collection_test.rb +4 -3
  92. data/test/adapter/json_api/errors_test.rb +17 -19
  93. data/test/adapter/json_api/fields_test.rb +4 -3
  94. data/test/adapter/json_api/has_many_test.rb +39 -18
  95. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +166 -0
  96. data/test/adapter/json_api/json_api_test.rb +5 -7
  97. data/test/adapter/json_api/linked_test.rb +33 -12
  98. data/test/adapter/json_api/links_test.rb +4 -2
  99. data/test/adapter/json_api/pagination_links_test.rb +35 -8
  100. data/test/adapter/json_api/relationship_test.rb +309 -73
  101. data/test/adapter/json_api/resource_identifier_test.rb +27 -2
  102. data/test/adapter/json_api/resource_meta_test.rb +3 -3
  103. data/test/adapter/json_api/transform_test.rb +255 -253
  104. data/test/adapter/json_api/type_test.rb +1 -1
  105. data/test/adapter/json_test.rb +8 -7
  106. data/test/adapter/null_test.rb +1 -2
  107. data/test/adapter/polymorphic_test.rb +5 -5
  108. data/test/adapter_test.rb +1 -1
  109. data/test/benchmark/app.rb +1 -1
  110. data/test/benchmark/benchmarking_support.rb +1 -1
  111. data/test/benchmark/bm_active_record.rb +81 -0
  112. data/test/benchmark/bm_adapter.rb +38 -0
  113. data/test/benchmark/bm_caching.rb +16 -16
  114. data/test/benchmark/bm_lookup_chain.rb +83 -0
  115. data/test/benchmark/bm_transform.rb +16 -5
  116. data/test/benchmark/controllers.rb +16 -17
  117. data/test/benchmark/fixtures.rb +72 -72
  118. data/test/cache_test.rb +143 -49
  119. data/test/collection_serializer_test.rb +3 -3
  120. data/test/fixtures/poro.rb +52 -48
  121. data/test/generators/serializer_generator_test.rb +22 -5
  122. data/test/grape_test.rb +152 -56
  123. data/test/lint_test.rb +1 -1
  124. data/test/logger_test.rb +13 -11
  125. data/test/serializable_resource_test.rb +18 -22
  126. data/test/serializers/association_macros_test.rb +3 -2
  127. data/test/serializers/associations_test.rb +107 -32
  128. data/test/serializers/attribute_test.rb +2 -2
  129. data/test/serializers/attributes_test.rb +1 -1
  130. data/test/serializers/fieldset_test.rb +1 -1
  131. data/test/serializers/meta_test.rb +12 -6
  132. data/test/serializers/root_test.rb +1 -1
  133. data/test/serializers/serializer_for_test.rb +6 -4
  134. data/test/serializers/serializer_for_with_namespace_test.rb +87 -0
  135. data/test/support/isolated_unit.rb +5 -2
  136. data/test/support/rails5_shims.rb +8 -2
  137. data/test/support/rails_app.rb +0 -9
  138. data/test/support/serialization_testing.rb +23 -5
  139. data/test/test_helper.rb +1 -0
  140. metadata +85 -34
  141. data/.rubocop_todo.yml +0 -167
  142. data/lib/active_model/serializer/include_tree.rb +0 -111
  143. data/test/adapter/json_api/relationships_test.rb +0 -199
  144. data/test/include_tree/from_include_args_test.rb +0 -26
  145. data/test/include_tree/from_string_test.rb +0 -94
  146. data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -1,199 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class Serializer
5
- module Adapter
6
- class JsonApi
7
- class RelationshipTest < ActiveSupport::TestCase
8
- RelationshipAuthor = Class.new(::Model)
9
- class RelationshipAuthorSerializer < ActiveModel::Serializer
10
- has_one :bio do
11
- link :self, '//example.com/link_author/relationships/bio'
12
- end
13
-
14
- has_one :profile do
15
- link :related do
16
- "//example.com/profiles/#{object.profile.id}"
17
- end
18
- end
19
-
20
- has_many :locations do
21
- link :related do
22
- ids = object.locations.map(&:id).join(',')
23
- href "//example.com/locations/#{ids}"
24
- end
25
- end
26
-
27
- has_many :posts do
28
- link :related do
29
- ids = object.posts.map(&:id).join(',')
30
- href "//example.com/posts/#{ids}"
31
- meta ids: ids
32
- end
33
- end
34
-
35
- has_many :comments do
36
- link :self do
37
- meta ids: [1]
38
- end
39
- end
40
-
41
- has_many :roles do |serializer|
42
- meta count: object.posts.count
43
- serializer.cached_roles
44
- end
45
-
46
- has_one :blog do
47
- link :self, '//example.com/link_author/relationships/blog'
48
- include_data false
49
- end
50
-
51
- belongs_to :reviewer do
52
- meta name: 'Dan Brown'
53
- include_data true
54
- end
55
-
56
- has_many :likes do
57
- link :related do
58
- ids = object.likes.map(&:id).join(',')
59
- href "//example.com/likes/#{ids}"
60
- meta ids: ids
61
- end
62
- meta liked: object.likes.any?
63
- end
64
-
65
- def cached_roles
66
- [
67
- Role.new(id: 'from-serializer-method')
68
- ]
69
- end
70
- end
71
-
72
- def setup
73
- @post = Post.new(id: 1337, comments: [], author: nil)
74
- @blog = Blog.new(id: 1337, name: 'extra')
75
- @bio = Bio.new(id: 1337)
76
- @like = Like.new(id: 1337)
77
- @role = Role.new(id: 'from-record')
78
- @profile = Profile.new(id: 1337)
79
- @location = Location.new(id: 1337)
80
- @reviewer = Author.new(id: 1337)
81
- @comment = Comment.new(id: 1337)
82
- @author = RelationshipAuthor.new(
83
- id: 1337,
84
- posts: [@post],
85
- blog: @blog,
86
- reviewer: @reviewer,
87
- bio: @bio,
88
- likes: [@like],
89
- roles: [@role],
90
- locations: [@location],
91
- profile: @profile,
92
- comments: [@comment]
93
- )
94
- end
95
-
96
- def test_relationship_simple_link
97
- expected = {
98
- data: {
99
- id: '1337',
100
- type: 'bios'
101
- },
102
- links: {
103
- self: '//example.com/link_author/relationships/bio'
104
- }
105
- }
106
- assert_relationship(:bio, expected)
107
- end
108
-
109
- def test_relationship_block_link
110
- expected = {
111
- data: { id: '1337', type: 'profiles' },
112
- links: { related: '//example.com/profiles/1337' }
113
- }
114
- assert_relationship(:profile, expected)
115
- end
116
-
117
- def test_relationship_block_link_href
118
- expected = {
119
- data: [{ id: '1337', type: 'locations' }],
120
- links: {
121
- related: { href: '//example.com/locations/1337' }
122
- }
123
- }
124
- assert_relationship(:locations, expected)
125
- end
126
-
127
- def test_relationship_block_link_href_and_meta
128
- expected = {
129
- data: [{ id: '1337', type: 'posts' }],
130
- links: {
131
- related: {
132
- href: '//example.com/posts/1337',
133
- meta: { ids: '1337' }
134
- }
135
- }
136
- }
137
- assert_relationship(:posts, expected)
138
- end
139
-
140
- def test_relationship_block_link_meta
141
- expected = {
142
- data: [{ id: '1337', type: 'comments' }],
143
- links: {
144
- self: {
145
- meta: { ids: [1] }
146
- }
147
- }
148
- }
149
- assert_relationship(:comments, expected)
150
- end
151
-
152
- def test_relationship_meta
153
- expected = {
154
- data: [{ id: 'from-serializer-method', type: 'roles' }],
155
- meta: { count: 1 }
156
- }
157
- assert_relationship(:roles, expected)
158
- end
159
-
160
- def test_relationship_not_including_data
161
- expected = {
162
- links: { self: '//example.com/link_author/relationships/blog' }
163
- }
164
- assert_relationship(:blog, expected)
165
- end
166
-
167
- def test_relationship_including_data_explicit
168
- expected = {
169
- data: { id: '1337', type: 'authors' },
170
- meta: { name: 'Dan Brown' }
171
- }
172
- assert_relationship(:reviewer, expected)
173
- end
174
-
175
- def test_relationship_with_everything
176
- expected = {
177
- data: [{ id: '1337', type: 'likes' }],
178
- links: {
179
- related: {
180
- href: '//example.com/likes/1337',
181
- meta: { ids: '1337' }
182
- }
183
- },
184
- meta: { liked: true }
185
- }
186
- assert_relationship(:likes, expected)
187
- end
188
-
189
- private
190
-
191
- def assert_relationship(relationship_name, expected)
192
- hash = serializable(@author, adapter: :json_api).serializable_hash
193
- assert_equal(expected, hash[:data][:relationships][relationship_name])
194
- end
195
- end
196
- end
197
- end
198
- end
199
- 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