active_model_serializers 0.8.3 → 0.10.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -0
  3. data/.rubocop.yml +82 -0
  4. data/.rubocop_todo.yml +315 -0
  5. data/.simplecov +99 -0
  6. data/.travis.yml +26 -20
  7. data/CHANGELOG.md +14 -67
  8. data/CONTRIBUTING.md +31 -0
  9. data/Gemfile +45 -1
  10. data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
  11. data/README.md +186 -488
  12. data/Rakefile +33 -12
  13. data/active_model_serializers.gemspec +49 -23
  14. data/appveyor.yml +25 -0
  15. data/docs/README.md +29 -0
  16. data/docs/general/adapters.md +110 -0
  17. data/docs/general/configuration_options.md +11 -0
  18. data/docs/general/getting_started.md +73 -0
  19. data/docs/howto/add_pagination_links.md +112 -0
  20. data/docs/howto/add_root_key.md +51 -0
  21. data/docs/howto/outside_controller_use.md +42 -0
  22. data/lib/action_controller/serialization.rb +31 -31
  23. data/lib/active_model/serializable_resource.rb +70 -0
  24. data/lib/active_model/serializer/adapter/flatten_json.rb +12 -0
  25. data/lib/active_model/serializer/adapter/fragment_cache.rb +75 -0
  26. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +5 -0
  27. data/lib/active_model/serializer/adapter/json.rb +47 -0
  28. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +13 -0
  29. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +50 -0
  30. data/lib/active_model/serializer/adapter/json_api.rb +158 -0
  31. data/lib/active_model/serializer/adapter/null.rb +5 -0
  32. data/lib/active_model/serializer/adapter.rb +159 -0
  33. data/lib/active_model/serializer/array_serializer.rb +40 -0
  34. data/lib/active_model/serializer/association.rb +20 -0
  35. data/lib/active_model/serializer/associations.rb +83 -219
  36. data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
  37. data/lib/active_model/serializer/collection_reflection.rb +7 -0
  38. data/lib/active_model/serializer/configuration.rb +14 -0
  39. data/lib/active_model/serializer/fieldset.rb +40 -0
  40. data/lib/active_model/serializer/has_many_reflection.rb +10 -0
  41. data/lib/active_model/serializer/has_one_reflection.rb +10 -0
  42. data/lib/active_model/serializer/lint.rb +129 -0
  43. data/lib/active_model/serializer/railtie.rb +15 -0
  44. data/lib/active_model/serializer/reflection.rb +74 -0
  45. data/lib/active_model/serializer/singular_reflection.rb +7 -0
  46. data/lib/active_model/serializer/utils.rb +35 -0
  47. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  48. data/lib/active_model/serializer.rb +121 -465
  49. data/lib/active_model_serializers.rb +26 -88
  50. data/lib/generators/serializer/USAGE +0 -3
  51. data/lib/generators/serializer/resource_override.rb +12 -0
  52. data/lib/generators/serializer/serializer_generator.rb +8 -13
  53. data/lib/generators/serializer/templates/serializer.rb.erb +8 -0
  54. data/lib/tasks/rubocop.rake +0 -0
  55. data/test/action_controller/adapter_selector_test.rb +53 -0
  56. data/test/action_controller/explicit_serializer_test.rb +134 -0
  57. data/test/action_controller/json_api/linked_test.rb +179 -0
  58. data/test/action_controller/json_api/pagination_test.rb +116 -0
  59. data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +15 -15
  60. data/test/action_controller/serialization_test.rb +420 -0
  61. data/test/active_record_test.rb +9 -0
  62. data/test/adapter/fragment_cache_test.rb +37 -0
  63. data/test/adapter/json/belongs_to_test.rb +47 -0
  64. data/test/adapter/json/collection_test.rb +82 -0
  65. data/test/adapter/json/has_many_test.rb +47 -0
  66. data/test/adapter/json_api/belongs_to_test.rb +157 -0
  67. data/test/adapter/json_api/collection_test.rb +95 -0
  68. data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
  69. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  70. data/test/adapter/json_api/has_many_test.rb +145 -0
  71. data/test/adapter/json_api/has_one_test.rb +81 -0
  72. data/test/adapter/json_api/json_api_test.rb +37 -0
  73. data/test/adapter/json_api/linked_test.rb +283 -0
  74. data/test/adapter/json_api/pagination_links_test.rb +115 -0
  75. data/test/adapter/json_api/resource_type_config_test.rb +59 -0
  76. data/test/adapter/json_test.rb +47 -0
  77. data/test/adapter/null_test.rb +25 -0
  78. data/test/adapter_test.rb +42 -0
  79. data/test/array_serializer_test.rb +99 -73
  80. data/test/capture_warnings.rb +65 -0
  81. data/test/fixtures/active_record.rb +56 -0
  82. data/test/fixtures/poro.rb +261 -0
  83. data/test/generators/scaffold_controller_generator_test.rb +23 -0
  84. data/test/generators/serializer_generator_test.rb +56 -0
  85. data/test/lint_test.rb +37 -0
  86. data/test/logger_test.rb +18 -0
  87. data/test/poro_test.rb +9 -0
  88. data/test/serializable_resource_test.rb +27 -0
  89. data/test/serializers/adapter_for_test.rb +170 -0
  90. data/test/serializers/association_macros_test.rb +36 -0
  91. data/test/serializers/associations_test.rb +150 -0
  92. data/test/serializers/attribute_test.rb +62 -0
  93. data/test/serializers/attributes_test.rb +57 -0
  94. data/test/serializers/cache_test.rb +165 -0
  95. data/test/serializers/configuration_test.rb +15 -0
  96. data/test/serializers/fieldset_test.rb +25 -0
  97. data/test/serializers/meta_test.rb +121 -0
  98. data/test/serializers/options_test.rb +21 -0
  99. data/test/serializers/root_test.rb +21 -0
  100. data/test/serializers/serializer_for_test.rb +65 -0
  101. data/test/support/rails_app.rb +21 -0
  102. data/test/support/serialization_testing.rb +13 -0
  103. data/test/support/simplecov.rb +6 -0
  104. data/test/support/stream_capture.rb +50 -0
  105. data/test/support/test_case.rb +5 -0
  106. data/test/test_helper.rb +48 -24
  107. data/test/utils/include_args_to_hash_test.rb +79 -0
  108. metadata +219 -47
  109. data/DESIGN.textile +0 -586
  110. data/Gemfile.edge +0 -9
  111. data/bench/perf.rb +0 -43
  112. data/cruft.md +0 -19
  113. data/lib/active_model/array_serializer.rb +0 -104
  114. data/lib/active_record/serializer_override.rb +0 -16
  115. data/lib/generators/resource_override.rb +0 -13
  116. data/lib/generators/serializer/templates/serializer.rb +0 -19
  117. data/test/association_test.rb +0 -592
  118. data/test/caching_test.rb +0 -96
  119. data/test/generators_test.rb +0 -85
  120. data/test/no_serialization_scope_test.rb +0 -34
  121. data/test/serialization_test.rb +0 -392
  122. data/test/serializer_support_test.rb +0 -51
  123. data/test/serializer_test.rb +0 -1465
  124. data/test/test_fakes.rb +0 -217
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Adapter
6
+ class JsonApiTest < Minitest::Test
7
+ def setup
8
+ ActionController::Base.cache_store.clear
9
+ @author = Author.new(id: 1, name: 'Steve K.')
10
+ @post = Post.new(id: 1, title: 'New Post', body: 'Body')
11
+ @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
12
+ @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
13
+ @post.comments = [@first_comment, @second_comment]
14
+ @first_comment.post = @post
15
+ @second_comment.post = @post
16
+ @post.author = @author
17
+ @blog = Blog.new(id: 1, name: 'My Blog!!')
18
+ @post.blog = @blog
19
+ end
20
+
21
+ def test_custom_keys
22
+ serializer = PostWithCustomKeysSerializer.new(@post)
23
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
24
+
25
+ assert_equal({
26
+ reviews: { data: [
27
+ { type: 'comments', id: '1' },
28
+ { type: 'comments', id: '2' }
29
+ ] },
30
+ writer: { data: { type: 'authors', id: '1' } },
31
+ site: { data: { type: 'blogs', id: '1' } }
32
+ }, adapter.serializable_hash[:data][:relationships])
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,283 @@
1
+ require 'test_helper'
2
+ module ActiveModel
3
+ class Serializer
4
+ class Adapter
5
+ class JsonApi
6
+ class LinkedTest < Minitest::Test
7
+ def setup
8
+ ActionController::Base.cache_store.clear
9
+ @author1 = Author.new(id: 1, name: 'Steve K.')
10
+ @author2 = Author.new(id: 2, name: 'Tenderlove')
11
+ @bio1 = Bio.new(id: 1, content: 'AMS Contributor')
12
+ @bio2 = Bio.new(id: 2, content: 'Rails Contributor')
13
+ @first_post = Post.new(id: 10, title: 'Hello!!', body: 'Hello, world!!')
14
+ @second_post = Post.new(id: 20, title: 'New Post', body: 'Body')
15
+ @third_post = Post.new(id: 30, title: 'Yet Another Post', body: 'Body')
16
+ @blog = Blog.new({ name: 'AMS Blog' })
17
+ @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
18
+ @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
19
+ @first_post.blog = @blog
20
+ @second_post.blog = @blog
21
+ @third_post.blog = nil
22
+ @first_post.comments = [@first_comment, @second_comment]
23
+ @second_post.comments = []
24
+ @third_post.comments = []
25
+ @first_post.author = @author1
26
+ @second_post.author = @author2
27
+ @third_post.author = @author1
28
+ @first_comment.post = @first_post
29
+ @first_comment.author = nil
30
+ @second_comment.post = @first_post
31
+ @second_comment.author = nil
32
+ @author1.posts = [@first_post, @third_post]
33
+ @author1.bio = @bio1
34
+ @author1.roles = []
35
+ @author2.posts = [@second_post]
36
+ @author2.bio = @bio2
37
+ @author2.roles = []
38
+ @bio1.author = @author1
39
+ @bio2.author = @author2
40
+ end
41
+
42
+ def test_include_multiple_posts_and_linked_array
43
+ serializer = ArraySerializer.new([@first_post, @second_post])
44
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
45
+ serializer,
46
+ include: [:comments, author: [:bio]]
47
+ )
48
+ alt_adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
49
+ serializer,
50
+ include: [:comments, author: [:bio]]
51
+ )
52
+
53
+ expected = {
54
+ data: [
55
+ {
56
+ id: '10',
57
+ type: 'posts',
58
+ attributes: {
59
+ title: 'Hello!!',
60
+ body: 'Hello, world!!'
61
+ },
62
+ relationships: {
63
+ comments: { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] },
64
+ blog: { data: { type: 'blogs', id: '999' } },
65
+ author: { data: { type: 'authors', id: '1' } }
66
+ }
67
+ },
68
+ {
69
+ id: '20',
70
+ type: 'posts',
71
+ attributes: {
72
+ title: 'New Post',
73
+ body: 'Body'
74
+ },
75
+ relationships: {
76
+ comments: { data: [] },
77
+ blog: { data: { type: 'blogs', id: '999' } },
78
+ author: { data: { type: 'authors', id: '2' } }
79
+ }
80
+ }
81
+ ],
82
+ included: [
83
+ {
84
+ id: '1',
85
+ type: 'comments',
86
+ attributes: {
87
+ body: 'ZOMG A COMMENT'
88
+ },
89
+ relationships: {
90
+ post: { data: { type: 'posts', id: '10' } },
91
+ author: { data: nil }
92
+ }
93
+ }, {
94
+ id: '2',
95
+ type: 'comments',
96
+ attributes: {
97
+ body: 'ZOMG ANOTHER COMMENT',
98
+ },
99
+ relationships: {
100
+ post: { data: { type: 'posts', id: '10' } },
101
+ author: { data: nil }
102
+ }
103
+ }, {
104
+ id: '1',
105
+ type: 'authors',
106
+ attributes: {
107
+ name: 'Steve K.'
108
+ },
109
+ relationships: {
110
+ posts: { data: [{ type: 'posts', id: '10' }, { type: 'posts', id: '30' }] },
111
+ roles: { data: [] },
112
+ bio: { data: { type: 'bios', id: '1' } }
113
+ }
114
+ }, {
115
+ id: '1',
116
+ type: 'bios',
117
+ attributes: {
118
+ content: 'AMS Contributor',
119
+ rating: nil
120
+ },
121
+ relationships: {
122
+ author: { data: { type: 'authors', id: '1' } }
123
+ }
124
+ }, {
125
+ id: '2',
126
+ type: 'authors',
127
+ attributes: {
128
+ name: 'Tenderlove'
129
+ },
130
+ relationships: {
131
+ posts: { data: [{ type: 'posts', id: '20' }] },
132
+ roles: { data: [] },
133
+ bio: { data: { type: 'bios', id: '2' } }
134
+ }
135
+ }, {
136
+ id: '2',
137
+ type: 'bios',
138
+ attributes: {
139
+ rating: nil,
140
+ content: 'Rails Contributor',
141
+ },
142
+ relationships: {
143
+ author: { data: { type: 'authors', id: '2' } }
144
+ }
145
+ }
146
+ ]
147
+ }
148
+ assert_equal expected, adapter.serializable_hash
149
+ assert_equal expected, alt_adapter.serializable_hash
150
+ end
151
+
152
+ def test_include_multiple_posts_and_linked
153
+ serializer = BioSerializer.new @bio1
154
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
155
+ serializer,
156
+ include: [author: [:posts]]
157
+ )
158
+ alt_adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
159
+ serializer,
160
+ include: [author: [:posts]]
161
+ )
162
+
163
+ expected = [
164
+ {
165
+ id: '1',
166
+ type: 'authors',
167
+ attributes: {
168
+ name: 'Steve K.'
169
+ },
170
+ relationships: {
171
+ posts: { data: [{ type: 'posts', id: '10' }, { type: 'posts', id: '30' }] },
172
+ roles: { data: [] },
173
+ bio: { data: { type: 'bios', id: '1' } }
174
+ }
175
+ }, {
176
+ id: '10',
177
+ type: 'posts',
178
+ attributes: {
179
+ title: 'Hello!!',
180
+ body: 'Hello, world!!'
181
+ },
182
+ relationships: {
183
+ comments: { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] },
184
+ blog: { data: { type: 'blogs', id: '999' } },
185
+ author: { data: { type: 'authors', id: '1' } }
186
+ }
187
+ }, {
188
+ id: '30',
189
+ type: 'posts',
190
+ attributes: {
191
+ title: 'Yet Another Post',
192
+ body: 'Body'
193
+ },
194
+ relationships: {
195
+ comments: { data: [] },
196
+ blog: { data: { type: 'blogs', id: '999' } },
197
+ author: { data: { type: 'authors', id: '1' } }
198
+ }
199
+ }
200
+ ]
201
+
202
+ assert_equal expected, adapter.serializable_hash[:included]
203
+ assert_equal expected, alt_adapter.serializable_hash[:included]
204
+ end
205
+
206
+ def test_underscore_model_namespace_for_linked_resource_type
207
+ spammy_post = Post.new(id: 123)
208
+ spammy_post.related = [Spam::UnrelatedLink.new(id: 456)]
209
+ serializer = SpammyPostSerializer.new(spammy_post)
210
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
211
+ relationships = adapter.serializable_hash[:data][:relationships]
212
+ expected = {
213
+ related: {
214
+ data: [{
215
+ type: 'spam_unrelated_links',
216
+ id: '456'
217
+ }]
218
+ }
219
+ }
220
+ assert_equal expected, relationships
221
+ end
222
+
223
+ def test_multiple_references_to_same_resource
224
+ serializer = ArraySerializer.new([@first_comment, @second_comment])
225
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
226
+ serializer,
227
+ include: [:post]
228
+ )
229
+
230
+ expected = [
231
+ {
232
+ id: '10',
233
+ type: 'posts',
234
+ attributes: {
235
+ title: 'Hello!!',
236
+ body: 'Hello, world!!'
237
+ },
238
+ relationships: {
239
+ comments: {
240
+ data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }]
241
+ },
242
+ blog: {
243
+ data: { type: 'blogs', id: '999' }
244
+ },
245
+ author: {
246
+ data: { type: 'authors', id: '1' }
247
+ }
248
+ }
249
+ }
250
+ ]
251
+
252
+ assert_equal expected, adapter.serializable_hash[:included]
253
+ end
254
+
255
+ def test_nil_link_with_specified_serializer
256
+ @first_post.author = nil
257
+ serializer = PostPreviewSerializer.new(@first_post)
258
+ adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
259
+ serializer,
260
+ include: [:author]
261
+ )
262
+
263
+ expected = {
264
+ data: {
265
+ id: '10',
266
+ type: 'posts',
267
+ attributes: {
268
+ title: 'Hello!!',
269
+ body: 'Hello, world!!'
270
+ },
271
+ relationships: {
272
+ comments: { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] },
273
+ author: { data: nil }
274
+ }
275
+ }
276
+ }
277
+ assert_equal expected, adapter.serializable_hash
278
+ end
279
+ end
280
+ end
281
+ end
282
+ end
283
+ end
@@ -0,0 +1,115 @@
1
+ require 'test_helper'
2
+ require 'will_paginate/array'
3
+ require 'kaminari'
4
+ require 'kaminari/hooks'
5
+ ::Kaminari::Hooks.init
6
+
7
+ module ActiveModel
8
+ class Serializer
9
+ class Adapter
10
+ class JsonApi
11
+ class PaginationLinksTest < Minitest::Test
12
+ URI = 'http://example.com'
13
+
14
+ def setup
15
+ ActionController::Base.cache_store.clear
16
+ @array = [
17
+ Profile.new({ id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
18
+ Profile.new({ id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
19
+ Profile.new({ id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3' })
20
+ ]
21
+ end
22
+
23
+ def mock_request(query_parameters = {}, original_url = URI)
24
+ context = Minitest::Mock.new
25
+ context.expect(:original_url, original_url)
26
+ context.expect(:query_parameters, query_parameters)
27
+ @options = {}
28
+ @options[:context] = context
29
+ end
30
+
31
+ def load_adapter(paginated_collection, options = {})
32
+ options = options.merge(adapter: :json_api)
33
+ ActiveModel::SerializableResource.new(paginated_collection, options)
34
+ end
35
+
36
+ def using_kaminari
37
+ Kaminari.paginate_array(@array).page(2).per(1)
38
+ end
39
+
40
+ def using_will_paginate
41
+ @array.paginate(page: 2, per_page: 1)
42
+ end
43
+
44
+ def data
45
+ { data: [
46
+ { id: '1', type: 'profiles', attributes: { name: 'Name 1', description: 'Description 1' } },
47
+ { id: '2', type: 'profiles', attributes: { name: 'Name 2', description: 'Description 2' } },
48
+ { id: '3', type: 'profiles', attributes: { name: 'Name 3', description: 'Description 3' } }
49
+ ]
50
+ }
51
+ end
52
+
53
+ def links
54
+ {
55
+ links: {
56
+ self: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
57
+ first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
58
+ prev: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
59
+ next: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
60
+ last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"
61
+ }
62
+ }
63
+ end
64
+
65
+ def expected_response_without_pagination_links
66
+ data
67
+ end
68
+
69
+ def expected_response_with_pagination_links
70
+ {}.tap do |hash|
71
+ hash[:data] = [data.values.flatten.second]
72
+ hash.merge! links
73
+ end
74
+ end
75
+
76
+ def expected_response_with_pagination_links_and_additional_params
77
+ new_links = links[:links].each_with_object({}) { |(key, value), hash| hash[key] = "#{value}&test=test" }
78
+ {}.tap do |hash|
79
+ hash[:data] = [data.values.flatten.second]
80
+ hash.merge! links: new_links
81
+ end
82
+ end
83
+
84
+ def test_pagination_links_using_kaminari
85
+ adapter = load_adapter(using_kaminari)
86
+
87
+ mock_request
88
+ assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
89
+ end
90
+
91
+ def test_pagination_links_using_will_paginate
92
+ adapter = load_adapter(using_will_paginate)
93
+
94
+ mock_request
95
+ assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
96
+ end
97
+
98
+ def test_pagination_links_with_additional_params
99
+ adapter = load_adapter(using_will_paginate)
100
+
101
+ mock_request({ test: 'test' })
102
+ assert_equal expected_response_with_pagination_links_and_additional_params,
103
+ adapter.serializable_hash(@options)
104
+ end
105
+
106
+ def test_not_showing_pagination_links
107
+ adapter = load_adapter(@array)
108
+
109
+ assert_equal expected_response_without_pagination_links, adapter.serializable_hash
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,59 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Adapter
6
+ class JsonApi
7
+ class ResourceTypeConfigTest < Minitest::Test
8
+ def setup
9
+ @author = Author.new(id: 1, name: 'Steve K.')
10
+ @author.bio = nil
11
+ @author.roles = []
12
+ @blog = Blog.new(id: 23, name: 'AMS Blog')
13
+ @post = Post.new(id: 42, title: 'New Post', body: 'Body')
14
+ @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
15
+ @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
16
+ @post.comments = [@comment]
17
+ @post.blog = @blog
18
+ @anonymous_post.comments = []
19
+ @anonymous_post.blog = nil
20
+ @comment.post = @post
21
+ @comment.author = nil
22
+ @post.author = @author
23
+ @anonymous_post.author = nil
24
+ @blog = Blog.new(id: 1, name: 'My Blog!!')
25
+ @blog.writer = @author
26
+ @blog.articles = [@post, @anonymous_post]
27
+ @author.posts = []
28
+ end
29
+
30
+ def with_jsonapi_resource_type type
31
+ old_type = ActiveModel::Serializer.config.jsonapi_resource_type
32
+ ActiveModel::Serializer.config.jsonapi_resource_type = type
33
+ yield
34
+ ensure
35
+ ActiveModel::Serializer.config.jsonapi_resource_type = old_type
36
+ end
37
+
38
+ def test_config_plural
39
+ with_adapter :json_api do
40
+ with_jsonapi_resource_type :plural do
41
+ hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
42
+ assert_equal('comments', hash[:data][:type])
43
+ end
44
+ end
45
+ end
46
+
47
+ def test_config_singular
48
+ with_adapter :json_api do
49
+ with_jsonapi_resource_type :singular do
50
+ hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
51
+ assert_equal('comment', hash[:data][:type])
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Adapter
6
+ class JsonTest < Minitest::Test
7
+ def setup
8
+ ActionController::Base.cache_store.clear
9
+ @author = Author.new(id: 1, name: 'Steve K.')
10
+ @post = Post.new(id: 1, title: 'New Post', body: 'Body')
11
+ @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
12
+ @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
13
+ @post.comments = [@first_comment, @second_comment]
14
+ @first_comment.post = @post
15
+ @second_comment.post = @post
16
+ @post.author = @author
17
+ @blog = Blog.new(id: 1, name: 'My Blog!!')
18
+ @post.blog = @blog
19
+
20
+ @serializer = PostSerializer.new(@post)
21
+ @adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
22
+ end
23
+
24
+ def test_has_many
25
+ assert_equal([
26
+ { id: 1, body: 'ZOMG A COMMENT' },
27
+ { id: 2, body: 'ZOMG ANOTHER COMMENT' }
28
+ ], @adapter.serializable_hash[:post][:comments])
29
+ end
30
+
31
+ def test_custom_keys
32
+ serializer = PostWithCustomKeysSerializer.new(@post)
33
+ adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
34
+
35
+ assert_equal({
36
+ id: 1,
37
+ reviews: [{ id: 1, body: 'ZOMG A COMMENT' },
38
+ { id: 2, body: 'ZOMG ANOTHER COMMENT' }
39
+ ],
40
+ writer: { id: 1, name: 'Steve K.' },
41
+ site: { id: 1, name: 'My Blog!!' }
42
+ }, adapter.serializable_hash[:post])
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Adapter
6
+ class NullTest < Minitest::Test
7
+ def setup
8
+ profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9
+ serializer = ProfileSerializer.new(profile)
10
+
11
+ @adapter = Null.new(serializer)
12
+ end
13
+
14
+ def test_serializable_hash
15
+ assert_equal({}, @adapter.serializable_hash)
16
+ end
17
+
18
+ def test_it_returns_empty_json
19
+ assert_equal('{}', @adapter.to_json)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class AdapterTest < Minitest::Test
6
+ def setup
7
+ profile = Profile.new
8
+ @serializer = ProfileSerializer.new(profile)
9
+ @adapter = ActiveModel::Serializer::Adapter.new(@serializer)
10
+ end
11
+
12
+ def test_serializable_hash_is_abstract_method
13
+ assert_raises(NotImplementedError) do
14
+ @adapter.serializable_hash(only: [:name])
15
+ end
16
+ end
17
+
18
+ def test_serializer
19
+ assert_equal @serializer, @adapter.serializer
20
+ end
21
+
22
+ def test_create_adapter
23
+ adapter = ActiveModel::Serializer::Adapter.create(@serializer)
24
+ assert_equal ActiveModel::Serializer::Adapter::FlattenJson, adapter.class
25
+ end
26
+
27
+ def test_create_adapter_with_override
28
+ adapter = ActiveModel::Serializer::Adapter.create(@serializer, { adapter: :json_api })
29
+ assert_equal ActiveModel::Serializer::Adapter::JsonApi, adapter.class
30
+ end
31
+
32
+ def test_inflected_adapter_class_for_known_adapter
33
+ ActiveSupport::Inflector.inflections(:en) { |inflect| inflect.acronym 'API' }
34
+ klass = ActiveModel::Serializer::Adapter.adapter_class(:json_api)
35
+
36
+ ActiveSupport::Inflector.inflections.acronyms.clear
37
+
38
+ assert_equal ActiveModel::Serializer::Adapter::JsonApi, klass
39
+ end
40
+ end
41
+ end
42
+ end