gpi-active_model_serializers 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,51 @@
1
+ require "test_helper"
2
+
3
+ class RandomModel
4
+ include ActiveModel::SerializerSupport
5
+ end
6
+
7
+ class OtherRandomModel
8
+ include ActiveModel::SerializerSupport
9
+ end
10
+
11
+ class OtherRandomModelSerializer
12
+ end
13
+
14
+ class RandomModelCollection
15
+ include ActiveModel::ArraySerializerSupport
16
+ end
17
+
18
+ module ActiveRecord
19
+ class Relation
20
+ end
21
+ end
22
+
23
+ module Mongoid
24
+ class Criteria
25
+ end
26
+ end
27
+
28
+ class SerializerSupportTest < ActiveModel::TestCase
29
+ test "it returns nil if no serializer exists" do
30
+ assert_equal nil, RandomModel.new.active_model_serializer
31
+ end
32
+
33
+ test "it returns a deducted serializer if it exists exists" do
34
+ assert_equal OtherRandomModelSerializer, OtherRandomModel.new.active_model_serializer
35
+ end
36
+
37
+ test "it returns ArraySerializer for a collection" do
38
+ assert_equal ActiveModel::ArraySerializer, RandomModelCollection.new.active_model_serializer
39
+ end
40
+
41
+ test "it automatically includes array_serializer in active_record/relation" do
42
+ ActiveSupport.run_load_hooks(:active_record)
43
+ assert_equal ActiveModel::ArraySerializer, ActiveRecord::Relation.new.active_model_serializer
44
+ end
45
+
46
+ test "it automatically includes array_serializer in mongoid/criteria" do
47
+ ActiveSupport.run_load_hooks(:mongoid)
48
+ assert_equal ActiveModel::ArraySerializer, Mongoid::Criteria.new.active_model_serializer
49
+ end
50
+ end
51
+
@@ -0,0 +1,1521 @@
1
+ require "test_helper"
2
+ require "test_fakes"
3
+
4
+ class SerializerTest < ActiveModel::TestCase
5
+ def test_scope_works_correct
6
+ serializer = ActiveModel::Serializer.new :foo, scope: :bar
7
+ assert_equal serializer.scope, :bar
8
+ end
9
+
10
+ def test_attributes
11
+ user = User.new
12
+ user_serializer = DefaultUserSerializer.new(user, {})
13
+
14
+ hash = user_serializer.as_json
15
+
16
+ assert_equal({
17
+ default_user: { first_name: "Jose", last_name: "Valim" }
18
+ }, hash)
19
+ end
20
+
21
+ def test_attributes_method
22
+ user = User.new
23
+ user_serializer = UserSerializer.new(user, scope: {})
24
+
25
+ hash = user_serializer.as_json
26
+
27
+ assert_equal({
28
+ user: { first_name: "Jose", last_name: "Valim", ok: true }
29
+ }, hash)
30
+ end
31
+
32
+ def test_attributes_method_specifying_keys
33
+ user = User.new
34
+ user_serializer = UserAttributesWithKeySerializer.new(user, scope: {})
35
+
36
+ hash = user_serializer.as_json
37
+
38
+ assert_equal({
39
+ user_attributes_with_key: { f_name: "Jose", l_name: "Valim", ok: true }
40
+ }, hash)
41
+ end
42
+
43
+ def test_attributes_method_specifying_some_keys
44
+ user = User.new
45
+ user_serializer = UserAttributesWithSomeKeySerializer.new(user, scope: {})
46
+
47
+ hash = user_serializer.as_json
48
+
49
+ assert_equal({
50
+ user_attributes_with_some_key: { first_name: "Jose", l_name: "Valim", ok: true }
51
+ }, hash)
52
+ end
53
+
54
+ def test_attributes_method_with_unsymbolizable_key
55
+ user = User.new
56
+ user_serializer = UserAttributesWithUnsymbolizableKeySerializer.new(user, scope: {})
57
+
58
+ hash = user_serializer.as_json
59
+
60
+ assert_equal({
61
+ user_attributes_with_unsymbolizable_key: { first_name: "Jose", :"last-name" => "Valim", ok: true }
62
+ }, hash)
63
+ end
64
+
65
+ def test_attribute_method_with_name_as_serializer_prefix
66
+ object = SomeObject.new("something")
67
+ object_serializer = SomeSerializer.new(object, {})
68
+
69
+ hash = object_serializer.as_json
70
+
71
+ assert_equal({
72
+ some: { some: "something" }
73
+ }, hash)
74
+ end
75
+
76
+ def test_serializer_receives_scope
77
+ user = User.new
78
+ user_serializer = UserSerializer.new(user, scope: { scope: true })
79
+
80
+ hash = user_serializer.as_json
81
+
82
+ assert_equal({
83
+ user: {
84
+ first_name: "Jose",
85
+ last_name: "Valim",
86
+ ok: true,
87
+ scope: true
88
+ }
89
+ }, hash)
90
+ end
91
+
92
+ def test_serializer_receives_url_options
93
+ user = User.new
94
+ user_serializer = UserSerializer.new(user, url_options: { host: "test.local" })
95
+ assert_equal({ host: "test.local" }, user_serializer.url_options)
96
+ end
97
+
98
+ def test_serializer_returns_empty_hash_without_url_options
99
+ user = User.new
100
+ user_serializer = UserSerializer.new(user)
101
+ assert_equal({}, user_serializer.url_options)
102
+ end
103
+
104
+ def test_pretty_accessors
105
+ user = User.new
106
+ user.superuser = true
107
+ user_serializer = MyUserSerializer.new(user)
108
+
109
+ hash = user_serializer.as_json
110
+
111
+ assert_equal({
112
+ my_user: {
113
+ first_name: "Jose", last_name: "Valim", super_user: true
114
+ }
115
+ }, hash)
116
+ end
117
+
118
+ def test_has_many
119
+ user = User.new
120
+
121
+ post = Post.new(title: "New Post", body: "Body of new post", email: "tenderlove@tenderlove.com")
122
+ comments = [Comment.new(title: "Comment1"), Comment.new(title: "Comment2")]
123
+ post.comments = comments
124
+
125
+ post_serializer = PostSerializer.new(post, scope: user)
126
+
127
+ assert_equal({
128
+ post: {
129
+ title: "New Post",
130
+ body: "Body of new post",
131
+ comments: [
132
+ { title: "Comment1" },
133
+ { title: "Comment2" }
134
+ ]
135
+ }
136
+ }, post_serializer.as_json)
137
+ end
138
+
139
+ def test_conditionally_included_associations
140
+ user = User.new
141
+
142
+ post = Post.new(title: "New Post", body: "Body of new post", email: "tenderlove@tenderlove.com")
143
+ comments = [Comment.new(title: "Comment1"), Comment.new(title: "Comment2")]
144
+ post.comments = comments
145
+
146
+ post_serializer = PostWithConditionalCommentsSerializer.new(post, scope: user)
147
+
148
+ # comments enabled
149
+ post.comments_disabled = false
150
+ assert_equal({
151
+ post: {
152
+ title: "New Post",
153
+ body: "Body of new post",
154
+ comments: [
155
+ { title: "Comment1" },
156
+ { title: "Comment2" }
157
+ ]
158
+ }
159
+ }, post_serializer.as_json)
160
+
161
+ # comments disabled
162
+ post.comments_disabled = true
163
+ assert_equal({
164
+ post: {
165
+ title: "New Post",
166
+ body: "Body of new post"
167
+ }
168
+ }, post_serializer.as_json)
169
+ end
170
+
171
+ def test_conditionally_included_associations_and_attributes
172
+ user = User.new
173
+
174
+ post = Post.new(title: "New Post", body: "Body of new post", author: 'Sausage King', email: "tenderlove@tenderlove.com")
175
+ comments = [Comment.new(title: "Comment1"), Comment.new(title: "Comment2")]
176
+ post.comments = comments
177
+
178
+ post_serializer = PostWithMultipleConditionalsSerializer.new(post, scope: user)
179
+
180
+ # comments enabled
181
+ post.comments_disabled = false
182
+ assert_equal({
183
+ post: {
184
+ title: "New Post",
185
+ body: "Body of new post",
186
+ comments: [
187
+ { title: "Comment1" },
188
+ { title: "Comment2" }
189
+ ]
190
+ }
191
+ }, post_serializer.as_json)
192
+
193
+ # comments disabled
194
+ post.comments_disabled = true
195
+ assert_equal({
196
+ post: {
197
+ title: "New Post",
198
+ body: "Body of new post"
199
+ }
200
+ }, post_serializer.as_json)
201
+
202
+ # superuser - should see author
203
+ user.superuser = true
204
+ assert_equal({
205
+ post: {
206
+ title: "New Post",
207
+ body: "Body of new post",
208
+ author: "Sausage King"
209
+ }
210
+ }, post_serializer.as_json)
211
+ end
212
+
213
+ def test_has_one
214
+ user = User.new
215
+ blog = Blog.new
216
+ blog.author = user
217
+
218
+ json = BlogSerializer.new(blog, scope: user).as_json
219
+ assert_equal({
220
+ blog: {
221
+ author: {
222
+ first_name: "Jose",
223
+ last_name: "Valim"
224
+ }
225
+ }
226
+ }, json)
227
+ end
228
+
229
+ def test_overridden_associations
230
+ author_serializer = Class.new(ActiveModel::Serializer) do
231
+ attributes :first_name
232
+ end
233
+
234
+ blog_serializer = Class.new(ActiveModel::Serializer) do
235
+ def person
236
+ object.author
237
+ end
238
+
239
+ has_one :person, serializer: author_serializer
240
+ end
241
+
242
+ user = User.new
243
+ blog = Blog.new
244
+ blog.author = user
245
+
246
+ json = blog_serializer.new(blog, scope: user).as_json
247
+ assert_equal({
248
+ person: {
249
+ first_name: "Jose"
250
+ }
251
+ }, json)
252
+ end
253
+
254
+ def post_serializer
255
+ Class.new(ActiveModel::Serializer) do
256
+ attributes :title, :body
257
+ has_many :comments, serializer: CommentSerializer
258
+ has_one :author, serializer: DefaultUserSerializer
259
+ end
260
+ end
261
+
262
+ def test_associations_with_nil_association
263
+ user = User.new
264
+ blog = Blog.new
265
+
266
+ json = BlogSerializer.new(blog, scope: user).as_json
267
+ assert_equal({
268
+ blog: { author: nil }
269
+ }, json)
270
+
271
+ serializer = Class.new(BlogSerializer) do
272
+ root :blog
273
+ end
274
+
275
+ json = serializer.new(blog, scope: user).as_json
276
+ assert_equal({ blog: { author: nil } }, json)
277
+ end
278
+
279
+ def test_custom_root
280
+ user = User.new
281
+ blog = Blog.new
282
+
283
+ serializer = Class.new(BlogSerializer) do
284
+ root :my_blog
285
+ end
286
+
287
+ assert_equal({ my_blog: { author: nil } }, serializer.new(blog, scope: user).as_json)
288
+ end
289
+
290
+ def test_nil_root_object
291
+ user = User.new
292
+ blog = nil
293
+
294
+ serializer = Class.new(BlogSerializer) do
295
+ root false
296
+ end
297
+
298
+ assert_equal(nil, serializer.new(blog, scope: user).as_json)
299
+ end
300
+
301
+ def test_custom_root_with_nil_root_object
302
+ user = User.new
303
+ blog = nil
304
+
305
+ serializer = Class.new(BlogSerializer) do
306
+ root :my_blog
307
+ end
308
+
309
+ assert_equal({ my_blog: nil }, serializer.new(blog, scope: user).as_json)
310
+ end
311
+
312
+ def test_false_root
313
+ user = User.new
314
+ blog = Blog.new
315
+
316
+ serializer = Class.new(BlogSerializer) do
317
+ root false
318
+ end
319
+
320
+ another_serializer = Class.new(BlogSerializer) do
321
+ self.root = false
322
+ end
323
+
324
+ assert_equal({ author: nil }, serializer.new(blog, scope: user).as_json)
325
+ assert_equal({ author: nil }, another_serializer.new(blog, scope: user).as_json)
326
+
327
+ # test inherited false root
328
+ serializer = Class.new(serializer)
329
+ assert_equal({ author: nil }, serializer.new(blog, scope: user).as_json)
330
+ end
331
+
332
+ def test_true_root
333
+ blog = Blog.new
334
+
335
+ assert_equal({
336
+ blog_with_root: {
337
+ author: nil,
338
+ }
339
+ }, BlogWithRootSerializer.new(blog).as_json)
340
+ end
341
+
342
+ def test_root_false_on_load_active_model_serializers
343
+ begin
344
+ ActiveSupport.on_load(:active_model_serializers) do
345
+ self.root = false
346
+ end
347
+
348
+ blog = Blog.new
349
+ serializer = BlogSerializer.new(blog)
350
+
351
+ assert_equal({ author: nil }, serializer.as_json)
352
+ ensure
353
+ ActiveSupport.on_load(:active_model_serializers) do
354
+ self.root = nil
355
+ end
356
+ end
357
+ end
358
+
359
+ def test_embed_ids
360
+ serializer = post_serializer
361
+
362
+ serializer.class_eval do
363
+ root :post
364
+ embed :ids
365
+ end
366
+
367
+ post = Post.new(title: "New Post", body: "Body of new post", email: "tenderlove@tenderlove.com")
368
+ comments = [Comment.new(title: "Comment1", id: 1), Comment.new(title: "Comment2", id: 2)]
369
+ post.comments = comments
370
+
371
+ serializer = serializer.new(post)
372
+
373
+ assert_equal({
374
+ post: {
375
+ title: "New Post",
376
+ body: "Body of new post",
377
+ comment_ids: [1, 2],
378
+ author_id: nil
379
+ }
380
+ }, serializer.as_json)
381
+ end
382
+
383
+ def test_embed_ids_include_true
384
+ serializer_class = post_serializer
385
+
386
+ serializer_class.class_eval do
387
+ root :post
388
+ embed :ids, include: true
389
+ end
390
+
391
+ post = Post.new(title: "New Post", body: "Body of new post", email: "tenderlove@tenderlove.com")
392
+ comments = [Comment.new(title: "Comment1", id: 1), Comment.new(title: "Comment2", id: 2)]
393
+ post.comments = comments
394
+
395
+ serializer = serializer_class.new(post)
396
+
397
+ assert_equal({
398
+ post: {
399
+ title: "New Post",
400
+ body: "Body of new post",
401
+ comment_ids: [1, 2],
402
+ author_id: nil
403
+ },
404
+ comments: [
405
+ { title: "Comment1" },
406
+ { title: "Comment2" }
407
+ ],
408
+ authors: []
409
+ }, serializer.as_json)
410
+
411
+ post.author = User.new(id: 1)
412
+
413
+ serializer = serializer_class.new(post)
414
+
415
+ assert_equal({
416
+ post: {
417
+ title: "New Post",
418
+ body: "Body of new post",
419
+ comment_ids: [1, 2],
420
+ author_id: 1
421
+ },
422
+ comments: [
423
+ { title: "Comment1" },
424
+ { title: "Comment2" }
425
+ ],
426
+ authors: [{ first_name: "Jose", last_name: "Valim" }]
427
+ }, serializer.as_json)
428
+ end
429
+
430
+ def test_methods_take_priority_over_associations
431
+ post_serializer = Class.new(ActiveModel::Serializer) do
432
+ attributes :title
433
+ has_many :comments
434
+ embed :ids
435
+
436
+ def comments
437
+ object.comments[0,1]
438
+ end
439
+ end
440
+
441
+ post = Post.new(title: "My Post")
442
+ comments = [Comment.new(title: "Comment1", id: 1), Comment.new(title: "Comment2", id: 2)]
443
+ post.comments = comments
444
+
445
+ post.class_eval do
446
+ define_method :comment_ids, lambda {
447
+ self.comments.map { |c| c.read_attribute_for_serialization(:id) }
448
+ }
449
+ end
450
+ json = post_serializer.new(post).as_json
451
+ assert_equal({
452
+ title: "My Post",
453
+ comment_ids: [1]
454
+ }, json)
455
+ end
456
+
457
+ def test_methods_take_priority_over_associations_and_call_the_appropriate_id_method
458
+ comment_serializer = Class.new(ActiveModel::Serializer) do
459
+ def id
460
+ "OMG"
461
+ end
462
+ end
463
+
464
+ post_serializer = Class.new(ActiveModel::Serializer) do
465
+ attributes :title
466
+ has_many :comments, serializer: comment_serializer
467
+ embed :ids
468
+
469
+ def comments
470
+ object.comments[0,1]
471
+ end
472
+ end
473
+
474
+ post = Post.new(title: "My Post")
475
+ comments = [Comment.new(title: "Comment1", id: 1), Comment.new(title: "Comment2", id: 2)]
476
+ post.comments = comments
477
+
478
+ post.class_eval do
479
+ define_method :comment_ids, lambda {
480
+ self.comments.map { |c| c.read_attribute_for_serialization(:id) }
481
+ }
482
+ end
483
+ json = post_serializer.new(post).as_json
484
+ assert_equal({
485
+ title: "My Post",
486
+ comment_ids: ["OMG"]
487
+ }, json)
488
+ end
489
+
490
+ def test_embed_objects
491
+ serializer = post_serializer
492
+
493
+ serializer.class_eval do
494
+ root :post
495
+ embed :objects
496
+ end
497
+
498
+ post = Post.new(title: "New Post", body: "Body of new post", email: "tenderlove@tenderlove.com")
499
+ comments = [Comment.new(title: "Comment1", id: 1), Comment.new(title: "Comment2", id: 2)]
500
+ post.comments = comments
501
+
502
+ serializer = serializer.new(post)
503
+
504
+ assert_equal({
505
+ post: {
506
+ title: "New Post",
507
+ body: "Body of new post",
508
+ author: nil,
509
+ comments: [
510
+ { title: "Comment1" },
511
+ { title: "Comment2" }
512
+ ]
513
+ }
514
+ }, serializer.as_json)
515
+ end
516
+
517
+ def test_sets_can_be_serialized
518
+ post1 = Post.new(title: "Post1", author: "Author1", id: 1)
519
+ post2 = Post.new(title: "Post2", author: "Author2", id: 2)
520
+
521
+ set = Set.new
522
+ set << post1
523
+ set << post2
524
+
525
+ serializer = set.active_model_serializer.new set, each_serializer: CustomPostSerializer
526
+
527
+ as_json = serializer.as_json
528
+ assert_equal 2, as_json.size
529
+ assert as_json.include?({ title: "Post1" })
530
+ assert as_json.include?({ title: "Post2" })
531
+ end
532
+
533
+ def test_associations_with_as
534
+ posts = [
535
+ Post.new(title: 'First Post', body: 'text'),
536
+ Post.new(title: 'Second Post', body: 'text')
537
+ ]
538
+ user = User.new
539
+
540
+ custom_blog = CustomBlog.new
541
+ custom_blog.public_posts = posts
542
+ custom_blog.public_user = user
543
+
544
+ serializer = CustomBlogSerializer.new(custom_blog, scope: { scope: true })
545
+
546
+ assert_equal({
547
+ custom_blog: {
548
+ posts: [
549
+ {title: 'First Post', body: 'text', comments: []},
550
+ {title: 'Second Post', body: 'text', comments: []}
551
+ ],
552
+ user: {
553
+ first_name: "Jose",
554
+ last_name: "Valim", ok: true,
555
+ scope: true
556
+ }
557
+ }
558
+ }, serializer.as_json)
559
+ end
560
+
561
+ def test_implicity_detection_for_association_serializers
562
+ implicit_serializer = Class.new(ActiveModel::Serializer) do
563
+ root :custom_blog
564
+ const_set(:UserSerializer, UserSerializer)
565
+ const_set(:PostSerializer, PostSerializer)
566
+
567
+ has_many :public_posts, key: :posts
568
+ has_one :public_user, key: :user
569
+ end
570
+
571
+ posts = [
572
+ Post.new(title: 'First Post', body: 'text', comments: []),
573
+ Post.new(title: 'Second Post', body: 'text', comments: [])
574
+ ]
575
+ user = User.new
576
+
577
+ custom_blog = CustomBlog.new
578
+ custom_blog.public_posts = posts
579
+ custom_blog.public_user = user
580
+
581
+ serializer = implicit_serializer.new(custom_blog, scope: { scope: true })
582
+
583
+ assert_equal({
584
+ custom_blog: {
585
+ posts: [
586
+ {title: 'First Post', body: 'text', comments: []},
587
+ {title: 'Second Post', body: 'text', comments: []}
588
+ ],
589
+ user: {
590
+ first_name: "Jose",
591
+ last_name: "Valim", ok: true,
592
+ scope: true
593
+ }
594
+ }
595
+ }, serializer.as_json)
596
+ end
597
+
598
+ def test_attribute_key
599
+ serializer_class = Class.new(ActiveModel::Serializer) do
600
+ root :user
601
+
602
+ attribute :first_name, key: :firstName
603
+ attribute :last_name, key: :lastName
604
+ attribute :password
605
+ end
606
+
607
+ serializer = serializer_class.new(User.new)
608
+
609
+ assert_equal({
610
+ user: {
611
+ firstName: "Jose",
612
+ lastName: "Valim",
613
+ password: "oh noes yugive my password"
614
+ }
615
+ }, serializer.as_json)
616
+ end
617
+
618
+ def setup_model
619
+ Class.new do
620
+ class << self
621
+ def columns_hash
622
+ { "name" => Struct.new(:type).new(:string), "age" => Struct.new(:type).new(:integer) }
623
+ end
624
+
625
+ def reflect_on_association(name)
626
+ case name
627
+ when :posts
628
+ Struct.new(:macro, :name).new(:has_many, :posts)
629
+ when :parent
630
+ Struct.new(:macro, :name).new(:belongs_to, :parent)
631
+ end
632
+ end
633
+ end
634
+ end
635
+ end
636
+
637
+ def test_schema
638
+ model = setup_model
639
+
640
+ serializer = Class.new(ActiveModel::Serializer) do
641
+ class << self; self; end.class_eval do
642
+ define_method(:model_class) do model end
643
+ end
644
+
645
+ # Computed attributes (not real columns or associations).
646
+ def can_edit; end
647
+ def can_view; end
648
+ def drafts; end
649
+
650
+ attributes :name, :age, { can_edit: :boolean }, :can_view
651
+ has_many :posts, serializer: Class.new
652
+ has_many :drafts, serializer: Class.new
653
+ has_one :parent, serializer: Class.new
654
+ end
655
+
656
+ assert_equal serializer.schema, {
657
+ attributes: { name: :string, age: :integer, can_edit: :boolean, can_view: nil },
658
+ associations: {
659
+ posts: { has_many: :posts },
660
+ drafts: nil,
661
+ parent: { belongs_to: :parent }
662
+ }
663
+ }
664
+ end
665
+
666
+ def test_schema_with_as
667
+ model = setup_model
668
+
669
+ serializer = Class.new(ActiveModel::Serializer) do
670
+ class << self; self; end.class_eval do
671
+ define_method(:model_class) do model end
672
+ end
673
+
674
+ attributes :name, :age
675
+ has_many :posts, key: :my_posts, serializer: Class.new
676
+ has_one :parent, key: :my_parent, serializer: Class.new
677
+ end
678
+
679
+ assert_equal serializer.schema, {
680
+ attributes: { name: :string, age: :integer },
681
+ associations: {
682
+ my_posts: { has_many: :posts },
683
+ my_parent: { belongs_to: :parent }
684
+ }
685
+ }
686
+ end
687
+
688
+ def test_embed_id_for_has_one
689
+ author_serializer = Class.new(ActiveModel::Serializer)
690
+
691
+ serializer_class = Class.new(ActiveModel::Serializer) do
692
+ embed :ids
693
+ root :post
694
+
695
+ attributes :title, :body
696
+ has_one :author, serializer: author_serializer
697
+ end
698
+
699
+ post_class = Class.new(Model) do
700
+ attr_accessor :author
701
+ end
702
+
703
+ author_class = Class.new(Model)
704
+
705
+ post = post_class.new(title: "New Post", body: "It's a new post!")
706
+ author = author_class.new(id: 5)
707
+ post.author = author
708
+
709
+ hash = serializer_class.new(post)
710
+
711
+ assert_equal({
712
+ post: {
713
+ title: "New Post",
714
+ body: "It's a new post!",
715
+ author_id: 5
716
+ }
717
+ }, hash.as_json)
718
+ end
719
+
720
+ def test_embed_id_for_has_one_overriding_associated_id
721
+ author_serializer = Class.new(ActiveModel::Serializer) do
722
+ def id
723
+ "OMG"
724
+ end
725
+ end
726
+
727
+ serializer_class = Class.new(ActiveModel::Serializer) do
728
+ embed :ids
729
+ root :post
730
+
731
+ attributes :title, :body
732
+ has_one :author, serializer: author_serializer
733
+ end
734
+
735
+ post_class = Class.new(Model) do
736
+ attr_accessor :author
737
+ end
738
+
739
+ author_class = Class.new(Model)
740
+
741
+ post = post_class.new(title: "New Post", body: "It's a new post!")
742
+ author = author_class.new(id: 5)
743
+ post.author = author
744
+
745
+ hash = serializer_class.new(post)
746
+
747
+ assert_equal({
748
+ post: {
749
+ title: "New Post",
750
+ body: "It's a new post!",
751
+ author_id: "OMG"
752
+ }
753
+ }, hash.as_json)
754
+ end
755
+
756
+ def test_embed_objects_for_has_one
757
+ author_serializer = Class.new(ActiveModel::Serializer) do
758
+ attributes :id, :name
759
+ end
760
+
761
+ serializer_class = Class.new(ActiveModel::Serializer) do
762
+ root :post
763
+
764
+ attributes :title, :body
765
+ has_one :author, serializer: author_serializer
766
+ end
767
+
768
+ post_class = Class.new(Model) do
769
+ attr_accessor :author
770
+ end
771
+
772
+ author_class = Class.new(Model)
773
+
774
+ post = post_class.new(title: "New Post", body: "It's a new post!")
775
+ author = author_class.new(id: 5, name: "Tom Dale")
776
+ post.author = author
777
+
778
+ hash = serializer_class.new(post)
779
+
780
+ assert_equal({
781
+ post: {
782
+ title: "New Post",
783
+ body: "It's a new post!",
784
+ author: { id: 5, name: "Tom Dale" }
785
+ }
786
+ }, hash.as_json)
787
+ end
788
+
789
+ def test_root_provided_in_options
790
+ author_serializer = Class.new(ActiveModel::Serializer) do
791
+ attributes :id, :name
792
+ end
793
+
794
+ serializer_class = Class.new(ActiveModel::Serializer) do
795
+ root :post
796
+
797
+ attributes :title, :body
798
+ has_one :author, serializer: author_serializer
799
+ end
800
+
801
+ post_class = Class.new(Model) do
802
+ attr_accessor :author
803
+ end
804
+
805
+ author_class = Class.new(Model)
806
+
807
+ post = post_class.new(title: "New Post", body: "It's a new post!")
808
+ author = author_class.new(id: 5, name: "Tom Dale")
809
+ post.author = author
810
+
811
+ assert_equal({
812
+ blog_post: {
813
+ title: "New Post",
814
+ body: "It's a new post!",
815
+ author: { id: 5, name: "Tom Dale" }
816
+ }
817
+ }, serializer_class.new(post, root: :blog_post).as_json)
818
+
819
+ assert_equal({
820
+ title: "New Post",
821
+ body: "It's a new post!",
822
+ author: { id: 5, name: "Tom Dale" }
823
+ }, serializer_class.new(post, root: false).as_json)
824
+
825
+ assert_equal({
826
+ blog_post: {
827
+ title: "New Post",
828
+ body: "It's a new post!",
829
+ author: { id: 5, name: "Tom Dale" }
830
+ }
831
+ }, serializer_class.new(post).as_json(root: :blog_post))
832
+
833
+ assert_equal({
834
+ title: "New Post",
835
+ body: "It's a new post!",
836
+ author: { id: 5, name: "Tom Dale" }
837
+ }, serializer_class.new(post).as_json(root: false))
838
+ end
839
+
840
+ def test_serializer_has_access_to_root_object
841
+ hash_object = nil
842
+
843
+ author_serializer = Class.new(ActiveModel::Serializer) do
844
+ attributes :id, :name
845
+
846
+ define_method :serializable_hash do
847
+ hash_object = @options[:hash]
848
+ super()
849
+ end
850
+ end
851
+
852
+ serializer_class = Class.new(ActiveModel::Serializer) do
853
+ root :post
854
+
855
+ attributes :title, :body
856
+ has_one :author, serializer: author_serializer
857
+ end
858
+
859
+ post_class = Class.new(Model) do
860
+ attr_accessor :author
861
+ end
862
+
863
+ author_class = Class.new(Model)
864
+
865
+ post = post_class.new(title: "New Post", body: "It's a new post!")
866
+ author = author_class.new(id: 5, name: "Tom Dale")
867
+ post.author = author
868
+
869
+ expected = serializer_class.new(post).as_json
870
+ assert_equal expected, hash_object
871
+ end
872
+
873
+ def test_embed_ids_include_true_with_root
874
+ serializer_class = post_serializer
875
+
876
+ serializer_class.class_eval do
877
+ root :post
878
+ embed :ids, include: true
879
+ has_many :comments, key: :comment_ids, root: :comments
880
+ has_one :author, serializer: DefaultUserSerializer, key: :author_id, root: :author
881
+ end
882
+
883
+ post = Post.new(title: "New Post", body: "Body of new post", email: "tenderlove@tenderlove.com")
884
+ comments = [Comment.new(title: "Comment1", id: 1), Comment.new(title: "Comment2", id: 2)]
885
+ post.comments = comments
886
+
887
+ serializer = serializer_class.new(post)
888
+
889
+ assert_equal({
890
+ post: {
891
+ title: "New Post",
892
+ body: "Body of new post",
893
+ comment_ids: [1, 2],
894
+ author_id: nil
895
+ },
896
+ comments: [
897
+ { title: "Comment1" },
898
+ { title: "Comment2" }
899
+ ],
900
+ author: []
901
+ }, serializer.as_json)
902
+
903
+ post.author = User.new(id: 1)
904
+
905
+ serializer = serializer_class.new(post)
906
+
907
+ assert_equal({
908
+ post: {
909
+ title: "New Post",
910
+ body: "Body of new post",
911
+ comment_ids: [1, 2],
912
+ author_id: 1
913
+ },
914
+ comments: [
915
+ { title: "Comment1" },
916
+ { title: "Comment2" }
917
+ ],
918
+ author: [{ first_name: "Jose", last_name: "Valim" }]
919
+ }, serializer.as_json)
920
+ end
921
+
922
+ # the point of this test is to illustrate that deeply nested serializers
923
+ # still side-load at the root.
924
+ def test_embed_with_include_inserts_at_root
925
+ tag_serializer = Class.new(ActiveModel::Serializer) do
926
+ attributes :id, :name
927
+ end
928
+
929
+ comment_serializer = Class.new(ActiveModel::Serializer) do
930
+ embed :ids, include: true
931
+ attributes :id, :body
932
+ has_many :tags, serializer: tag_serializer
933
+ end
934
+
935
+ post_serializer = Class.new(ActiveModel::Serializer) do
936
+ embed :ids, include: true
937
+ attributes :id, :title, :body
938
+ has_many :comments, serializer: comment_serializer
939
+ end
940
+
941
+ post_class = Class.new(Model) do
942
+ attr_accessor :comments
943
+
944
+ define_method :active_model_serializer do
945
+ post_serializer
946
+ end
947
+ end
948
+
949
+ comment_class = Class.new(Model) do
950
+ attr_accessor :tags
951
+ end
952
+
953
+ tag_class = Class.new(Model)
954
+
955
+ post = post_class.new(title: "New Post", body: "NEW POST", id: 1)
956
+ comment1 = comment_class.new(body: "EWOT", id: 1)
957
+ comment2 = comment_class.new(body: "YARLY", id: 2)
958
+ tag1 = tag_class.new(name: "lolcat", id: 1)
959
+ tag2 = tag_class.new(name: "nyancat", id: 2)
960
+ tag3 = tag_class.new(name: "violetcat", id: 3)
961
+
962
+ post.comments = [comment1, comment2]
963
+ comment1.tags = [tag1, tag3]
964
+ comment2.tags = [tag1, tag2]
965
+
966
+ actual = ActiveModel::ArraySerializer.new([post], root: :posts).as_json
967
+ assert_equal({
968
+ posts: [
969
+ { title: "New Post", body: "NEW POST", id: 1, comment_ids: [1,2] }
970
+ ],
971
+
972
+ comments: [
973
+ { body: "EWOT", id: 1, tag_ids: [1,3] },
974
+ { body: "YARLY", id: 2, tag_ids: [1,2] }
975
+ ],
976
+
977
+ tags: [
978
+ { name: "lolcat", id: 1 },
979
+ { name: "violetcat", id: 3 },
980
+ { name: "nyancat", id: 2 }
981
+ ]
982
+ }, actual)
983
+ end
984
+
985
+ def test_can_customize_attributes
986
+ serializer = Class.new(ActiveModel::Serializer) do
987
+ attributes :title, :body
988
+
989
+ def title
990
+ object.title.upcase
991
+ end
992
+ end
993
+
994
+ klass = Class.new do
995
+ def read_attribute_for_serialization(name)
996
+ { title: "New post!", body: "First post body" }[name]
997
+ end
998
+
999
+ def title
1000
+ read_attribute_for_serialization(:title)
1001
+ end
1002
+
1003
+ def body
1004
+ read_attribute_for_serialization(:body)
1005
+ end
1006
+ end
1007
+
1008
+ object = klass.new
1009
+
1010
+ actual = serializer.new(object, root: :post).as_json
1011
+
1012
+ assert_equal({
1013
+ post: {
1014
+ title: "NEW POST!",
1015
+ body: "First post body"
1016
+ }
1017
+ }, actual)
1018
+ end
1019
+
1020
+ def test_can_customize_attributes_with_read_attributes
1021
+ serializer = Class.new(ActiveModel::Serializer) do
1022
+ attributes :title, :body
1023
+
1024
+ def read_attribute_for_serialization(name)
1025
+ { title: "New post!", body: "First post body" }[name]
1026
+ end
1027
+ end
1028
+
1029
+ actual = serializer.new(Object.new, root: :post).as_json
1030
+
1031
+ assert_equal({
1032
+ post: {
1033
+ title: "New post!",
1034
+ body: "First post body"
1035
+ }
1036
+ }, actual)
1037
+ end
1038
+
1039
+ def test_active_support_on_load_hooks_fired
1040
+ loaded = nil
1041
+ ActiveSupport.on_load(:active_model_serializers) do
1042
+ loaded = self
1043
+ end
1044
+ assert_equal ActiveModel::Serializer, loaded
1045
+ end
1046
+
1047
+ def tests_query_attributes_strip_question_mark
1048
+ todo = Class.new do
1049
+ def overdue?
1050
+ true
1051
+ end
1052
+
1053
+ def read_attribute_for_serialization(name)
1054
+ send name
1055
+ end
1056
+ end
1057
+
1058
+ serializer = Class.new(ActiveModel::Serializer) do
1059
+ attribute :overdue?
1060
+ end
1061
+
1062
+ actual = serializer.new(todo.new).as_json
1063
+
1064
+ assert_equal({
1065
+ overdue: true
1066
+ }, actual)
1067
+ end
1068
+
1069
+ def tests_query_attributes_allow_key_option
1070
+ todo = Class.new do
1071
+ def overdue?
1072
+ true
1073
+ end
1074
+
1075
+ def read_attribute_for_serialization(name)
1076
+ send name
1077
+ end
1078
+ end
1079
+
1080
+ serializer = Class.new(ActiveModel::Serializer) do
1081
+ attribute :overdue?, key: :foo
1082
+ end
1083
+
1084
+ actual = serializer.new(todo.new).as_json
1085
+
1086
+ assert_equal({
1087
+ foo: true
1088
+ }, actual)
1089
+ end
1090
+
1091
+ def tests_can_handle_polymorphism
1092
+ email_serializer = Class.new(ActiveModel::Serializer) do
1093
+ attributes :subject, :body
1094
+ end
1095
+
1096
+ email_class = Class.new(Model) do
1097
+ def self.to_s
1098
+ "Email"
1099
+ end
1100
+
1101
+ define_method :active_model_serializer do
1102
+ email_serializer
1103
+ end
1104
+ end
1105
+
1106
+ attachment_serializer = Class.new(ActiveModel::Serializer) do
1107
+ attributes :name, :url
1108
+ has_one :attachable, polymorphic: true
1109
+ end
1110
+
1111
+ email = email_class.new subject: 'foo', body: 'bar'
1112
+
1113
+ attachment = Attachment.new name: 'logo.png', url: 'http://example.com/logo.png', attachable: email
1114
+
1115
+ actual = attachment_serializer.new(attachment, {}).as_json
1116
+
1117
+ assert_equal({
1118
+ name: 'logo.png',
1119
+ url: 'http://example.com/logo.png',
1120
+ attachable: {
1121
+ type: :email,
1122
+ email: { subject: 'foo', body: 'bar' }
1123
+ }
1124
+ }, actual)
1125
+ end
1126
+
1127
+ def test_can_handle_polymoprhic_ids
1128
+ email_serializer = Class.new(ActiveModel::Serializer) do
1129
+ attributes :subject, :body
1130
+ end
1131
+
1132
+ email_class = Class.new(Model) do
1133
+ def self.to_s
1134
+ "Email"
1135
+ end
1136
+
1137
+ define_method :active_model_serializer do
1138
+ email_serializer
1139
+ end
1140
+ end
1141
+
1142
+ attachment_serializer = Class.new(ActiveModel::Serializer) do
1143
+ embed :ids
1144
+ attributes :name, :url
1145
+ has_one :attachable, polymorphic: true
1146
+ end
1147
+
1148
+ email = email_class.new id: 1
1149
+
1150
+ attachment = Attachment.new name: 'logo.png', url: 'http://example.com/logo.png', attachable: email
1151
+
1152
+ actual = attachment_serializer.new(attachment, {}).as_json
1153
+
1154
+ assert_equal({
1155
+ name: 'logo.png',
1156
+ url: 'http://example.com/logo.png',
1157
+ attachable: {
1158
+ type: :email,
1159
+ id: 1
1160
+ }
1161
+ }, actual)
1162
+ end
1163
+
1164
+ def test_polymorphic_associations_are_included_at_root
1165
+ email_serializer = Class.new(ActiveModel::Serializer) do
1166
+ attributes :subject, :body, :id
1167
+ end
1168
+
1169
+ email_class = Class.new(Model) do
1170
+ def self.to_s
1171
+ "Email"
1172
+ end
1173
+
1174
+ define_method :active_model_serializer do
1175
+ email_serializer
1176
+ end
1177
+ end
1178
+
1179
+ attachment_serializer = Class.new(ActiveModel::Serializer) do
1180
+ root :attachment
1181
+ embed :ids, include: true
1182
+ attributes :name, :url
1183
+ has_one :attachable, polymorphic: true
1184
+ end
1185
+
1186
+ email = email_class.new id: 1, subject: "Hello", body: "World"
1187
+
1188
+ attachment = Attachment.new name: 'logo.png', url: 'http://example.com/logo.png', attachable: email
1189
+
1190
+ actual = attachment_serializer.new(attachment, {}).as_json
1191
+
1192
+ assert_equal({
1193
+ attachment: {
1194
+ name: 'logo.png',
1195
+ url: 'http://example.com/logo.png',
1196
+ attachable: {
1197
+ type: :email,
1198
+ id: 1
1199
+ }},
1200
+ emails: [{
1201
+ id: 1,
1202
+ subject: "Hello",
1203
+ body: "World"
1204
+ }]
1205
+ }, actual)
1206
+ end
1207
+
1208
+ def test_multiple_polymorphic_associations
1209
+ email_serializer = Class.new(ActiveModel::Serializer) do
1210
+ attributes :subject, :body, :id
1211
+ end
1212
+
1213
+ orange_serializer = Class.new(ActiveModel::Serializer) do
1214
+ embed :ids, include: true
1215
+
1216
+ attributes :plu, :id
1217
+ has_one :readable, polymorphic: true
1218
+ end
1219
+
1220
+ email_class = Class.new(Model) do
1221
+ def self.to_s
1222
+ "Email"
1223
+ end
1224
+
1225
+ define_method :active_model_serializer do
1226
+ email_serializer
1227
+ end
1228
+ end
1229
+
1230
+ orange_class = Class.new(Model) do
1231
+ def self.to_s
1232
+ "Orange"
1233
+ end
1234
+
1235
+ def readable
1236
+ @attributes[:readable]
1237
+ end
1238
+
1239
+ define_method :active_model_serializer do
1240
+ orange_serializer
1241
+ end
1242
+ end
1243
+
1244
+ attachment_serializer = Class.new(ActiveModel::Serializer) do
1245
+ root :attachment
1246
+ embed :ids, include: true
1247
+
1248
+ attributes :name, :url
1249
+
1250
+ has_one :attachable, polymorphic: true
1251
+ has_one :readable, polymorphic: true
1252
+ has_one :edible, polymorphic: true
1253
+ end
1254
+
1255
+ email = email_class.new id: 1, subject: "Hello", body: "World"
1256
+ orange = orange_class.new id: 1, plu: "3027", readable: email
1257
+
1258
+ attachment = Attachment.new({
1259
+ name: 'logo.png',
1260
+ url: 'http://example.com/logo.png',
1261
+ attachable: email,
1262
+ readable: email,
1263
+ edible: orange
1264
+ })
1265
+
1266
+ actual = attachment_serializer.new(attachment, {}).as_json
1267
+
1268
+ assert_equal({
1269
+ emails: [{
1270
+ subject: "Hello",
1271
+ body: "World",
1272
+ id: 1
1273
+ }],
1274
+
1275
+ oranges: [{
1276
+ plu: "3027",
1277
+ id: 1,
1278
+ readable: { type: :email, id: 1 }
1279
+ }],
1280
+
1281
+ attachment: {
1282
+ name: 'logo.png',
1283
+ url: 'http://example.com/logo.png',
1284
+ attachable: { type: :email, id: 1 },
1285
+ readable: { type: :email, id: 1 },
1286
+ edible: { type: :orange, id: 1 }
1287
+ }
1288
+ }, actual)
1289
+ end
1290
+
1291
+ def test_raises_an_error_when_a_child_serializer_includes_associations_when_the_source_doesnt
1292
+ attachment_serializer = Class.new(ActiveModel::Serializer) do
1293
+ attributes :name
1294
+ end
1295
+
1296
+ fruit_serializer = Class.new(ActiveModel::Serializer) do
1297
+ embed :ids, include: true
1298
+ has_one :attachment, serializer: attachment_serializer
1299
+ attribute :color
1300
+ end
1301
+
1302
+ banana_class = Class.new Model do
1303
+ def self.to_s
1304
+ 'banana'
1305
+ end
1306
+
1307
+ def attachment
1308
+ @attributes[:attachment]
1309
+ end
1310
+
1311
+ define_method :active_model_serializer do
1312
+ fruit_serializer
1313
+ end
1314
+ end
1315
+
1316
+ strawberry_class = Class.new Model do
1317
+ def self.to_s
1318
+ 'strawberry'
1319
+ end
1320
+
1321
+ def attachment
1322
+ @attributes[:attachment]
1323
+ end
1324
+
1325
+ define_method :active_model_serializer do
1326
+ fruit_serializer
1327
+ end
1328
+ end
1329
+
1330
+ smoothie = Class.new do
1331
+ attr_reader :base, :flavor
1332
+
1333
+ def initialize(base, flavor)
1334
+ @base, @flavor = base, flavor
1335
+ end
1336
+ end
1337
+
1338
+ smoothie_serializer = Class.new(ActiveModel::Serializer) do
1339
+ root false
1340
+ embed :ids, include: true
1341
+
1342
+ has_one :base, polymorphic: true
1343
+ has_one :flavor, polymorphic: true
1344
+ end
1345
+
1346
+ banana_attachment = Attachment.new({
1347
+ name: 'banana_blending.md',
1348
+ id: 3,
1349
+ })
1350
+
1351
+ strawberry_attachment = Attachment.new({
1352
+ name: 'strawberry_cleaning.doc',
1353
+ id: 4
1354
+ })
1355
+
1356
+ banana = banana_class.new color: "yellow", id: 1, attachment: banana_attachment
1357
+ strawberry = strawberry_class.new color: "red", id: 2, attachment: strawberry_attachment
1358
+
1359
+ smoothie = smoothie_serializer.new(smoothie.new(banana, strawberry))
1360
+
1361
+ assert_raise ActiveModel::Serializer::IncludeError do
1362
+ smoothie.as_json
1363
+ end
1364
+ end
1365
+
1366
+ def tests_includes_does_not_include_nil_polymoprhic_associations
1367
+ post_serializer = Class.new(ActiveModel::Serializer) do
1368
+ root :post
1369
+ embed :ids, include: true
1370
+ has_one :author, polymorphic: true
1371
+ attributes :title
1372
+ end
1373
+
1374
+ post = Post.new(title: 'Foo')
1375
+
1376
+ actual = post_serializer.new(post).as_json
1377
+
1378
+ assert_equal({
1379
+ post: {
1380
+ title: 'Foo',
1381
+ author: nil
1382
+ }
1383
+ }, actual)
1384
+ end
1385
+
1386
+ def test_meta_key_serialization
1387
+ tag_serializer = Class.new(ActiveModel::Serializer) do
1388
+ attributes :name
1389
+ end
1390
+
1391
+ tag_class = Class.new(Model) do
1392
+ def name
1393
+ @attributes[:name]
1394
+ end
1395
+
1396
+ define_method :active_model_serializer do
1397
+ tag_serializer
1398
+ end
1399
+ end
1400
+
1401
+ serializable_array = Class.new(Array)
1402
+
1403
+ array = serializable_array.new
1404
+ array << tag_class.new(name: 'Rails')
1405
+ array << tag_class.new(name: 'Sinatra')
1406
+
1407
+ actual = array.active_model_serializer.new(array, root: :tags, meta: {total: 10}).as_json
1408
+
1409
+ assert_equal({
1410
+ meta: {
1411
+ total: 10,
1412
+ },
1413
+ tags: [
1414
+ { name: "Rails" },
1415
+ { name: "Sinatra" },
1416
+ ]
1417
+ }, actual)
1418
+
1419
+ actual = array.active_model_serializer.new(array, root: :tags, meta: {total: 10}, meta_key: 'meta_object').as_json
1420
+
1421
+ assert_equal({
1422
+ meta_object: {
1423
+ total: 10,
1424
+ },
1425
+ tags: [
1426
+ { name: "Rails" },
1427
+ { name: "Sinatra" },
1428
+ ]
1429
+ }, actual)
1430
+ end
1431
+
1432
+ def test_inheritance_does_not_used_cached_attributes
1433
+ parent = Class.new(ActiveModel::Serializer) do
1434
+ attributes :title
1435
+ end
1436
+
1437
+ child = Class.new(parent) do
1438
+ attributes :body
1439
+ end
1440
+
1441
+ data_class = Class.new do
1442
+ attr_accessor :title, :body
1443
+ end
1444
+
1445
+ item = data_class.new
1446
+ item.title = "title"
1447
+ item.body = "body"
1448
+
1449
+ 2.times do
1450
+ assert_equal({title: "title"},
1451
+ parent.new(item).attributes)
1452
+ assert_equal({body: "body", title: "title"},
1453
+ child.new(item).attributes)
1454
+ end
1455
+
1456
+ end
1457
+
1458
+ def test_scope_name_method
1459
+ serializer = Class.new(ActiveModel::Serializer) do
1460
+ def has_permission?
1461
+ current_user.super_user?
1462
+ end
1463
+ end
1464
+
1465
+ user = User.new
1466
+ user.superuser = true
1467
+ post = Post.new(title: 'Foo')
1468
+
1469
+ a_serializer = serializer.new(post, scope: user, scope_name: :current_user)
1470
+ assert a_serializer.has_permission?
1471
+ end
1472
+
1473
+ def test_only_option_filters_attributes_and_associations
1474
+ post = Post.new(title: "New Post", body: "Body of new post")
1475
+ comments = [Comment.new(title: "Comment1")]
1476
+ post.comments = comments
1477
+
1478
+ post_serializer = PostSerializer.new(post, only: :title)
1479
+
1480
+ assert_equal({
1481
+ post: {
1482
+ title: "New Post"
1483
+ }
1484
+ }, post_serializer.as_json)
1485
+ end
1486
+
1487
+ def test_except_option_filters_attributes_and_associations
1488
+ post = Post.new(title: "New Post", body: "Body of new post")
1489
+ comments = [Comment.new(title: "Comment1")]
1490
+ post.comments = comments
1491
+
1492
+ post_serializer = PostSerializer.new(post, except: [:body, :comments])
1493
+
1494
+ assert_equal({
1495
+ post: {
1496
+ title: "New Post"
1497
+ }
1498
+ }, post_serializer.as_json)
1499
+ end
1500
+
1501
+ def test_only_option_takes_precedence_over_custom_defined_include_methods
1502
+ user = User.new
1503
+
1504
+ post = Post.new(title: "New Post", body: "Body of new post", author: "Sausage King")
1505
+ comments = [Comment.new(title: "Comment")]
1506
+ post.comments = comments
1507
+
1508
+ post_serializer = PostWithMultipleConditionalsSerializer.new(post, scope: user, only: :title)
1509
+
1510
+ # comments enabled
1511
+ post.comments_disabled = false
1512
+ # superuser - should see author
1513
+ user.superuser = true
1514
+
1515
+ assert_equal({
1516
+ post: {
1517
+ title: "New Post"
1518
+ }
1519
+ }, post_serializer.as_json)
1520
+ end
1521
+ end