active_model_serializers 0.8.4 → 0.9.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -45
  3. data/CONTRIBUTING.md +20 -0
  4. data/DESIGN.textile +4 -4
  5. data/{MIT-LICENSE.txt → MIT-LICENSE} +0 -0
  6. data/README.md +187 -113
  7. data/lib/action_controller/serialization.rb +30 -16
  8. data/lib/active_model/array_serializer.rb +36 -82
  9. data/lib/active_model/default_serializer.rb +22 -0
  10. data/lib/active_model/serializable.rb +25 -0
  11. data/lib/active_model/serializer.rb +126 -447
  12. data/lib/active_model/serializer/associations.rb +53 -211
  13. data/lib/active_model/serializer/config.rb +31 -0
  14. data/lib/active_model/serializer/generators/resource_override.rb +13 -0
  15. data/lib/{generators → active_model/serializer/generators}/serializer/USAGE +0 -0
  16. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +14 -0
  17. data/lib/active_model/serializer/generators/serializer/serializer_generator.rb +37 -0
  18. data/lib/active_model/serializer/generators/serializer/templates/controller.rb +93 -0
  19. data/lib/active_model/serializer/generators/serializer/templates/serializer.rb +8 -0
  20. data/lib/active_model/serializer/railtie.rb +10 -0
  21. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  22. data/lib/active_model/serializer_support.rb +5 -0
  23. data/lib/active_model_serializers.rb +7 -86
  24. data/test/coverage_setup.rb +15 -0
  25. data/test/fixtures/active_record.rb +92 -0
  26. data/test/fixtures/poro.rb +64 -0
  27. data/test/integration/action_controller/serialization_test.rb +234 -0
  28. data/test/integration/active_record/active_record_test.rb +77 -0
  29. data/test/integration/generators/resource_generator_test.rb +26 -0
  30. data/test/integration/generators/scaffold_controller_generator_test.rb +67 -0
  31. data/test/integration/generators/serializer_generator_test.rb +41 -0
  32. data/test/test_app.rb +11 -0
  33. data/test/test_helper.rb +7 -41
  34. data/test/tmp/app/serializers/account_serializer.rb +3 -0
  35. data/test/unit/active_model/array_serializer/meta_test.rb +53 -0
  36. data/test/unit/active_model/array_serializer/root_test.rb +102 -0
  37. data/test/unit/active_model/array_serializer/scope_test.rb +24 -0
  38. data/test/unit/active_model/array_serializer/serialization_test.rb +83 -0
  39. data/test/unit/active_model/default_serializer_test.rb +13 -0
  40. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +21 -0
  41. data/test/unit/active_model/serializer/associations_test.rb +19 -0
  42. data/test/unit/active_model/serializer/attributes_test.rb +41 -0
  43. data/test/unit/active_model/serializer/config_test.rb +86 -0
  44. data/test/unit/active_model/serializer/filter_test.rb +49 -0
  45. data/test/unit/active_model/serializer/has_many_test.rb +173 -0
  46. data/test/unit/active_model/serializer/has_one_test.rb +151 -0
  47. data/test/unit/active_model/serializer/meta_test.rb +39 -0
  48. data/test/unit/active_model/serializer/root_test.rb +117 -0
  49. data/test/unit/active_model/serializer/scope_test.rb +49 -0
  50. metadata +78 -74
  51. data/.gitignore +0 -18
  52. data/.travis.yml +0 -34
  53. data/Gemfile +0 -38
  54. data/Rakefile +0 -22
  55. data/active_model_serializers.gemspec +0 -24
  56. data/appveyor.yml +0 -27
  57. data/bench/perf.rb +0 -43
  58. data/cruft.md +0 -19
  59. data/lib/active_record/serializer_override.rb +0 -16
  60. data/lib/generators/resource_override.rb +0 -13
  61. data/lib/generators/serializer/serializer_generator.rb +0 -42
  62. data/lib/generators/serializer/templates/serializer.rb +0 -19
  63. data/test/array_serializer_test.rb +0 -75
  64. data/test/association_test.rb +0 -592
  65. data/test/caching_test.rb +0 -177
  66. data/test/generators_test.rb +0 -85
  67. data/test/no_serialization_scope_test.rb +0 -34
  68. data/test/serialization_scope_name_test.rb +0 -67
  69. data/test/serialization_test.rb +0 -396
  70. data/test/serializer_support_test.rb +0 -51
  71. data/test/serializer_test.rb +0 -1466
  72. data/test/test_fakes.rb +0 -218
@@ -1,51 +0,0 @@
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
-
@@ -1,1466 +0,0 @@
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_embed_objects
458
- serializer = post_serializer
459
-
460
- serializer.class_eval do
461
- root :post
462
- embed :objects
463
- end
464
-
465
- post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
466
- comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
467
- post.comments = comments
468
-
469
- serializer = serializer.new(post)
470
-
471
- assert_equal({
472
- :post => {
473
- :title => "New Post",
474
- :body => "Body of new post",
475
- :author => nil,
476
- :comments => [
477
- { :title => "Comment1" },
478
- { :title => "Comment2" }
479
- ]
480
- }
481
- }, serializer.as_json)
482
- end
483
-
484
- def test_sets_can_be_serialized
485
- post1 = Post.new(:title => "Post1", :author => "Author1", :id => 1)
486
- post2 = Post.new(:title => "Post2", :author => "Author2", :id => 2)
487
-
488
- set = Set.new
489
- set << post1
490
- set << post2
491
-
492
- serializer = set.active_model_serializer.new set, :each_serializer => CustomPostSerializer
493
-
494
- as_json = serializer.as_json
495
- assert_equal 2, as_json.size
496
- assert as_json.include?({ :title => "Post1" })
497
- assert as_json.include?({ :title => "Post2" })
498
- end
499
-
500
- def test_associations_with_as
501
- posts = [
502
- Post.new(:title => 'First Post', :body => 'text'),
503
- Post.new(:title => 'Second Post', :body => 'text')
504
- ]
505
- user = User.new
506
-
507
- custom_blog = CustomBlog.new
508
- custom_blog.public_posts = posts
509
- custom_blog.public_user = user
510
-
511
- serializer = CustomBlogSerializer.new(custom_blog, :scope => { :scope => true })
512
-
513
- assert_equal({
514
- :custom_blog => {
515
- :posts => [
516
- {:title => 'First Post', :body => 'text', :comments => []},
517
- {:title => 'Second Post', :body => 'text', :comments => []}
518
- ],
519
- :user => {
520
- :first_name => "Jose",
521
- :last_name => "Valim", :ok => true,
522
- :scope => true
523
- }
524
- }
525
- }, serializer.as_json)
526
- end
527
-
528
- def test_implicity_detection_for_association_serializers
529
- implicit_serializer = Class.new(ActiveModel::Serializer) do
530
- root :custom_blog
531
- const_set(:UserSerializer, UserSerializer)
532
- const_set(:PostSerializer, PostSerializer)
533
-
534
- has_many :public_posts, :key => :posts
535
- has_one :public_user, :key => :user
536
- end
537
-
538
- posts = [
539
- Post.new(:title => 'First Post', :body => 'text', :comments => []),
540
- Post.new(:title => 'Second Post', :body => 'text', :comments => [])
541
- ]
542
- user = User.new
543
-
544
- custom_blog = CustomBlog.new
545
- custom_blog.public_posts = posts
546
- custom_blog.public_user = user
547
-
548
- serializer = implicit_serializer.new(custom_blog, :scope => { :scope => true })
549
-
550
- assert_equal({
551
- :custom_blog => {
552
- :posts => [
553
- {:title => 'First Post', :body => 'text', :comments => []},
554
- {:title => 'Second Post', :body => 'text', :comments => []}
555
- ],
556
- :user => {
557
- :first_name => "Jose",
558
- :last_name => "Valim", :ok => true,
559
- :scope => true
560
- }
561
- }
562
- }, serializer.as_json)
563
- end
564
-
565
- def test_attribute_key
566
- serializer_class = Class.new(ActiveModel::Serializer) do
567
- root :user
568
-
569
- attribute :first_name, :key => :firstName
570
- attribute :last_name, :key => :lastName
571
- attribute :password
572
- end
573
-
574
- serializer = serializer_class.new(User.new)
575
-
576
- assert_equal({
577
- :user => {
578
- :firstName => "Jose",
579
- :lastName => "Valim",
580
- :password => "oh noes yugive my password"
581
- }
582
- }, serializer.as_json)
583
- end
584
-
585
- def setup_model
586
- Class.new do
587
- class << self
588
- def columns_hash
589
- { "name" => Struct.new(:type).new(:string), "age" => Struct.new(:type).new(:integer) }
590
- end
591
-
592
- def reflect_on_association(name)
593
- case name
594
- when :posts
595
- Struct.new(:macro, :name).new(:has_many, :posts)
596
- when :parent
597
- Struct.new(:macro, :name).new(:belongs_to, :parent)
598
- end
599
- end
600
- end
601
- end
602
- end
603
-
604
- def test_schema
605
- model = setup_model
606
-
607
- serializer = Class.new(ActiveModel::Serializer) do
608
- class << self; self; end.class_eval do
609
- define_method(:model_class) do model end
610
- end
611
-
612
- # Computed attributes (not real columns or associations).
613
- def can_edit; end
614
- def can_view; end
615
- def drafts; end
616
-
617
- attributes :name, :age, {:can_edit => :boolean}, :can_view
618
- has_many :posts, :serializer => Class.new
619
- has_many :drafts, :serializer => Class.new
620
- has_one :parent, :serializer => Class.new
621
- end
622
-
623
- assert_equal serializer.schema, {
624
- :attributes => { :name => :string, :age => :integer, :can_edit => :boolean, :can_view => nil },
625
- :associations => {
626
- :posts => { :has_many => :posts },
627
- :drafts => nil,
628
- :parent => { :belongs_to => :parent }
629
- }
630
- }
631
- end
632
-
633
- def test_schema_with_as
634
- model = setup_model
635
-
636
- serializer = Class.new(ActiveModel::Serializer) do
637
- class << self; self; end.class_eval do
638
- define_method(:model_class) do model end
639
- end
640
-
641
- attributes :name, :age
642
- has_many :posts, :key => :my_posts, :serializer => Class.new
643
- has_one :parent, :key => :my_parent, :serializer => Class.new
644
- end
645
-
646
- assert_equal serializer.schema, {
647
- :attributes => { :name => :string, :age => :integer },
648
- :associations => {
649
- :my_posts => { :has_many => :posts },
650
- :my_parent => { :belongs_to => :parent }
651
- }
652
- }
653
- end
654
-
655
- def test_embed_id_for_has_one
656
- author_serializer = Class.new(ActiveModel::Serializer)
657
-
658
- serializer_class = Class.new(ActiveModel::Serializer) do
659
- embed :ids
660
- root :post
661
-
662
- attributes :title, :body
663
- has_one :author, :serializer => author_serializer
664
- end
665
-
666
- post_class = Class.new(Model) do
667
- attr_accessor :author
668
- end
669
-
670
- author_class = Class.new(Model)
671
-
672
- post = post_class.new(:title => "New Post", :body => "It's a new post!")
673
- author = author_class.new(:id => 5)
674
- post.author = author
675
-
676
- hash = serializer_class.new(post)
677
-
678
- assert_equal({
679
- :post => {
680
- :title => "New Post",
681
- :body => "It's a new post!",
682
- :author_id => 5
683
- }
684
- }, hash.as_json)
685
- end
686
-
687
- def test_embed_objects_for_has_one
688
- author_serializer = Class.new(ActiveModel::Serializer) do
689
- attributes :id, :name
690
- end
691
-
692
- serializer_class = Class.new(ActiveModel::Serializer) do
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, :name => "Tom Dale")
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, :name => "Tom Dale" }
716
- }
717
- }, hash.as_json)
718
- end
719
-
720
- def test_root_provided_in_options
721
- author_serializer = Class.new(ActiveModel::Serializer) do
722
- attributes :id, :name
723
- end
724
-
725
- serializer_class = Class.new(ActiveModel::Serializer) do
726
- root :post
727
-
728
- attributes :title, :body
729
- has_one :author, :serializer => author_serializer
730
- end
731
-
732
- post_class = Class.new(Model) do
733
- attr_accessor :author
734
- end
735
-
736
- author_class = Class.new(Model)
737
-
738
- post = post_class.new(:title => "New Post", :body => "It's a new post!")
739
- author = author_class.new(:id => 5, :name => "Tom Dale")
740
- post.author = author
741
-
742
- assert_equal({
743
- :blog_post => {
744
- :title => "New Post",
745
- :body => "It's a new post!",
746
- :author => { :id => 5, :name => "Tom Dale" }
747
- }
748
- }, serializer_class.new(post, :root => :blog_post).as_json)
749
-
750
- assert_equal({
751
- :title => "New Post",
752
- :body => "It's a new post!",
753
- :author => { :id => 5, :name => "Tom Dale" }
754
- }, serializer_class.new(post, :root => false).as_json)
755
-
756
- assert_equal({
757
- :blog_post => {
758
- :title => "New Post",
759
- :body => "It's a new post!",
760
- :author => { :id => 5, :name => "Tom Dale" }
761
- }
762
- }, serializer_class.new(post).as_json(:root => :blog_post))
763
-
764
- assert_equal({
765
- :title => "New Post",
766
- :body => "It's a new post!",
767
- :author => { :id => 5, :name => "Tom Dale" }
768
- }, serializer_class.new(post).as_json(:root => false))
769
- end
770
-
771
- def test_serializer_has_access_to_root_object
772
- hash_object = nil
773
-
774
- author_serializer = Class.new(ActiveModel::Serializer) do
775
- attributes :id, :name
776
-
777
- define_method :serializable_hash do
778
- hash_object = @options[:hash]
779
- super()
780
- end
781
- end
782
-
783
- serializer_class = Class.new(ActiveModel::Serializer) do
784
- root :post
785
-
786
- attributes :title, :body
787
- has_one :author, :serializer => author_serializer
788
- end
789
-
790
- post_class = Class.new(Model) do
791
- attr_accessor :author
792
- end
793
-
794
- author_class = Class.new(Model)
795
-
796
- post = post_class.new(:title => "New Post", :body => "It's a new post!")
797
- author = author_class.new(:id => 5, :name => "Tom Dale")
798
- post.author = author
799
-
800
- expected = serializer_class.new(post).as_json
801
- assert_equal expected, hash_object
802
- end
803
-
804
- def test_embed_ids_include_true_with_root
805
- serializer_class = post_serializer
806
-
807
- serializer_class.class_eval do
808
- root :post
809
- embed :ids, :include => true
810
- has_many :comments, :key => :comment_ids, :root => :comments
811
- has_one :author, :serializer => DefaultUserSerializer, :key => :author_id, :root => :author
812
- end
813
-
814
- post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
815
- comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
816
- post.comments = comments
817
-
818
- serializer = serializer_class.new(post)
819
-
820
- assert_equal({
821
- :post => {
822
- :title => "New Post",
823
- :body => "Body of new post",
824
- :comment_ids => [1, 2],
825
- :author_id => nil
826
- },
827
- :comments => [
828
- { :title => "Comment1" },
829
- { :title => "Comment2" }
830
- ],
831
- :author => []
832
- }, serializer.as_json)
833
-
834
- post.author = User.new(:id => 1)
835
-
836
- serializer = serializer_class.new(post)
837
-
838
- assert_equal({
839
- :post => {
840
- :title => "New Post",
841
- :body => "Body of new post",
842
- :comment_ids => [1, 2],
843
- :author_id => 1
844
- },
845
- :comments => [
846
- { :title => "Comment1" },
847
- { :title => "Comment2" }
848
- ],
849
- :author => [{ :first_name => "Jose", :last_name => "Valim" }]
850
- }, serializer.as_json)
851
- end
852
-
853
- # the point of this test is to illustrate that deeply nested serializers
854
- # still side-load at the root.
855
- def test_embed_with_include_inserts_at_root
856
- tag_serializer = Class.new(ActiveModel::Serializer) do
857
- attributes :id, :name
858
- end
859
-
860
- comment_serializer = Class.new(ActiveModel::Serializer) do
861
- embed :ids, :include => true
862
- attributes :id, :body
863
- has_many :tags, :serializer => tag_serializer
864
- end
865
-
866
- post_serializer = Class.new(ActiveModel::Serializer) do
867
- embed :ids, :include => true
868
- attributes :id, :title, :body
869
- has_many :comments, :serializer => comment_serializer
870
- end
871
-
872
- post_class = Class.new(Model) do
873
- attr_accessor :comments
874
-
875
- define_method :active_model_serializer do
876
- post_serializer
877
- end
878
- end
879
-
880
- comment_class = Class.new(Model) do
881
- attr_accessor :tags
882
- end
883
-
884
- tag_class = Class.new(Model)
885
-
886
- post = post_class.new(:title => "New Post", :body => "NEW POST", :id => 1)
887
- comment1 = comment_class.new(:body => "EWOT", :id => 1)
888
- comment2 = comment_class.new(:body => "YARLY", :id => 2)
889
- tag1 = tag_class.new(:name => "lolcat", :id => 1)
890
- tag2 = tag_class.new(:name => "nyancat", :id => 2)
891
- tag3 = tag_class.new(:name => "violetcat", :id => 3)
892
-
893
- post.comments = [comment1, comment2]
894
- comment1.tags = [tag1, tag3]
895
- comment2.tags = [tag1, tag2]
896
-
897
- actual = ActiveModel::ArraySerializer.new([post], :root => :posts).as_json
898
- assert_equal({
899
- :posts => [
900
- { :title => "New Post", :body => "NEW POST", :id => 1, :comment_ids => [1,2] }
901
- ],
902
-
903
- :comments => [
904
- { :body => "EWOT", :id => 1, :tag_ids => [1,3] },
905
- { :body => "YARLY", :id => 2, :tag_ids => [1,2] }
906
- ],
907
-
908
- :tags => [
909
- { :name => "lolcat", :id => 1 },
910
- { :name => "violetcat", :id => 3 },
911
- { :name => "nyancat", :id => 2 }
912
- ]
913
- }, actual)
914
- end
915
-
916
- def test_can_customize_attributes
917
- serializer = Class.new(ActiveModel::Serializer) do
918
- attributes :title, :body
919
-
920
- def title
921
- object.title.upcase
922
- end
923
- end
924
-
925
- klass = Class.new do
926
- def read_attribute_for_serialization(name)
927
- { :title => "New post!", :body => "First post body" }[name]
928
- end
929
-
930
- def title
931
- read_attribute_for_serialization(:title)
932
- end
933
-
934
- def body
935
- read_attribute_for_serialization(:body)
936
- end
937
- end
938
-
939
- object = klass.new
940
-
941
- actual = serializer.new(object, :root => :post).as_json
942
-
943
- assert_equal({
944
- :post => {
945
- :title => "NEW POST!",
946
- :body => "First post body"
947
- }
948
- }, actual)
949
- end
950
-
951
- def test_can_customize_attributes_with_read_attributes
952
- serializer = Class.new(ActiveModel::Serializer) do
953
- attributes :title, :body
954
-
955
- def read_attribute_for_serialization(name)
956
- { :title => "New post!", :body => "First post body" }[name]
957
- end
958
- end
959
-
960
- actual = serializer.new(Object.new, :root => :post).as_json
961
-
962
- assert_equal({
963
- :post => {
964
- :title => "New post!",
965
- :body => "First post body"
966
- }
967
- }, actual)
968
- end
969
-
970
- def test_active_support_on_load_hooks_fired
971
- loaded = nil
972
- ActiveSupport.on_load(:active_model_serializers) do
973
- loaded = self
974
- end
975
- assert_equal ActiveModel::Serializer, loaded
976
- end
977
-
978
- def tests_query_attributes_strip_question_mark
979
- todo = Class.new do
980
- def overdue?
981
- true
982
- end
983
-
984
- def read_attribute_for_serialization(name)
985
- send name
986
- end
987
- end
988
-
989
- serializer = Class.new(ActiveModel::Serializer) do
990
- attribute :overdue?
991
- end
992
-
993
- actual = serializer.new(todo.new).as_json
994
-
995
- assert_equal({
996
- :overdue => true
997
- }, actual)
998
- end
999
-
1000
- def tests_query_attributes_allow_key_option
1001
- todo = Class.new do
1002
- def overdue?
1003
- true
1004
- end
1005
-
1006
- def read_attribute_for_serialization(name)
1007
- send name
1008
- end
1009
- end
1010
-
1011
- serializer = Class.new(ActiveModel::Serializer) do
1012
- attribute :overdue?, :key => :foo
1013
- end
1014
-
1015
- actual = serializer.new(todo.new).as_json
1016
-
1017
- assert_equal({
1018
- :foo => true
1019
- }, actual)
1020
- end
1021
-
1022
- def tests_can_handle_polymorphism
1023
- email_serializer = Class.new(ActiveModel::Serializer) do
1024
- attributes :subject, :body
1025
- end
1026
-
1027
- email_class = Class.new(Model) do
1028
- def self.to_s
1029
- "Email"
1030
- end
1031
-
1032
- define_method :active_model_serializer do
1033
- email_serializer
1034
- end
1035
- end
1036
-
1037
- attachment_serializer = Class.new(ActiveModel::Serializer) do
1038
- attributes :name, :url
1039
- has_one :attachable, :polymorphic => true
1040
- end
1041
-
1042
- email = email_class.new :subject => 'foo', :body => 'bar'
1043
-
1044
- attachment = Attachment.new :name => 'logo.png', :url => 'http://example.com/logo.png', :attachable => email
1045
-
1046
- actual = attachment_serializer.new(attachment, {}).as_json
1047
-
1048
- assert_equal({
1049
- :name => 'logo.png',
1050
- :url => 'http://example.com/logo.png',
1051
- :attachable => {
1052
- :type => :email,
1053
- :email => { :subject => 'foo', :body => 'bar' }
1054
- }
1055
- }, actual)
1056
- end
1057
-
1058
- def test_can_handle_polymoprhic_ids
1059
- email_serializer = Class.new(ActiveModel::Serializer) do
1060
- attributes :subject, :body
1061
- end
1062
-
1063
- email_class = Class.new(Model) do
1064
- def self.to_s
1065
- "Email"
1066
- end
1067
-
1068
- define_method :active_model_serializer do
1069
- email_serializer
1070
- end
1071
- end
1072
-
1073
- attachment_serializer = Class.new(ActiveModel::Serializer) do
1074
- embed :ids
1075
- attributes :name, :url
1076
- has_one :attachable, :polymorphic => true
1077
- end
1078
-
1079
- email = email_class.new :id => 1
1080
-
1081
- attachment = Attachment.new :name => 'logo.png', :url => 'http://example.com/logo.png', :attachable => email
1082
-
1083
- actual = attachment_serializer.new(attachment, {}).as_json
1084
-
1085
- assert_equal({
1086
- :name => 'logo.png',
1087
- :url => 'http://example.com/logo.png',
1088
- :attachable => {
1089
- :type => :email,
1090
- :id => 1
1091
- }
1092
- }, actual)
1093
- end
1094
-
1095
- def test_polymorphic_associations_are_included_at_root
1096
- email_serializer = Class.new(ActiveModel::Serializer) do
1097
- attributes :subject, :body, :id
1098
- end
1099
-
1100
- email_class = Class.new(Model) do
1101
- def self.to_s
1102
- "Email"
1103
- end
1104
-
1105
- define_method :active_model_serializer do
1106
- email_serializer
1107
- end
1108
- end
1109
-
1110
- attachment_serializer = Class.new(ActiveModel::Serializer) do
1111
- root :attachment
1112
- embed :ids, :include => true
1113
- attributes :name, :url
1114
- has_one :attachable, :polymorphic => true
1115
- end
1116
-
1117
- email = email_class.new :id => 1, :subject => "Hello", :body => "World"
1118
-
1119
- attachment = Attachment.new :name => 'logo.png', :url => 'http://example.com/logo.png', :attachable => email
1120
-
1121
- actual = attachment_serializer.new(attachment, {}).as_json
1122
-
1123
- assert_equal({
1124
- :attachment => {
1125
- :name => 'logo.png',
1126
- :url => 'http://example.com/logo.png',
1127
- :attachable => {
1128
- :type => :email,
1129
- :id => 1
1130
- }},
1131
- :emails => [{
1132
- :id => 1,
1133
- :subject => "Hello",
1134
- :body => "World"
1135
- }]
1136
- }, actual)
1137
- end
1138
-
1139
- def test_multiple_polymorphic_associations
1140
- email_serializer = Class.new(ActiveModel::Serializer) do
1141
- attributes :subject, :body, :id
1142
- end
1143
-
1144
- orange_serializer = Class.new(ActiveModel::Serializer) do
1145
- embed :ids, :include => true
1146
-
1147
- attributes :plu, :id
1148
- has_one :readable, :polymorphic => true
1149
- end
1150
-
1151
- email_class = Class.new(Model) do
1152
- def self.to_s
1153
- "Email"
1154
- end
1155
-
1156
- define_method :active_model_serializer do
1157
- email_serializer
1158
- end
1159
- end
1160
-
1161
- orange_class = Class.new(Model) do
1162
- def self.to_s
1163
- "Orange"
1164
- end
1165
-
1166
- def readable
1167
- @attributes[:readable]
1168
- end
1169
-
1170
- define_method :active_model_serializer do
1171
- orange_serializer
1172
- end
1173
- end
1174
-
1175
- attachment_serializer = Class.new(ActiveModel::Serializer) do
1176
- root :attachment
1177
- embed :ids, :include => true
1178
-
1179
- attributes :name, :url
1180
-
1181
- has_one :attachable, :polymorphic => true
1182
- has_one :readable, :polymorphic => true
1183
- has_one :edible, :polymorphic => true
1184
- end
1185
-
1186
- email = email_class.new :id => 1, :subject => "Hello", :body => "World"
1187
- orange = orange_class.new :id => 1, :plu => "3027", :readable => email
1188
-
1189
- attachment = Attachment.new({
1190
- :name => 'logo.png',
1191
- :url => 'http://example.com/logo.png',
1192
- :attachable => email,
1193
- :readable => email,
1194
- :edible => orange
1195
- })
1196
-
1197
- actual = attachment_serializer.new(attachment, {}).as_json
1198
-
1199
- assert_equal({
1200
- :emails => [{
1201
- :subject => "Hello",
1202
- :body => "World",
1203
- :id => 1
1204
- }],
1205
-
1206
- :oranges => [{
1207
- :plu => "3027",
1208
- :id => 1,
1209
- :readable => { :type => :email, :id => 1 }
1210
- }],
1211
-
1212
- :attachment => {
1213
- :name => 'logo.png',
1214
- :url => 'http://example.com/logo.png',
1215
- :attachable => { :type => :email, :id => 1 },
1216
- :readable => { :type => :email, :id => 1 },
1217
- :edible => { :type => :orange, :id => 1 }
1218
- }
1219
- }, actual)
1220
- end
1221
-
1222
- def test_raises_an_error_when_a_child_serializer_includes_associations_when_the_source_doesnt
1223
- attachment_serializer = Class.new(ActiveModel::Serializer) do
1224
- attributes :name
1225
- end
1226
-
1227
- fruit_serializer = Class.new(ActiveModel::Serializer) do
1228
- embed :ids, :include => true
1229
- has_one :attachment, :serializer => attachment_serializer
1230
- attribute :color
1231
- end
1232
-
1233
- banana_class = Class.new Model do
1234
- def self.to_s
1235
- 'banana'
1236
- end
1237
-
1238
- def attachment
1239
- @attributes[:attachment]
1240
- end
1241
-
1242
- define_method :active_model_serializer do
1243
- fruit_serializer
1244
- end
1245
- end
1246
-
1247
- strawberry_class = Class.new Model do
1248
- def self.to_s
1249
- 'strawberry'
1250
- end
1251
-
1252
- def attachment
1253
- @attributes[:attachment]
1254
- end
1255
-
1256
- define_method :active_model_serializer do
1257
- fruit_serializer
1258
- end
1259
- end
1260
-
1261
- smoothie = Class.new do
1262
- attr_reader :base, :flavor
1263
-
1264
- def initialize(base, flavor)
1265
- @base, @flavor = base, flavor
1266
- end
1267
- end
1268
-
1269
- smoothie_serializer = Class.new(ActiveModel::Serializer) do
1270
- root false
1271
- embed :ids, :include => true
1272
-
1273
- has_one :base, :polymorphic => true
1274
- has_one :flavor, :polymorphic => true
1275
- end
1276
-
1277
- banana_attachment = Attachment.new({
1278
- :name => 'banana_blending.md',
1279
- :id => 3,
1280
- })
1281
-
1282
- strawberry_attachment = Attachment.new({
1283
- :name => 'strawberry_cleaning.doc',
1284
- :id => 4
1285
- })
1286
-
1287
- banana = banana_class.new :color => "yellow", :id => 1, :attachment => banana_attachment
1288
- strawberry = strawberry_class.new :color => "red", :id => 2, :attachment => strawberry_attachment
1289
-
1290
- smoothie = smoothie_serializer.new(smoothie.new(banana, strawberry))
1291
-
1292
- assert_raise ActiveModel::Serializer::IncludeError do
1293
- smoothie.as_json
1294
- end
1295
- end
1296
-
1297
- def tests_includes_does_not_include_nil_polymoprhic_associations
1298
- post_serializer = Class.new(ActiveModel::Serializer) do
1299
- root :post
1300
- embed :ids, :include => true
1301
- has_one :author, :polymorphic => true
1302
- attributes :title
1303
- end
1304
-
1305
- post = Post.new(:title => 'Foo')
1306
-
1307
- actual = post_serializer.new(post).as_json
1308
-
1309
- assert_equal({
1310
- :post => {
1311
- :title => 'Foo',
1312
- :author => nil
1313
- }
1314
- }, actual)
1315
- end
1316
-
1317
- def test_meta_key_serialization
1318
- tag_serializer = Class.new(ActiveModel::Serializer) do
1319
- attributes :name
1320
- end
1321
-
1322
- tag_class = Class.new(Model) do
1323
- def name
1324
- @attributes[:name]
1325
- end
1326
-
1327
- define_method :active_model_serializer do
1328
- tag_serializer
1329
- end
1330
- end
1331
-
1332
- serializable_array = Class.new(Array)
1333
-
1334
- array = serializable_array.new
1335
- array << tag_class.new(:name => 'Rails')
1336
- array << tag_class.new(:name => 'Sinatra')
1337
-
1338
- actual = array.active_model_serializer.new(array, :root => :tags, :meta => {:total => 10}).as_json
1339
-
1340
- assert_equal({
1341
- :meta => {
1342
- :total => 10,
1343
- },
1344
- :tags => [
1345
- { :name => "Rails" },
1346
- { :name => "Sinatra" },
1347
- ]
1348
- }, actual)
1349
-
1350
- actual = array.active_model_serializer.new(array, :root => :tags, :meta => {:total => 10}, :meta_key => 'meta_object').as_json
1351
-
1352
- assert_equal({
1353
- :meta_object => {
1354
- :total => 10,
1355
- },
1356
- :tags => [
1357
- { :name => "Rails" },
1358
- { :name => "Sinatra" },
1359
- ]
1360
- }, actual)
1361
- end
1362
-
1363
- def test_inheritance_does_not_used_cached_attributes
1364
- parent = Class.new(ActiveModel::Serializer) do
1365
- attributes :title
1366
- end
1367
-
1368
- child = Class.new(parent) do
1369
- attributes :body
1370
- end
1371
-
1372
- data_class = Class.new do
1373
- include ActiveModel::Serializers::JSON
1374
- attr_accessor :title, :body
1375
- end
1376
-
1377
- item = data_class.new
1378
- item.title = "title"
1379
- item.body = "body"
1380
-
1381
- 2.times do
1382
- assert_equal({:title => "title"},
1383
- parent.new(item).attributes)
1384
- assert_equal({:body => "body", :title => "title"},
1385
- child.new(item).attributes)
1386
- end
1387
-
1388
- end
1389
-
1390
- def test_scope_name_method
1391
- serializer = Class.new(ActiveModel::Serializer) do
1392
- def has_permission?
1393
- current_user.super_user?
1394
- end
1395
- end
1396
-
1397
- user = User.new
1398
- user.superuser = true
1399
- post = Post.new(:title => 'Foo')
1400
-
1401
- a_serializer = serializer.new(post, :scope => user, :scope_name => :current_user)
1402
- assert a_serializer.has_permission?
1403
- end
1404
-
1405
- def test_only_option_filters_attributes_and_associations
1406
- post = Post.new(:title => "New Post", :body => "Body of new post")
1407
- comments = [Comment.new(:title => "Comment1")]
1408
- post.comments = comments
1409
-
1410
- post_serializer = PostSerializer.new(post, :only => :title)
1411
-
1412
- assert_equal({
1413
- :post => {
1414
- :title => "New Post"
1415
- }
1416
- }, post_serializer.as_json)
1417
- end
1418
-
1419
- def test_except_option_filters_attributes_and_associations
1420
- post = Post.new(:title => "New Post", :body => "Body of new post")
1421
- comments = [Comment.new(:title => "Comment1")]
1422
- post.comments = comments
1423
-
1424
- post_serializer = PostSerializer.new(post, :except => [:body, :comments])
1425
-
1426
- assert_equal({
1427
- :post => {
1428
- :title => "New Post"
1429
- }
1430
- }, post_serializer.as_json)
1431
- end
1432
-
1433
- def test_only_option_takes_precedence_over_custom_defined_include_methods
1434
- user = User.new
1435
-
1436
- post = Post.new(:title => "New Post", :body => "Body of new post", :author => "Sausage King")
1437
- comments = [Comment.new(:title => "Comment")]
1438
- post.comments = comments
1439
-
1440
- post_serializer = PostWithMultipleConditionalsSerializer.new(post, :scope => user, :only => :title)
1441
-
1442
- # comments enabled
1443
- post.comments_disabled = false
1444
- # superuser - should see author
1445
- user.superuser = true
1446
-
1447
- assert_equal({
1448
- :post => {
1449
- :title => "New Post"
1450
- }
1451
- }, post_serializer.as_json)
1452
- end
1453
-
1454
- def test_as_json_with_nil_options
1455
- user = User.new
1456
- user_serializer = DefaultUserSerializer.new(user, {})
1457
-
1458
- # ActiveSupport 3.1 Object#to_json generates this downstream call
1459
- assert_equal({
1460
- :default_user => {
1461
- :first_name => "Jose",
1462
- :last_name => "Valim"
1463
- }
1464
- }, user_serializer.as_json(nil))
1465
- end
1466
- end