active_model_serializers 0.10.9 → 0.10.13

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.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +41 -2
  3. data/README.md +18 -19
  4. data/lib/action_controller/serialization.rb +9 -1
  5. data/lib/active_model/serializer/collection_serializer.rb +11 -2
  6. data/lib/active_model/serializer/concerns/caching.rb +2 -1
  7. data/lib/active_model/serializer/fieldset.rb +1 -1
  8. data/lib/active_model/serializer/reflection.rb +4 -4
  9. data/lib/active_model/serializer/version.rb +1 -1
  10. data/lib/active_model/serializer.rb +13 -4
  11. data/lib/active_model_serializers/adapter/attributes.rb +21 -0
  12. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +5 -1
  13. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +1 -1
  14. data/lib/active_model_serializers/model/caching.rb +26 -0
  15. metadata +20 -292
  16. data/.github/ISSUE_TEMPLATE.md +0 -29
  17. data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
  18. data/.gitignore +0 -35
  19. data/.rubocop.yml +0 -109
  20. data/.simplecov +0 -110
  21. data/.travis.yml +0 -63
  22. data/CODE_OF_CONDUCT.md +0 -74
  23. data/CONTRIBUTING.md +0 -105
  24. data/Gemfile +0 -74
  25. data/Rakefile +0 -76
  26. data/active_model_serializers.gemspec +0 -64
  27. data/appveyor.yml +0 -28
  28. data/bin/bench +0 -171
  29. data/bin/bench_regression +0 -316
  30. data/bin/rubocop +0 -38
  31. data/bin/serve_benchmark +0 -39
  32. data/docs/README.md +0 -41
  33. data/docs/STYLE.md +0 -58
  34. data/docs/general/adapters.md +0 -269
  35. data/docs/general/caching.md +0 -58
  36. data/docs/general/configuration_options.md +0 -185
  37. data/docs/general/deserialization.md +0 -100
  38. data/docs/general/fields.md +0 -31
  39. data/docs/general/getting_started.md +0 -133
  40. data/docs/general/instrumentation.md +0 -40
  41. data/docs/general/key_transforms.md +0 -40
  42. data/docs/general/logging.md +0 -21
  43. data/docs/general/rendering.md +0 -293
  44. data/docs/general/serializers.md +0 -495
  45. data/docs/how-open-source-maintained.jpg +0 -0
  46. data/docs/howto/add_pagination_links.md +0 -138
  47. data/docs/howto/add_relationship_links.md +0 -140
  48. data/docs/howto/add_root_key.md +0 -62
  49. data/docs/howto/grape_integration.md +0 -42
  50. data/docs/howto/outside_controller_use.md +0 -66
  51. data/docs/howto/passing_arbitrary_options.md +0 -27
  52. data/docs/howto/serialize_poro.md +0 -73
  53. data/docs/howto/test.md +0 -154
  54. data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
  55. data/docs/integrations/ember-and-json-api.md +0 -147
  56. data/docs/integrations/grape.md +0 -19
  57. data/docs/jsonapi/errors.md +0 -56
  58. data/docs/jsonapi/schema/schema.json +0 -366
  59. data/docs/jsonapi/schema.md +0 -151
  60. data/docs/rfcs/0000-namespace.md +0 -106
  61. data/docs/rfcs/template.md +0 -15
  62. data/test/action_controller/adapter_selector_test.rb +0 -64
  63. data/test/action_controller/explicit_serializer_test.rb +0 -137
  64. data/test/action_controller/json/include_test.rb +0 -248
  65. data/test/action_controller/json_api/deserialization_test.rb +0 -114
  66. data/test/action_controller/json_api/errors_test.rb +0 -42
  67. data/test/action_controller/json_api/fields_test.rb +0 -68
  68. data/test/action_controller/json_api/linked_test.rb +0 -204
  69. data/test/action_controller/json_api/pagination_test.rb +0 -126
  70. data/test/action_controller/json_api/transform_test.rb +0 -191
  71. data/test/action_controller/lookup_proc_test.rb +0 -51
  72. data/test/action_controller/namespace_lookup_test.rb +0 -234
  73. data/test/action_controller/serialization_scope_name_test.rb +0 -237
  74. data/test/action_controller/serialization_test.rb +0 -480
  75. data/test/active_model_serializers/adapter_for_test.rb +0 -210
  76. data/test/active_model_serializers/json_pointer_test.rb +0 -24
  77. data/test/active_model_serializers/logging_test.rb +0 -79
  78. data/test/active_model_serializers/model_test.rb +0 -144
  79. data/test/active_model_serializers/railtie_test_isolated.rb +0 -70
  80. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -163
  81. data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -73
  82. data/test/active_model_serializers/test/schema_test.rb +0 -133
  83. data/test/active_model_serializers/test/serializer_test.rb +0 -64
  84. data/test/active_record_test.rb +0 -11
  85. data/test/adapter/attributes_test.rb +0 -42
  86. data/test/adapter/deprecation_test.rb +0 -102
  87. data/test/adapter/json/belongs_to_test.rb +0 -47
  88. data/test/adapter/json/collection_test.rb +0 -106
  89. data/test/adapter/json/has_many_test.rb +0 -55
  90. data/test/adapter/json/transform_test.rb +0 -95
  91. data/test/adapter/json_api/belongs_to_test.rb +0 -157
  92. data/test/adapter/json_api/collection_test.rb +0 -98
  93. data/test/adapter/json_api/errors_test.rb +0 -78
  94. data/test/adapter/json_api/fields_test.rb +0 -98
  95. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -98
  96. data/test/adapter/json_api/has_many_test.rb +0 -175
  97. data/test/adapter/json_api/has_one_test.rb +0 -82
  98. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -215
  99. data/test/adapter/json_api/json_api_test.rb +0 -35
  100. data/test/adapter/json_api/linked_test.rb +0 -415
  101. data/test/adapter/json_api/links_test.rb +0 -112
  102. data/test/adapter/json_api/pagination_links_test.rb +0 -208
  103. data/test/adapter/json_api/parse_test.rb +0 -139
  104. data/test/adapter/json_api/relationship_test.rb +0 -399
  105. data/test/adapter/json_api/resource_meta_test.rb +0 -102
  106. data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -84
  107. data/test/adapter/json_api/transform_test.rb +0 -514
  108. data/test/adapter/json_api/type_test.rb +0 -195
  109. data/test/adapter/json_test.rb +0 -48
  110. data/test/adapter/null_test.rb +0 -24
  111. data/test/adapter/polymorphic_test.rb +0 -220
  112. data/test/adapter_test.rb +0 -69
  113. data/test/array_serializer_test.rb +0 -24
  114. data/test/benchmark/app.rb +0 -67
  115. data/test/benchmark/benchmarking_support.rb +0 -69
  116. data/test/benchmark/bm_active_record.rb +0 -83
  117. data/test/benchmark/bm_adapter.rb +0 -40
  118. data/test/benchmark/bm_caching.rb +0 -121
  119. data/test/benchmark/bm_lookup_chain.rb +0 -85
  120. data/test/benchmark/bm_transform.rb +0 -47
  121. data/test/benchmark/config.ru +0 -3
  122. data/test/benchmark/controllers.rb +0 -85
  123. data/test/benchmark/fixtures.rb +0 -221
  124. data/test/cache_test.rb +0 -717
  125. data/test/collection_serializer_test.rb +0 -129
  126. data/test/fixtures/active_record.rb +0 -115
  127. data/test/fixtures/poro.rb +0 -227
  128. data/test/generators/scaffold_controller_generator_test.rb +0 -26
  129. data/test/generators/serializer_generator_test.rb +0 -77
  130. data/test/grape_test.rb +0 -198
  131. data/test/lint_test.rb +0 -51
  132. data/test/logger_test.rb +0 -22
  133. data/test/poro_test.rb +0 -11
  134. data/test/serializable_resource_test.rb +0 -81
  135. data/test/serializers/association_macros_test.rb +0 -39
  136. data/test/serializers/associations_test.rb +0 -520
  137. data/test/serializers/attribute_test.rb +0 -155
  138. data/test/serializers/attributes_test.rb +0 -54
  139. data/test/serializers/caching_configuration_test_isolated.rb +0 -172
  140. data/test/serializers/configuration_test.rb +0 -34
  141. data/test/serializers/fieldset_test.rb +0 -16
  142. data/test/serializers/meta_test.rb +0 -204
  143. data/test/serializers/options_test.rb +0 -34
  144. data/test/serializers/read_attribute_for_serialization_test.rb +0 -81
  145. data/test/serializers/reflection_test.rb +0 -481
  146. data/test/serializers/root_test.rb +0 -23
  147. data/test/serializers/serialization_test.rb +0 -57
  148. data/test/serializers/serializer_for_test.rb +0 -138
  149. data/test/serializers/serializer_for_with_namespace_test.rb +0 -90
  150. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  151. data/test/support/isolated_unit.rb +0 -86
  152. data/test/support/rails5_shims.rb +0 -55
  153. data/test/support/rails_app.rb +0 -40
  154. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  155. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
  156. data/test/support/schemas/custom/show.json +0 -7
  157. data/test/support/schemas/hyper_schema.json +0 -93
  158. data/test/support/schemas/render_using_json_api.json +0 -43
  159. data/test/support/schemas/simple_json_pointers.json +0 -10
  160. data/test/support/serialization_testing.rb +0 -81
  161. data/test/test_helper.rb +0 -72
@@ -1,399 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModelSerializers
6
- module Adapter
7
- class JsonApi
8
- class RelationshipTest < ActiveSupport::TestCase
9
- def test_relationship_with_data
10
- expected = {
11
- data: {
12
- id: '1',
13
- type: 'blogs'
14
- }
15
- }
16
-
17
- model_attributes = { blog: Blog.new(id: 1) }
18
- relationship_name = :blog
19
- model = new_model(model_attributes)
20
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
21
- has_one :blog
22
- end
23
- assert_equal(expected, actual)
24
- end
25
-
26
- def test_relationship_with_nil_model
27
- expected = { data: nil }
28
-
29
- model_attributes = { blog: nil }
30
- relationship_name = :blog
31
- model = new_model(model_attributes)
32
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
33
- has_one :blog
34
- end
35
- assert_equal(expected, actual)
36
- end
37
-
38
- def test_relationship_with_data_array
39
- expected = {
40
- data: [
41
- {
42
- id: '1',
43
- type: 'posts'
44
- },
45
- {
46
- id: '2',
47
- type: 'posts'
48
- }
49
- ]
50
- }
51
-
52
- model_attributes = { posts: [Post.new(id: 1), Post.new(id: 2)] }
53
- relationship_name = :posts
54
- model = new_model(model_attributes)
55
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
56
- has_many :posts
57
- end
58
- assert_equal(expected, actual)
59
- end
60
-
61
- def test_relationship_data_not_included
62
- expected = { meta: {} }
63
-
64
- model_attributes = { blog: :does_not_matter }
65
- relationship_name = :blog
66
- model = new_model(model_attributes)
67
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
68
- has_one :blog do
69
- include_data false
70
- end
71
- end
72
- assert_equal(expected, actual)
73
- end
74
-
75
- def test_relationship_many_links
76
- expected = {
77
- links: {
78
- self: 'a link',
79
- related: 'another link'
80
- }
81
- }
82
-
83
- model_attributes = { blog: :does_not_matter }
84
- relationship_name = :blog
85
- model = new_model(model_attributes)
86
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
87
- has_one :blog do
88
- include_data false
89
- link :self, 'a link'
90
- link :related, 'another link'
91
- end
92
- end
93
- assert_equal(expected, actual)
94
- end
95
-
96
- def test_relationship_block_link_with_meta
97
- expected = {
98
- links: {
99
- self: {
100
- href: '1',
101
- meta: { id: 1 }
102
- }
103
- }
104
- }
105
-
106
- model_attributes = { blog: Blog.new(id: 1) }
107
- relationship_name = :blog
108
- model = new_model(model_attributes)
109
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
110
- has_one :blog do
111
- include_data false
112
- link :self do
113
- href object.blog.id.to_s
114
- meta(id: object.blog.id)
115
- end
116
- end
117
- end
118
- assert_equal(expected, actual)
119
- end
120
-
121
- def test_relationship_simple_meta
122
- expected = { meta: { id: '1' } }
123
-
124
- model_attributes = { blog: Blog.new(id: 1) }
125
- relationship_name = :blog
126
- model = new_model(model_attributes)
127
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
128
- has_one :blog do
129
- include_data false
130
- meta(id: object.blog.id.to_s)
131
- end
132
- end
133
- assert_equal(expected, actual)
134
- end
135
-
136
- def test_relationship_block_meta
137
- expected = {
138
- meta: {
139
- id: 1
140
- }
141
- }
142
-
143
- model_attributes = { blog: Blog.new(id: 1) }
144
- relationship_name = :blog
145
- model = new_model(model_attributes)
146
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
147
- has_one :blog do
148
- include_data false
149
- meta(id: object.blog.id)
150
- end
151
- end
152
- assert_equal(expected, actual)
153
- end
154
-
155
- def test_relationship_simple_link
156
- expected = {
157
- data: {
158
- id: '1337',
159
- type: 'bios'
160
- },
161
- links: {
162
- self: '//example.com/link_author/relationships/bio'
163
- }
164
- }
165
-
166
- model_attributes = { bio: Bio.new(id: 1337) }
167
- relationship_name = :bio
168
- model = new_model(model_attributes)
169
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
170
- has_one :bio do
171
- link :self, '//example.com/link_author/relationships/bio'
172
- end
173
- end
174
- assert_equal(expected, actual)
175
- end
176
-
177
- def test_relationship_block_link
178
- expected = {
179
- data: { id: '1337', type: 'profiles' },
180
- links: { related: '//example.com/profiles/1337' }
181
- }
182
-
183
- model_attributes = { profile: Profile.new(id: 1337) }
184
- relationship_name = :profile
185
- model = new_model(model_attributes)
186
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
187
- has_one :profile do
188
- id = object.profile.id
189
- link :related do
190
- "//example.com/profiles/#{id}" if id != 123
191
- end
192
- end
193
- end
194
- assert_equal(expected, actual)
195
- end
196
-
197
- def test_relationship_with_everything
198
- expected = {
199
- data: [{ id: '1337', type: 'likes' }],
200
- links: {
201
- related: {
202
- href: '//example.com/likes/1337',
203
- meta: { ids: '1337' }
204
- }
205
- },
206
- meta: { liked: true }
207
- }
208
-
209
- model_attributes = { likes: [Like.new(id: 1337)] }
210
- relationship_name = :likes
211
- model = new_model(model_attributes)
212
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
213
- has_many :likes do
214
- link :related do
215
- ids = object.likes.map(&:id).join(',')
216
- href "//example.com/likes/#{ids}"
217
- meta ids: ids
218
- end
219
- meta liked: object.likes.any?
220
- end
221
- end
222
- assert_equal(expected, actual)
223
- end
224
-
225
- def test_relationship_nil_link
226
- expected = {
227
- data: { id: '123', type: 'profiles' }
228
- }
229
-
230
- model_attributes = { profile: Profile.new(id: 123) }
231
- relationship_name = :profile
232
- model = new_model(model_attributes)
233
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
234
- has_one :profile do
235
- id = object.profile.id
236
- link :related do
237
- "//example.com/profiles/#{id}" if id != 123
238
- end
239
- end
240
- end
241
- assert_equal(expected, actual)
242
- end
243
-
244
- def test_relationship_block_link_href
245
- expected = {
246
- data: [{ id: '1337', type: 'locations' }],
247
- links: {
248
- related: { href: '//example.com/locations/1337' }
249
- }
250
- }
251
-
252
- model_attributes = { locations: [Location.new(id: 1337)] }
253
- relationship_name = :locations
254
- model = new_model(model_attributes)
255
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
256
- has_many :locations do
257
- link :related do
258
- ids = object.locations.map(&:id).join(',')
259
- href "//example.com/locations/#{ids}"
260
- end
261
- end
262
- end
263
- assert_equal(expected, actual)
264
- end
265
-
266
- def test_relationship_block_link_href_and_meta
267
- expected = {
268
- data: [{ id: '1337', type: 'posts' }],
269
- links: {
270
- related: {
271
- href: '//example.com/posts/1337',
272
- meta: { ids: '1337' }
273
- }
274
- }
275
- }
276
-
277
- model_attributes = { posts: [Post.new(id: 1337, comments: [], author: nil)] }
278
- relationship_name = :posts
279
- model = new_model(model_attributes)
280
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
281
- has_many :posts do
282
- link :related do
283
- ids = object.posts.map(&:id).join(',')
284
- href "//example.com/posts/#{ids}"
285
- meta ids: ids
286
- end
287
- end
288
- end
289
- assert_equal(expected, actual)
290
- end
291
-
292
- def test_relationship_block_link_meta
293
- expected = {
294
- data: [{ id: '1337', type: 'comments' }],
295
- links: {
296
- self: {
297
- meta: { ids: [1] }
298
- }
299
- }
300
- }
301
-
302
- model_attributes = { comments: [Comment.new(id: 1337)] }
303
- relationship_name = :comments
304
- model = new_model(model_attributes)
305
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
306
- has_many :comments do
307
- link :self do
308
- meta ids: [1]
309
- end
310
- end
311
- end
312
- assert_equal(expected, actual)
313
- end
314
-
315
- def test_relationship_meta
316
- expected = {
317
- data: [{ id: 'from-serializer-method', type: 'roles' }],
318
- meta: { count: 1 }
319
- }
320
-
321
- model_attributes = { roles: [Role.new(id: 'from-record')] }
322
- relationship_name = :roles
323
- model = new_model(model_attributes)
324
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
325
- has_many :roles do |serializer|
326
- meta count: object.roles.count
327
- serializer.cached_roles
328
- end
329
- def cached_roles
330
- [
331
- Role.new(id: 'from-serializer-method')
332
- ]
333
- end
334
- end
335
- assert_equal(expected, actual)
336
- end
337
-
338
- def test_relationship_not_including_data
339
- expected = {
340
- links: { self: '//example.com/link_author/relationships/blog' }
341
- }
342
-
343
- model_attributes = { blog: Object }
344
- relationship_name = :blog
345
- model = new_model(model_attributes)
346
- model.define_singleton_method(:read_attribute_for_serialization) do |attr|
347
- fail 'should not be called' if attr == :blog
348
- super(attr)
349
- end
350
- assert_nothing_raised do
351
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
352
- has_one :blog do
353
- link :self, '//example.com/link_author/relationships/blog'
354
- include_data false
355
- end
356
- end
357
- assert_equal(expected, actual)
358
- end
359
- end
360
-
361
- def test_relationship_including_data_explicit
362
- expected = {
363
- data: { id: '1337', type: 'authors' },
364
- meta: { name: 'Dan Brown' }
365
- }
366
-
367
- model_attributes = { reviewer: Author.new(id: 1337) }
368
- relationship_name = :reviewer
369
- model = new_model(model_attributes)
370
- actual = build_serializer_and_serialize_relationship(model, relationship_name) do
371
- belongs_to :reviewer do
372
- meta name: 'Dan Brown'
373
- include_data true
374
- end
375
- end
376
- assert_equal(expected, actual)
377
- end
378
-
379
- private
380
-
381
- def build_serializer_and_serialize_relationship(model, relationship_name, &block)
382
- serializer_class = Class.new(ActiveModel::Serializer, &block)
383
- hash = serializable(model, serializer: serializer_class, adapter: :json_api).serializable_hash
384
- hash[:data][:relationships][relationship_name]
385
- end
386
-
387
- def new_model(model_attributes)
388
- Class.new(ActiveModelSerializers::Model) do
389
- attributes(*model_attributes.keys)
390
-
391
- def self.name
392
- 'TestModel'
393
- end
394
- end.new(model_attributes)
395
- end
396
- end
397
- end
398
- end
399
- end
@@ -1,102 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModel
6
- class Serializer
7
- module Adapter
8
- class JsonApi
9
- class ResourceMetaTest < Minitest::Test
10
- class MetaHashPostSerializer < ActiveModel::Serializer
11
- attributes :id
12
- meta stuff: 'value'
13
- end
14
-
15
- class MetaBlockPostSerializer < ActiveModel::Serializer
16
- attributes :id
17
- meta do
18
- { comments_count: object.comments.count }
19
- end
20
- end
21
-
22
- class MetaBlockPostBlankMetaSerializer < ActiveModel::Serializer
23
- attributes :id
24
- meta do
25
- {}
26
- end
27
- end
28
-
29
- class MetaBlockPostEmptyStringSerializer < ActiveModel::Serializer
30
- attributes :id
31
- meta do
32
- ''
33
- end
34
- end
35
-
36
- def setup
37
- @post = Post.new(id: 1337, comments: [], author: nil)
38
- end
39
-
40
- def test_meta_hash_object_resource
41
- hash = ActiveModelSerializers::SerializableResource.new(
42
- @post,
43
- serializer: MetaHashPostSerializer,
44
- adapter: :json_api
45
- ).serializable_hash
46
- expected = {
47
- stuff: 'value'
48
- }
49
- assert_equal(expected, hash[:data][:meta])
50
- end
51
-
52
- def test_meta_block_object_resource
53
- hash = ActiveModelSerializers::SerializableResource.new(
54
- @post,
55
- serializer: MetaBlockPostSerializer,
56
- adapter: :json_api
57
- ).serializable_hash
58
- expected = {
59
- :"comments-count" => @post.comments.count
60
- }
61
- assert_equal(expected, hash[:data][:meta])
62
- end
63
-
64
- def test_meta_object_resource_in_array
65
- post2 = Post.new(id: 1339, comments: [Comment.new])
66
- posts = [@post, post2]
67
- hash = ActiveModelSerializers::SerializableResource.new(
68
- posts,
69
- each_serializer: MetaBlockPostSerializer,
70
- adapter: :json_api
71
- ).serializable_hash
72
- expected = {
73
- data: [
74
- { id: '1337', type: 'posts', meta: { :"comments-count" => 0 } },
75
- { id: '1339', type: 'posts', meta: { :"comments-count" => 1 } }
76
- ]
77
- }
78
- assert_equal(expected, hash)
79
- end
80
-
81
- def test_meta_object_blank_omitted
82
- hash = ActiveModelSerializers::SerializableResource.new(
83
- @post,
84
- serializer: MetaBlockPostBlankMetaSerializer,
85
- adapter: :json_api
86
- ).serializable_hash
87
- refute hash[:data].key? :meta
88
- end
89
-
90
- def test_meta_object_empty_string_omitted
91
- hash = ActiveModelSerializers::SerializableResource.new(
92
- @post,
93
- serializer: MetaBlockPostEmptyStringSerializer,
94
- adapter: :json_api
95
- ).serializable_hash
96
- refute hash[:data].key? :meta
97
- end
98
- end
99
- end
100
- end
101
- end
102
- end
@@ -1,84 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModelSerializers
6
- module Adapter
7
- class JsonApi
8
- class TopLevelJsonApiTest < ActiveSupport::TestCase
9
- def setup
10
- @author = Author.new(id: 1, name: 'Steve K.')
11
- @author.bio = nil
12
- @author.roles = []
13
- @blog = Blog.new(id: 23, name: 'AMS Blog')
14
- @post = Post.new(id: 42, title: 'New Post', body: 'Body')
15
- @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
16
- @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
17
- @post.comments = [@comment]
18
- @post.blog = @blog
19
- @anonymous_post.comments = []
20
- @anonymous_post.blog = nil
21
- @comment.post = @post
22
- @comment.author = nil
23
- @post.author = @author
24
- @anonymous_post.author = nil
25
- @blog = Blog.new(id: 1, name: 'My Blog!!')
26
- @blog.writer = @author
27
- @blog.articles = [@post, @anonymous_post]
28
- @author.posts = []
29
- end
30
-
31
- def test_toplevel_jsonapi_defaults_to_false
32
- assert_equal config.fetch(:jsonapi_include_toplevel_object), false
33
- end
34
-
35
- def test_disable_toplevel_jsonapi
36
- with_config(jsonapi_include_toplevel_object: false) do
37
- hash = serialize(@post)
38
- assert_nil(hash[:jsonapi])
39
- end
40
- end
41
-
42
- def test_enable_toplevel_jsonapi
43
- with_config(jsonapi_include_toplevel_object: true) do
44
- hash = serialize(@post)
45
- refute_nil(hash[:jsonapi])
46
- end
47
- end
48
-
49
- def test_default_toplevel_jsonapi_version
50
- with_config(jsonapi_include_toplevel_object: true) do
51
- hash = serialize(@post)
52
- assert_equal('1.0', hash[:jsonapi][:version])
53
- end
54
- end
55
-
56
- def test_toplevel_jsonapi_no_meta
57
- with_config(jsonapi_include_toplevel_object: true) do
58
- hash = serialize(@post)
59
- assert_nil(hash[:jsonapi][:meta])
60
- end
61
- end
62
-
63
- def test_toplevel_jsonapi_meta
64
- new_config = {
65
- jsonapi_include_toplevel_object: true,
66
- jsonapi_toplevel_meta: {
67
- 'copyright' => 'Copyright 2015 Example Corp.'
68
- }
69
- }
70
- with_config(new_config) do
71
- hash = serialize(@post)
72
- assert_equal(new_config[:jsonapi_toplevel_meta], hash.fetch(:jsonapi).fetch(:meta))
73
- end
74
- end
75
-
76
- private
77
-
78
- def serialize(resource, options = {})
79
- serializable(resource, { adapter: :json_api }.merge!(options)).serializable_hash
80
- end
81
- end
82
- end
83
- end
84
- end