active_model_serializers 0.1.0 → 0.5.0
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.
- data/{README.textile → DESIGN.textile} +31 -3
- data/Gemfile +3 -0
- data/MIT-LICENSE.txt +21 -0
- data/README.markdown +270 -0
- data/RELEASE_NOTES.md +4 -0
- data/active_model_serializers.gemspec +5 -1
- data/lib/action_controller/serialization.rb +14 -3
- data/lib/active_model/serializer.rb +231 -78
- data/lib/active_model/serializers/version.rb +5 -0
- data/lib/active_model_serializers.rb +11 -0
- data/lib/generators/resource_override.rb +13 -0
- data/lib/generators/serializer/serializer_generator.rb +1 -1
- data/test/association_test.rb +392 -0
- data/test/generators_test.rb +3 -3
- data/test/serialization_test.rb +50 -4
- data/test/serializer_test.rb +123 -93
- data/test/test_helper.rb +10 -0
- metadata +25 -8
data/test/serialization_test.rb
CHANGED
@@ -15,15 +15,20 @@ class RenderJsonTest < ActionController::TestCase
|
|
15
15
|
end
|
16
16
|
|
17
17
|
class JsonSerializer
|
18
|
-
def initialize(object,
|
19
|
-
@object, @
|
18
|
+
def initialize(object, options={})
|
19
|
+
@object, @options = object, options
|
20
20
|
end
|
21
21
|
|
22
22
|
def as_json(*)
|
23
|
-
hash = { :object =>
|
23
|
+
hash = { :object => serializable_hash, :scope => @options[:scope].as_json }
|
24
24
|
hash.merge!(:options => true) if @options[:options]
|
25
|
+
hash.merge!(:check_defaults => true) if @options[:check_defaults]
|
25
26
|
hash
|
26
27
|
end
|
28
|
+
|
29
|
+
def serializable_hash
|
30
|
+
@object.as_json
|
31
|
+
end
|
27
32
|
end
|
28
33
|
|
29
34
|
class JsonSerializable
|
@@ -40,6 +45,15 @@ class RenderJsonTest < ActionController::TestCase
|
|
40
45
|
end
|
41
46
|
end
|
42
47
|
|
48
|
+
class CustomSerializer
|
49
|
+
def initialize(*)
|
50
|
+
end
|
51
|
+
|
52
|
+
def as_json(*)
|
53
|
+
{ :hello => true }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
43
57
|
class TestController < ActionController::Base
|
44
58
|
protect_from_forgery
|
45
59
|
|
@@ -91,6 +105,11 @@ class RenderJsonTest < ActionController::TestCase
|
|
91
105
|
render :json => JsonSerializable.new
|
92
106
|
end
|
93
107
|
|
108
|
+
def render_json_with_serializer_and_implicit_root
|
109
|
+
@current_user = Struct.new(:as_json).new(:current_user => true)
|
110
|
+
render :json => [JsonSerializable.new]
|
111
|
+
end
|
112
|
+
|
94
113
|
def render_json_with_serializer_and_options
|
95
114
|
@current_user = Struct.new(:as_json).new(:current_user => true)
|
96
115
|
render :json => JsonSerializable.new, :options => true
|
@@ -100,6 +119,17 @@ class RenderJsonTest < ActionController::TestCase
|
|
100
119
|
@current_user = Struct.new(:as_json).new(:current_user => true)
|
101
120
|
render :json => JsonSerializable.new(true)
|
102
121
|
end
|
122
|
+
|
123
|
+
def render_json_with_custom_serializer
|
124
|
+
render :json => [], :serializer => CustomSerializer
|
125
|
+
end
|
126
|
+
|
127
|
+
private
|
128
|
+
def default_serializer_options
|
129
|
+
if params[:check_defaults]
|
130
|
+
{ :check_defaults => true }
|
131
|
+
end
|
132
|
+
end
|
103
133
|
end
|
104
134
|
|
105
135
|
tests TestController
|
@@ -124,7 +154,6 @@ class RenderJsonTest < ActionController::TestCase
|
|
124
154
|
assert_equal '[]', @response.body
|
125
155
|
end
|
126
156
|
|
127
|
-
|
128
157
|
def test_render_json
|
129
158
|
get :render_json_hello_world
|
130
159
|
assert_equal '{"hello":"world"}', @response.body
|
@@ -172,6 +201,18 @@ class RenderJsonTest < ActionController::TestCase
|
|
172
201
|
assert_match '"object":{"serializable_object":true}', @response.body
|
173
202
|
end
|
174
203
|
|
204
|
+
def test_render_json_with_serializer
|
205
|
+
get :render_json_with_serializer, :check_defaults => true
|
206
|
+
assert_match '"scope":{"current_user":true}', @response.body
|
207
|
+
assert_match '"object":{"serializable_object":true}', @response.body
|
208
|
+
assert_match '"check_defaults":true', @response.body
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_render_json_with_serializer_and_implicit_root
|
212
|
+
get :render_json_with_serializer_and_implicit_root
|
213
|
+
assert_match '"test":[{"serializable_object":true}]', @response.body
|
214
|
+
end
|
215
|
+
|
175
216
|
def test_render_json_with_serializer_and_options
|
176
217
|
get :render_json_with_serializer_and_options
|
177
218
|
assert_match '"scope":{"current_user":true}', @response.body
|
@@ -183,4 +224,9 @@ class RenderJsonTest < ActionController::TestCase
|
|
183
224
|
get :render_json_with_serializer_api_but_without_serializer
|
184
225
|
assert_match '{"serializable_object":true}', @response.body
|
185
226
|
end
|
227
|
+
|
228
|
+
def test_render_json_with_custom_serializer
|
229
|
+
get :render_json_with_custom_serializer
|
230
|
+
assert_match '{"hello":true}', @response.body
|
231
|
+
end
|
186
232
|
end
|
data/test/serializer_test.rb
CHANGED
@@ -37,9 +37,10 @@ class SerializerTest < ActiveModel::TestCase
|
|
37
37
|
def initialize(attributes)
|
38
38
|
super(attributes)
|
39
39
|
self.comments ||= []
|
40
|
+
self.author = nil
|
40
41
|
end
|
41
42
|
|
42
|
-
attr_accessor :comments
|
43
|
+
attr_accessor :comments, :author
|
43
44
|
def active_model_serializer; PostSerializer; end
|
44
45
|
end
|
45
46
|
|
@@ -51,7 +52,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
51
52
|
attributes :first_name, :last_name
|
52
53
|
|
53
54
|
def serializable_hash
|
54
|
-
attributes.merge(:ok => true).merge(scope)
|
55
|
+
attributes.merge(:ok => true).merge(options[:scope])
|
55
56
|
end
|
56
57
|
end
|
57
58
|
|
@@ -106,7 +107,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
106
107
|
|
107
108
|
def test_attributes_method
|
108
109
|
user = User.new
|
109
|
-
user_serializer = UserSerializer.new(user, {})
|
110
|
+
user_serializer = UserSerializer.new(user, :scope => {})
|
110
111
|
|
111
112
|
hash = user_serializer.as_json
|
112
113
|
|
@@ -117,7 +118,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
117
118
|
|
118
119
|
def test_serializer_receives_scope
|
119
120
|
user = User.new
|
120
|
-
user_serializer = UserSerializer.new(user, {:scope => true})
|
121
|
+
user_serializer = UserSerializer.new(user, :scope => {:scope => true})
|
121
122
|
|
122
123
|
hash = user_serializer.as_json
|
123
124
|
|
@@ -134,7 +135,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
134
135
|
def test_pretty_accessors
|
135
136
|
user = User.new
|
136
137
|
user.superuser = true
|
137
|
-
user_serializer = MyUserSerializer.new(user
|
138
|
+
user_serializer = MyUserSerializer.new(user)
|
138
139
|
|
139
140
|
hash = user_serializer.as_json
|
140
141
|
|
@@ -152,7 +153,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
152
153
|
comments = [Comment.new(:title => "Comment1"), Comment.new(:title => "Comment2")]
|
153
154
|
post.comments = comments
|
154
155
|
|
155
|
-
post_serializer = PostSerializer.new(post, user)
|
156
|
+
post_serializer = PostSerializer.new(post, :scope => user)
|
156
157
|
|
157
158
|
assert_equal({
|
158
159
|
:post => {
|
@@ -183,7 +184,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
183
184
|
blog = Blog.new
|
184
185
|
blog.author = user
|
185
186
|
|
186
|
-
json = BlogSerializer.new(blog, user).as_json
|
187
|
+
json = BlogSerializer.new(blog, :scope => user).as_json
|
187
188
|
assert_equal({
|
188
189
|
:blog => {
|
189
190
|
:author => {
|
@@ -211,7 +212,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
211
212
|
blog = Blog.new
|
212
213
|
blog.author = user
|
213
214
|
|
214
|
-
json = blog_serializer.new(blog, user).as_json
|
215
|
+
json = blog_serializer.new(blog, :scope => user).as_json
|
215
216
|
assert_equal({
|
216
217
|
:person => {
|
217
218
|
:first_name => "Jose"
|
@@ -219,84 +220,28 @@ class SerializerTest < ActiveModel::TestCase
|
|
219
220
|
}, json)
|
220
221
|
end
|
221
222
|
|
222
|
-
def post_serializer
|
223
|
+
def post_serializer
|
223
224
|
Class.new(ActiveModel::Serializer) do
|
224
225
|
attributes :title, :body
|
225
226
|
has_many :comments, :serializer => CommentSerializer
|
226
|
-
|
227
|
-
if type != :super
|
228
|
-
define_method :serializable_hash do
|
229
|
-
post_hash = attributes
|
230
|
-
post_hash.merge!(send(type))
|
231
|
-
post_hash
|
232
|
-
end
|
233
|
-
end
|
227
|
+
has_one :author, :serializer => DefaultUserSerializer
|
234
228
|
end
|
235
229
|
end
|
236
230
|
|
237
|
-
def test_associations
|
238
|
-
post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
|
239
|
-
comments = [Comment.new(:title => "Comment1"), Comment.new(:title => "Comment2")]
|
240
|
-
post.comments = comments
|
241
|
-
|
242
|
-
serializer = post_serializer(:associations).new(post, nil)
|
243
|
-
|
244
|
-
assert_equal({
|
245
|
-
:title => "New Post",
|
246
|
-
:body => "Body of new post",
|
247
|
-
:comments => [
|
248
|
-
{ :title => "Comment1" },
|
249
|
-
{ :title => "Comment2" }
|
250
|
-
]
|
251
|
-
}, serializer.as_json)
|
252
|
-
end
|
253
|
-
|
254
|
-
def test_association_ids
|
255
|
-
serializer = post_serializer(:association_ids)
|
256
|
-
|
257
|
-
serializer.class_eval do
|
258
|
-
def as_json(*)
|
259
|
-
{ :post => serializable_hash }.merge(associations)
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
|
264
|
-
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
|
265
|
-
post.comments = comments
|
266
|
-
|
267
|
-
serializer = serializer.new(post, nil)
|
268
|
-
|
269
|
-
assert_equal({
|
270
|
-
:post => {
|
271
|
-
:title => "New Post",
|
272
|
-
:body => "Body of new post",
|
273
|
-
:comments => [1, 2]
|
274
|
-
},
|
275
|
-
:comments => [
|
276
|
-
{ :title => "Comment1" },
|
277
|
-
{ :title => "Comment2" }
|
278
|
-
]
|
279
|
-
}, serializer.as_json)
|
280
|
-
end
|
281
|
-
|
282
231
|
def test_associations_with_nil_association
|
283
232
|
user = User.new
|
284
233
|
blog = Blog.new
|
285
234
|
|
286
|
-
json = BlogSerializer.new(blog, user).as_json
|
235
|
+
json = BlogSerializer.new(blog, :scope => user).as_json
|
287
236
|
assert_equal({
|
288
237
|
:blog => { :author => nil }
|
289
238
|
}, json)
|
290
239
|
|
291
240
|
serializer = Class.new(BlogSerializer) do
|
292
241
|
root :blog
|
293
|
-
|
294
|
-
def serializable_hash
|
295
|
-
attributes.merge(association_ids)
|
296
|
-
end
|
297
242
|
end
|
298
243
|
|
299
|
-
json = serializer.new(blog, user).as_json
|
244
|
+
json = serializer.new(blog, :scope => user).as_json
|
300
245
|
assert_equal({ :blog => { :author => nil } }, json)
|
301
246
|
end
|
302
247
|
|
@@ -308,7 +253,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
308
253
|
root :my_blog
|
309
254
|
end
|
310
255
|
|
311
|
-
assert_equal({ :my_blog => { :author => nil } }, serializer.new(blog, user).as_json)
|
256
|
+
assert_equal({ :my_blog => { :author => nil } }, serializer.new(blog, :scope => user).as_json)
|
312
257
|
end
|
313
258
|
|
314
259
|
def test_false_root
|
@@ -319,15 +264,15 @@ class SerializerTest < ActiveModel::TestCase
|
|
319
264
|
root false
|
320
265
|
end
|
321
266
|
|
322
|
-
assert_equal({ :author => nil }, serializer.new(blog, user).as_json)
|
267
|
+
assert_equal({ :author => nil }, serializer.new(blog, :scope => user).as_json)
|
323
268
|
|
324
269
|
# test inherited false root
|
325
270
|
serializer = Class.new(serializer)
|
326
|
-
assert_equal({ :author => nil }, serializer.new(blog, user).as_json)
|
271
|
+
assert_equal({ :author => nil }, serializer.new(blog, :scope => user).as_json)
|
327
272
|
end
|
328
273
|
|
329
274
|
def test_embed_ids
|
330
|
-
serializer = post_serializer
|
275
|
+
serializer = post_serializer
|
331
276
|
|
332
277
|
serializer.class_eval do
|
333
278
|
root :post
|
@@ -338,21 +283,22 @@ class SerializerTest < ActiveModel::TestCase
|
|
338
283
|
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
|
339
284
|
post.comments = comments
|
340
285
|
|
341
|
-
serializer = serializer.new(post
|
286
|
+
serializer = serializer.new(post)
|
342
287
|
|
343
288
|
assert_equal({
|
344
289
|
:post => {
|
345
290
|
:title => "New Post",
|
346
291
|
:body => "Body of new post",
|
347
|
-
:comments => [1, 2]
|
292
|
+
:comments => [1, 2],
|
293
|
+
:author => nil
|
348
294
|
}
|
349
295
|
}, serializer.as_json)
|
350
296
|
end
|
351
297
|
|
352
298
|
def test_embed_ids_include_true
|
353
|
-
|
299
|
+
serializer_class = post_serializer
|
354
300
|
|
355
|
-
|
301
|
+
serializer_class.class_eval do
|
356
302
|
root :post
|
357
303
|
embed :ids, :include => true
|
358
304
|
end
|
@@ -361,23 +307,43 @@ class SerializerTest < ActiveModel::TestCase
|
|
361
307
|
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
|
362
308
|
post.comments = comments
|
363
309
|
|
364
|
-
serializer =
|
310
|
+
serializer = serializer_class.new(post)
|
365
311
|
|
366
312
|
assert_equal({
|
367
313
|
:post => {
|
368
314
|
:title => "New Post",
|
369
315
|
:body => "Body of new post",
|
370
|
-
:comments => [1, 2]
|
316
|
+
:comments => [1, 2],
|
317
|
+
:author => nil
|
371
318
|
},
|
372
319
|
:comments => [
|
373
320
|
{ :title => "Comment1" },
|
374
321
|
{ :title => "Comment2" }
|
375
|
-
]
|
322
|
+
],
|
323
|
+
:authors => []
|
324
|
+
}, serializer.as_json)
|
325
|
+
|
326
|
+
post.author = User.new(:id => 1)
|
327
|
+
|
328
|
+
serializer = serializer_class.new(post)
|
329
|
+
|
330
|
+
assert_equal({
|
331
|
+
:post => {
|
332
|
+
:title => "New Post",
|
333
|
+
:body => "Body of new post",
|
334
|
+
:comments => [1, 2],
|
335
|
+
:author => 1
|
336
|
+
},
|
337
|
+
:comments => [
|
338
|
+
{ :title => "Comment1" },
|
339
|
+
{ :title => "Comment2" }
|
340
|
+
],
|
341
|
+
:authors => [{ :first_name => "Jose", :last_name => "Valim" }]
|
376
342
|
}, serializer.as_json)
|
377
343
|
end
|
378
344
|
|
379
345
|
def test_embed_objects
|
380
|
-
serializer = post_serializer
|
346
|
+
serializer = post_serializer
|
381
347
|
|
382
348
|
serializer.class_eval do
|
383
349
|
root :post
|
@@ -388,12 +354,13 @@ class SerializerTest < ActiveModel::TestCase
|
|
388
354
|
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
|
389
355
|
post.comments = comments
|
390
356
|
|
391
|
-
serializer = serializer.new(post
|
357
|
+
serializer = serializer.new(post)
|
392
358
|
|
393
359
|
assert_equal({
|
394
360
|
:post => {
|
395
361
|
:title => "New Post",
|
396
362
|
:body => "Body of new post",
|
363
|
+
:author => nil,
|
397
364
|
:comments => [
|
398
365
|
{ :title => "Comment1" },
|
399
366
|
{ :title => "Comment2" }
|
@@ -416,6 +383,20 @@ class SerializerTest < ActiveModel::TestCase
|
|
416
383
|
], serializer.as_json)
|
417
384
|
end
|
418
385
|
|
386
|
+
def test_array_serializer
|
387
|
+
comment1 = Comment.new(:title => "Comment1", :id => 1)
|
388
|
+
comment2 = Comment.new(:title => "Comment2", :id => 2)
|
389
|
+
|
390
|
+
array = [ comment1, comment2 ]
|
391
|
+
|
392
|
+
serializer = array.active_model_serializer.new(array, :root => :comments)
|
393
|
+
|
394
|
+
assert_equal({ :comments => [
|
395
|
+
{ :title => "Comment1" },
|
396
|
+
{ :title => "Comment2" }
|
397
|
+
]}, serializer.as_json)
|
398
|
+
end
|
399
|
+
|
419
400
|
class CustomBlog < Blog
|
420
401
|
attr_accessor :public_posts, :public_user
|
421
402
|
end
|
@@ -436,7 +417,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
436
417
|
custom_blog.public_posts = posts
|
437
418
|
custom_blog.public_user = user
|
438
419
|
|
439
|
-
serializer = CustomBlogSerializer.new(custom_blog, :scope => true)
|
420
|
+
serializer = CustomBlogSerializer.new(custom_blog, :scope => { :scope => true })
|
440
421
|
|
441
422
|
assert_equal({
|
442
423
|
:custom_blog => {
|
@@ -473,7 +454,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
473
454
|
custom_blog.public_posts = posts
|
474
455
|
custom_blog.public_user = user
|
475
456
|
|
476
|
-
serializer = implicit_serializer.new(custom_blog, :scope => true)
|
457
|
+
serializer = implicit_serializer.new(custom_blog, :scope => { :scope => true })
|
477
458
|
|
478
459
|
assert_equal({
|
479
460
|
:custom_blog => {
|
@@ -499,7 +480,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
499
480
|
attribute :password
|
500
481
|
end
|
501
482
|
|
502
|
-
serializer = serializer_class.new(User.new
|
483
|
+
serializer = serializer_class.new(User.new)
|
503
484
|
|
504
485
|
assert_equal({
|
505
486
|
:user => {
|
@@ -594,7 +575,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
594
575
|
author = author_class.new(:id => 5)
|
595
576
|
post.author = author
|
596
577
|
|
597
|
-
hash = serializer_class.new(post
|
578
|
+
hash = serializer_class.new(post)
|
598
579
|
|
599
580
|
assert_equal({
|
600
581
|
:post => {
|
@@ -627,7 +608,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
627
608
|
author = author_class.new(:id => 5, :name => "Tom Dale")
|
628
609
|
post.author = author
|
629
610
|
|
630
|
-
hash = serializer_class.new(post
|
611
|
+
hash = serializer_class.new(post)
|
631
612
|
|
632
613
|
assert_equal({
|
633
614
|
:post => {
|
@@ -666,13 +647,13 @@ class SerializerTest < ActiveModel::TestCase
|
|
666
647
|
:body => "It's a new post!",
|
667
648
|
:author => { :id => 5, :name => "Tom Dale" }
|
668
649
|
}
|
669
|
-
}, serializer_class.new(post,
|
650
|
+
}, serializer_class.new(post, :root => :blog_post).as_json)
|
670
651
|
|
671
652
|
assert_equal({
|
672
653
|
:title => "New Post",
|
673
654
|
:body => "It's a new post!",
|
674
655
|
:author => { :id => 5, :name => "Tom Dale" }
|
675
|
-
}, serializer_class.new(post,
|
656
|
+
}, serializer_class.new(post, :root => false).as_json)
|
676
657
|
|
677
658
|
assert_equal({
|
678
659
|
:blog_post => {
|
@@ -680,13 +661,13 @@ class SerializerTest < ActiveModel::TestCase
|
|
680
661
|
:body => "It's a new post!",
|
681
662
|
:author => { :id => 5, :name => "Tom Dale" }
|
682
663
|
}
|
683
|
-
}, serializer_class.new(post
|
664
|
+
}, serializer_class.new(post).as_json(:root => :blog_post))
|
684
665
|
|
685
666
|
assert_equal({
|
686
667
|
:title => "New Post",
|
687
668
|
:body => "It's a new post!",
|
688
669
|
:author => { :id => 5, :name => "Tom Dale" }
|
689
|
-
}, serializer_class.new(post
|
670
|
+
}, serializer_class.new(post).as_json(:root => false))
|
690
671
|
end
|
691
672
|
|
692
673
|
def test_serializer_has_access_to_root_object
|
@@ -696,7 +677,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
696
677
|
attributes :id, :name
|
697
678
|
|
698
679
|
define_method :serializable_hash do
|
699
|
-
hash_object = @hash
|
680
|
+
hash_object = @options[:hash]
|
700
681
|
super()
|
701
682
|
end
|
702
683
|
end
|
@@ -718,10 +699,59 @@ class SerializerTest < ActiveModel::TestCase
|
|
718
699
|
author = author_class.new(:id => 5, :name => "Tom Dale")
|
719
700
|
post.author = author
|
720
701
|
|
721
|
-
expected = serializer_class.new(post
|
702
|
+
expected = serializer_class.new(post).as_json
|
722
703
|
assert_equal expected, hash_object
|
723
704
|
end
|
705
|
+
|
706
|
+
def test_embed_ids_include_true_with_root
|
707
|
+
serializer_class = post_serializer
|
724
708
|
|
709
|
+
serializer_class.class_eval do
|
710
|
+
root :post
|
711
|
+
embed :ids, :include => true
|
712
|
+
has_many :comments, :key => :comment_ids, :root => :comments
|
713
|
+
has_one :author, :serializer => DefaultUserSerializer, :key => :author_id, :root => :author
|
714
|
+
end
|
715
|
+
|
716
|
+
post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
|
717
|
+
comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
|
718
|
+
post.comments = comments
|
719
|
+
|
720
|
+
serializer = serializer_class.new(post)
|
721
|
+
|
722
|
+
assert_equal({
|
723
|
+
:post => {
|
724
|
+
:title => "New Post",
|
725
|
+
:body => "Body of new post",
|
726
|
+
:comment_ids => [1, 2],
|
727
|
+
:author_id => nil
|
728
|
+
},
|
729
|
+
:comments => [
|
730
|
+
{ :title => "Comment1" },
|
731
|
+
{ :title => "Comment2" }
|
732
|
+
],
|
733
|
+
:author => []
|
734
|
+
}, serializer.as_json)
|
735
|
+
|
736
|
+
post.author = User.new(:id => 1)
|
737
|
+
|
738
|
+
serializer = serializer_class.new(post)
|
739
|
+
|
740
|
+
assert_equal({
|
741
|
+
:post => {
|
742
|
+
:title => "New Post",
|
743
|
+
:body => "Body of new post",
|
744
|
+
:comment_ids => [1, 2],
|
745
|
+
:author_id => 1
|
746
|
+
},
|
747
|
+
:comments => [
|
748
|
+
{ :title => "Comment1" },
|
749
|
+
{ :title => "Comment2" }
|
750
|
+
],
|
751
|
+
:author => [{ :first_name => "Jose", :last_name => "Valim" }]
|
752
|
+
}, serializer.as_json)
|
753
|
+
end
|
754
|
+
|
725
755
|
# the point of this test is to illustrate that deeply nested serializers
|
726
756
|
# still side-load at the root.
|
727
757
|
def test_embed_with_include_inserts_at_root
|
@@ -766,7 +796,7 @@ class SerializerTest < ActiveModel::TestCase
|
|
766
796
|
comment1.tags = [tag1, tag3]
|
767
797
|
comment2.tags = [tag1, tag2]
|
768
798
|
|
769
|
-
actual = ActiveModel::ArraySerializer.new([post],
|
799
|
+
actual = ActiveModel::ArraySerializer.new([post], :root => :posts).as_json
|
770
800
|
assert_equal({
|
771
801
|
:posts => [
|
772
802
|
{ :title => "New Post", :body => "NEW POST", :id => 1, :comments => [1,2] }
|