active_model_serializers 0.8.3 → 0.10.0.rc2

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 (82) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis.yml +18 -20
  4. data/CHANGELOG.md +8 -67
  5. data/CONTRIBUTING.md +31 -0
  6. data/Gemfile +14 -1
  7. data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
  8. data/README.md +169 -495
  9. data/Rakefile +6 -12
  10. data/active_model_serializers.gemspec +21 -19
  11. data/lib/action_controller/serialization.rb +36 -27
  12. data/lib/active_model/serializer/adapter/flatten_json.rb +12 -0
  13. data/lib/active_model/serializer/adapter/fragment_cache.rb +78 -0
  14. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +15 -0
  15. data/lib/active_model/serializer/adapter/json.rb +50 -0
  16. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +23 -0
  17. data/lib/active_model/serializer/adapter/json_api.rb +156 -0
  18. data/lib/active_model/serializer/adapter/null.rb +11 -0
  19. data/lib/active_model/serializer/adapter.rb +96 -0
  20. data/lib/active_model/serializer/array_serializer.rb +35 -0
  21. data/lib/active_model/serializer/configuration.rb +13 -0
  22. data/lib/active_model/serializer/fieldset.rb +40 -0
  23. data/lib/active_model/serializer/railtie.rb +8 -0
  24. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  25. data/lib/active_model/serializer.rb +177 -440
  26. data/lib/active_model_serializers.rb +10 -86
  27. data/lib/generators/serializer/USAGE +0 -3
  28. data/lib/generators/serializer/resource_override.rb +12 -0
  29. data/lib/generators/serializer/serializer_generator.rb +1 -6
  30. data/lib/generators/serializer/templates/serializer.rb +2 -13
  31. data/test/action_controller/adapter_selector_test.rb +53 -0
  32. data/test/action_controller/explicit_serializer_test.rb +134 -0
  33. data/test/action_controller/json_api_linked_test.rb +179 -0
  34. data/test/action_controller/rescue_from_test.rb +32 -0
  35. data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +7 -11
  36. data/test/action_controller/serialization_test.rb +383 -0
  37. data/test/adapter/fragment_cache_test.rb +27 -0
  38. data/test/adapter/json/belongs_to_test.rb +48 -0
  39. data/test/adapter/json/collection_test.rb +73 -0
  40. data/test/adapter/json/has_many_test.rb +36 -0
  41. data/test/adapter/json_api/belongs_to_test.rb +157 -0
  42. data/test/adapter/json_api/collection_test.rb +96 -0
  43. data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
  44. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  45. data/test/adapter/json_api/has_many_test.rb +110 -0
  46. data/test/adapter/json_api/has_one_test.rb +61 -0
  47. data/test/adapter/json_api/linked_test.rb +283 -0
  48. data/test/adapter/json_test.rb +34 -0
  49. data/test/adapter/null_test.rb +25 -0
  50. data/test/adapter_test.rb +43 -0
  51. data/test/array_serializer_test.rb +31 -63
  52. data/test/fixtures/poro.rb +230 -0
  53. data/test/generators/scaffold_controller_generator_test.rb +24 -0
  54. data/test/{generators_test.rb → generators/serializer_generator_test.rb} +2 -36
  55. data/test/serializers/adapter_for_test.rb +50 -0
  56. data/test/serializers/associations_test.rb +127 -0
  57. data/test/serializers/attribute_test.rb +38 -0
  58. data/test/serializers/attributes_test.rb +63 -0
  59. data/test/serializers/cache_test.rb +138 -0
  60. data/test/serializers/configuration_test.rb +15 -0
  61. data/test/serializers/fieldset_test.rb +26 -0
  62. data/test/serializers/meta_test.rb +107 -0
  63. data/test/serializers/options_test.rb +21 -0
  64. data/test/serializers/serializer_for_test.rb +65 -0
  65. data/test/serializers/urls_test.rb +26 -0
  66. data/test/test_helper.rb +28 -16
  67. metadata +109 -43
  68. data/DESIGN.textile +0 -586
  69. data/Gemfile.edge +0 -9
  70. data/bench/perf.rb +0 -43
  71. data/cruft.md +0 -19
  72. data/lib/active_model/array_serializer.rb +0 -104
  73. data/lib/active_model/serializer/associations.rb +0 -233
  74. data/lib/active_record/serializer_override.rb +0 -16
  75. data/lib/generators/resource_override.rb +0 -13
  76. data/test/association_test.rb +0 -592
  77. data/test/caching_test.rb +0 -96
  78. data/test/no_serialization_scope_test.rb +0 -34
  79. data/test/serialization_test.rb +0 -392
  80. data/test/serializer_support_test.rb +0 -51
  81. data/test/serializer_test.rb +0 -1465
  82. data/test/test_fakes.rb +0 -217
@@ -0,0 +1,383 @@
1
+ require 'test_helper'
2
+
3
+ module ActionController
4
+ module Serialization
5
+ class ImplicitSerializerTest < ActionController::TestCase
6
+ class ImplicitSerializationTestController < ActionController::Base
7
+ def render_using_implicit_serializer
8
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9
+ render json: @profile
10
+ end
11
+
12
+ def render_using_custom_root
13
+ with_adapter ActiveModel::Serializer::Adapter::Json do
14
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
15
+ render json: @profile, root: "custom_root"
16
+ end
17
+ end
18
+
19
+ def render_using_custom_root_and_meta
20
+ with_adapter ActiveModel::Serializer::Adapter::Json do
21
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
22
+ render json: @profile, root: "custom_root", meta: { total: 10 }
23
+ end
24
+ end
25
+
26
+ def render_using_default_adapter_root
27
+ with_adapter ActiveModel::Serializer::Adapter::JsonApi do
28
+ # JSON-API adapter sets root by default
29
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
30
+ render json: @profile
31
+ end
32
+ end
33
+
34
+ def render_using_custom_root_in_adapter_with_a_default
35
+ # JSON-API adapter sets root by default
36
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
37
+ render json: @profile, root: "profile", adapter: :json_api
38
+ end
39
+
40
+ def render_array_using_custom_root_and_meta
41
+ with_adapter ActiveModel::Serializer::Adapter::Json do
42
+ array = [
43
+ Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
44
+ Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
45
+ ]
46
+ render json: array, root: "custom_root", meta: { total: 10 }
47
+ end
48
+ end
49
+
50
+ def render_array_using_implicit_serializer
51
+ array = [
52
+ Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
53
+ Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
54
+ ]
55
+ render json: array
56
+ end
57
+
58
+ def render_array_using_implicit_serializer_and_meta
59
+ with_adapter ActiveModel::Serializer::Adapter::JsonApi do
60
+
61
+ @profiles = [
62
+ Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
63
+ ]
64
+
65
+ render json: @profiles, meta: { total: 10 }
66
+ end
67
+ end
68
+
69
+ def render_object_with_cache_enabled
70
+ @comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
71
+ @author = Author.new(id: 1, name: 'Joao Moura.')
72
+ @post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [@comment], author: @author })
73
+
74
+ generate_cached_serializer(@post)
75
+
76
+ @post.title = 'ZOMG a New Post'
77
+ render json: @post
78
+ end
79
+
80
+ def update_and_render_object_with_cache_enabled
81
+ @post.updated_at = DateTime.now
82
+
83
+ generate_cached_serializer(@post)
84
+ render json: @post
85
+ end
86
+
87
+ def render_object_expired_with_cache_enabled
88
+ comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
89
+ author = Author.new(id: 1, name: 'Joao Moura.')
90
+ post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author })
91
+
92
+ generate_cached_serializer(post)
93
+
94
+ post.title = 'ZOMG a New Post'
95
+ sleep 0.1
96
+ render json: post
97
+ end
98
+
99
+ def render_changed_object_with_cache_enabled
100
+ comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
101
+ author = Author.new(id: 1, name: 'Joao Moura.')
102
+ post = Post.new({ id: 1, title: 'ZOMG a New Post', body: 'Body', comments: [comment], author: author })
103
+
104
+ render json: post
105
+ end
106
+
107
+ def render_fragment_changed_object_with_only_cache_enabled
108
+ author = Author.new(id: 1, name: 'Joao Moura.')
109
+ role = Role.new({ id: 42, name: 'ZOMG A ROLE', description: 'DESCRIPTION HERE', author: author })
110
+
111
+ generate_cached_serializer(role)
112
+ role.name = 'lol'
113
+ role.description = 'HUEHUEBRBR'
114
+
115
+ render json: role
116
+ end
117
+
118
+ def render_fragment_changed_object_with_except_cache_enabled
119
+ author = Author.new(id: 1, name: 'Joao Moura.')
120
+ bio = Bio.new({ id: 42, content: 'ZOMG A ROLE', rating: 5, author: author })
121
+
122
+ generate_cached_serializer(bio)
123
+ bio.content = 'lol'
124
+ bio.rating = 0
125
+
126
+ render json: bio
127
+ end
128
+
129
+ def render_fragment_changed_object_with_relationship
130
+ comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
131
+ author = Author.new(id: 1, name: 'Joao Moura.')
132
+ post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author })
133
+ post2 = Post.new({ id: 1, title: 'New Post2', body: 'Body2', comments: [comment], author: author })
134
+ like = Like.new({ id: 1, post: post, time: 3.days.ago })
135
+
136
+ generate_cached_serializer(like)
137
+ like.post = post2
138
+ like.time = DateTime.now.to_s
139
+
140
+ render json: like
141
+ end
142
+
143
+ private
144
+ def generate_cached_serializer(obj)
145
+ serializer_class = ActiveModel::Serializer.serializer_for(obj)
146
+ serializer = serializer_class.new(obj)
147
+ adapter = ActiveModel::Serializer.adapter.new(serializer)
148
+ adapter.to_json
149
+ end
150
+
151
+ def with_adapter(adapter)
152
+ old_adapter = ActiveModel::Serializer.config.adapter
153
+ # JSON-API adapter sets root by default
154
+ ActiveModel::Serializer.config.adapter = adapter
155
+ yield
156
+ ensure
157
+ ActiveModel::Serializer.config.adapter = old_adapter
158
+ end
159
+ end
160
+
161
+ tests ImplicitSerializationTestController
162
+
163
+ # We just have Null for now, this will change
164
+ def test_render_using_implicit_serializer
165
+ get :render_using_implicit_serializer
166
+
167
+ expected = {
168
+ name: "Name 1",
169
+ description: "Description 1"
170
+ }
171
+
172
+ assert_equal 'application/json', @response.content_type
173
+ assert_equal expected.to_json, @response.body
174
+ end
175
+
176
+ def test_render_using_default_root
177
+ get :render_using_default_adapter_root
178
+
179
+ expected = {
180
+ data: {
181
+ id: assigns(:profile).id.to_s,
182
+ type: "profiles",
183
+ attributes: {
184
+ name: "Name 1",
185
+ description: "Description 1"
186
+ }
187
+ }
188
+ }
189
+
190
+ assert_equal 'application/json', @response.content_type
191
+ assert_equal expected.to_json, @response.body
192
+ end
193
+
194
+ def test_render_using_custom_root_in_adapter_with_a_default
195
+ get :render_using_custom_root_in_adapter_with_a_default
196
+
197
+ expected = {
198
+ data: {
199
+ id: assigns(:profile).id.to_s,
200
+ type: "profiles",
201
+ attributes: {
202
+ name: "Name 1",
203
+ description: "Description 1"
204
+ }
205
+ }
206
+ }
207
+
208
+ assert_equal 'application/json', @response.content_type
209
+ assert_equal expected.to_json, @response.body
210
+ end
211
+
212
+ def test_render_array_using_implicit_serializer
213
+ get :render_array_using_implicit_serializer
214
+ assert_equal 'application/json', @response.content_type
215
+
216
+ expected = [
217
+ {
218
+ name: 'Name 1',
219
+ description: 'Description 1',
220
+ },
221
+ {
222
+ name: 'Name 2',
223
+ description: 'Description 2',
224
+ }
225
+ ]
226
+
227
+ assert_equal expected.to_json, @response.body
228
+ end
229
+
230
+ def test_render_array_using_implicit_serializer_and_meta
231
+ get :render_array_using_implicit_serializer_and_meta
232
+
233
+ expected = {
234
+ data: [
235
+ {
236
+ id: assigns(:profiles).first.id.to_s,
237
+ type: "profiles",
238
+ attributes: {
239
+ name: "Name 1",
240
+ description: "Description 1"
241
+ }
242
+ }
243
+ ],
244
+ meta: {
245
+ total: 10
246
+ }
247
+ }
248
+
249
+ assert_equal 'application/json', @response.content_type
250
+ assert_equal expected.to_json, @response.body
251
+ end
252
+
253
+ def test_render_with_cache_enable
254
+ ActionController::Base.cache_store.clear
255
+ get :render_object_with_cache_enabled
256
+
257
+ expected = {
258
+ id: 1,
259
+ title: 'New Post',
260
+ body: 'Body',
261
+ comments: [
262
+ {
263
+ id: 1,
264
+ body: 'ZOMG A COMMENT' }
265
+ ],
266
+ blog: {
267
+ id: 999,
268
+ name: 'Custom blog'
269
+ },
270
+ author: {
271
+ id: 1,
272
+ name: 'Joao Moura.'
273
+ }
274
+ }
275
+
276
+ assert_equal 'application/json', @response.content_type
277
+ assert_equal expected.to_json, @response.body
278
+
279
+ get :render_changed_object_with_cache_enabled
280
+ assert_equal expected.to_json, @response.body
281
+
282
+ ActionController::Base.cache_store.clear
283
+ get :render_changed_object_with_cache_enabled
284
+ assert_not_equal expected.to_json, @response.body
285
+ end
286
+
287
+ def test_render_with_cache_enable_and_expired
288
+ ActionController::Base.cache_store.clear
289
+ get :render_object_expired_with_cache_enabled
290
+
291
+ expected = {
292
+ id: 1,
293
+ title: 'ZOMG a New Post',
294
+ body: 'Body',
295
+ comments: [
296
+ {
297
+ id: 1,
298
+ body: 'ZOMG A COMMENT' }
299
+ ],
300
+ blog: {
301
+ id: 999,
302
+ name: 'Custom blog'
303
+ },
304
+ author: {
305
+ id: 1,
306
+ name: 'Joao Moura.'
307
+ }
308
+ }
309
+
310
+ assert_equal 'application/json', @response.content_type
311
+ assert_equal expected.to_json, @response.body
312
+ end
313
+
314
+ def test_render_with_fragment_only_cache_enable
315
+ ActionController::Base.cache_store.clear
316
+ get :render_fragment_changed_object_with_only_cache_enabled
317
+ response = JSON.parse(@response.body)
318
+
319
+ assert_equal 'application/json', @response.content_type
320
+ assert_equal 'ZOMG A ROLE', response["name"]
321
+ assert_equal 'HUEHUEBRBR', response["description"]
322
+ end
323
+
324
+ def test_render_with_fragment_except_cache_enable
325
+ ActionController::Base.cache_store.clear
326
+ get :render_fragment_changed_object_with_except_cache_enabled
327
+ response = JSON.parse(@response.body)
328
+
329
+ assert_equal 'application/json', @response.content_type
330
+ assert_equal 5, response["rating"]
331
+ assert_equal 'lol', response["content"]
332
+ end
333
+
334
+ def test_render_fragment_changed_object_with_relationship
335
+ ActionController::Base.cache_store.clear
336
+ get :render_fragment_changed_object_with_relationship
337
+ response = JSON.parse(@response.body)
338
+
339
+ expected_return = {
340
+ "id"=>1,
341
+ "time"=>DateTime.now.to_s,
342
+ "post" => {
343
+ "id"=>1,
344
+ "title"=>"New Post",
345
+ "body"=>"Body"
346
+ }
347
+ }
348
+
349
+ assert_equal 'application/json', @response.content_type
350
+ assert_equal expected_return, response
351
+ end
352
+
353
+ def test_cache_expiration_on_update
354
+ ActionController::Base.cache_store.clear
355
+ get :render_object_with_cache_enabled
356
+
357
+ expected = {
358
+ id: 1,
359
+ title: 'ZOMG a New Post',
360
+ body: 'Body',
361
+ comments: [
362
+ {
363
+ id: 1,
364
+ body: 'ZOMG A COMMENT' }
365
+ ],
366
+ blog: {
367
+ id:999,
368
+ name: "Custom blog"
369
+ },
370
+ author: {
371
+ id: 1,
372
+ name: 'Joao Moura.'
373
+ }
374
+ }
375
+
376
+ get :update_and_render_object_with_cache_enabled
377
+
378
+ assert_equal 'application/json', @response.content_type
379
+ assert_equal expected.to_json, @response.body
380
+ end
381
+ end
382
+ end
383
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+ module ActiveModel
3
+ class Serializer
4
+ class Adapter
5
+ class FragmentCacheTest < Minitest::Test
6
+ def setup
7
+ @author = Author.new(name: 'Joao M. D. Moura')
8
+ @role = Role.new(name: 'Great Author', description:nil)
9
+ @role.author = [@author]
10
+ @role_serializer = RoleSerializer.new(@role)
11
+ @role_hash = FragmentCache.new(RoleSerializer.adapter.new(@role_serializer), @role_serializer, {})
12
+ end
13
+
14
+ def test_fragment_fetch_with_virtual_attributes
15
+ expected_result = {
16
+ id: @role.id,
17
+ description: @role.description,
18
+ slug: "#{@role.name}-#{@role.id}",
19
+ name: @role.name
20
+ }
21
+ assert_equal(@role_hash.fetch, expected_result)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Adapter
6
+ class Json
7
+ class BelongsToTest < Minitest::Test
8
+ def setup
9
+ @post = Post.new(id: 42, title: 'New Post', body: 'Body')
10
+ @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
11
+ @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
12
+ @post.comments = [@comment]
13
+ @anonymous_post.comments = []
14
+ @post.author = @author
15
+ @comment.post = @post
16
+ @comment.author = nil
17
+ @anonymous_post.author = nil
18
+ @blog = Blog.new(id: 1, name: "My Blog!!")
19
+ @post.blog = @blog
20
+ @anonymous_post.blog = nil
21
+
22
+ @serializer = CommentSerializer.new(@comment)
23
+ @adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
24
+ ActionController::Base.cache_store.clear
25
+ end
26
+
27
+ def test_includes_post
28
+ assert_equal({id: 42, title: 'New Post', body: 'Body'}, @adapter.serializable_hash[:comment][:post])
29
+ end
30
+
31
+ def test_include_nil_author
32
+ serializer = PostSerializer.new(@anonymous_post)
33
+ adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
34
+
35
+ assert_equal({post: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: {id: 999, name: "Custom blog"}, author: nil}}, adapter.serializable_hash)
36
+ end
37
+
38
+ def test_include_nil_author_with_specified_serializer
39
+ serializer = PostPreviewSerializer.new(@anonymous_post)
40
+ adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
41
+
42
+ assert_equal({posts: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}}, adapter.serializable_hash)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,73 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Adapter
6
+ class Json
7
+ class Collection < Minitest::Test
8
+ def setup
9
+ @author = Author.new(id: 1, name: 'Steve K.')
10
+ @first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
11
+ @second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
12
+ @first_post.comments = []
13
+ @second_post.comments = []
14
+ @first_post.author = @author
15
+ @second_post.author = @author
16
+ @blog = Blog.new(id: 1, name: "My Blog!!")
17
+ @first_post.blog = @blog
18
+ @second_post.blog = nil
19
+
20
+ @serializer = ArraySerializer.new([@first_post, @second_post])
21
+ @adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
22
+ ActionController::Base.cache_store.clear
23
+ end
24
+
25
+ def test_with_serializer_option
26
+ @blog.special_attribute = "Special"
27
+ @blog.articles = [@first_post, @second_post]
28
+ @serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
29
+ @adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
30
+
31
+ expected = {custom_blogs:[{
32
+ id: 1,
33
+ special_attribute: "Special",
34
+ articles: [{id: 1,title: "Hello!!", body: "Hello, world!!"}, {id: 2, title: "New Post", body: "Body"}]
35
+ }]}
36
+ assert_equal expected, @adapter.serializable_hash
37
+ end
38
+
39
+ def test_include_multiple_posts
40
+ expected = { posts: [{
41
+ title: "Hello!!",
42
+ body: "Hello, world!!",
43
+ id: 1,
44
+ comments: [],
45
+ author: {
46
+ id: 1,
47
+ name: "Steve K."
48
+ },
49
+ blog: {
50
+ id: 999,
51
+ name: "Custom blog"
52
+ }
53
+ }, {
54
+ title: "New Post",
55
+ body: "Body",
56
+ id: 2,
57
+ comments: [],
58
+ author: {
59
+ id: 1,
60
+ name: "Steve K."
61
+ },
62
+ blog: {
63
+ id: 999,
64
+ name: "Custom blog"
65
+ }
66
+ }]}
67
+ assert_equal expected, @adapter.serializable_hash
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Adapter
6
+ class Json
7
+ class HasManyTestTest < Minitest::Test
8
+ def setup
9
+ ActionController::Base.cache_store.clear
10
+ @author = Author.new(id: 1, name: 'Steve K.')
11
+ @post = Post.new(title: 'New Post', body: 'Body')
12
+ @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
13
+ @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
14
+ @post.comments = [@first_comment, @second_comment]
15
+ @post.author = @author
16
+ @first_comment.post = @post
17
+ @second_comment.post = @post
18
+ @blog = Blog.new(id: 1, name: "My Blog!!")
19
+ @post.blog = @blog
20
+
21
+ @serializer = PostSerializer.new(@post)
22
+ @adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
23
+ end
24
+
25
+ def test_has_many
26
+ assert_equal([
27
+ {id: 1, body: 'ZOMG A COMMENT'},
28
+ {id: 2, body: 'ZOMG ANOTHER COMMENT'}
29
+ ], @adapter.serializable_hash[:post][:comments])
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+