active_model_serializers 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,10 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class Foo < Rails::Application
4
+ if Rails.version.start_with? '4'
5
+ config.eager_load = false
6
+ config.secret_key_base = 'abc123'
7
+ end
4
8
  end
5
9
 
6
10
  Rails.application.load_generators
@@ -54,6 +54,19 @@ class RenderJsonTest < ActionController::TestCase
54
54
  end
55
55
  end
56
56
 
57
+ class AnotherCustomSerializer
58
+ def initialize(*)
59
+ end
60
+
61
+ def as_json(*)
62
+ { :rails => 'rocks' }
63
+ end
64
+ end
65
+
66
+ class DummyCustomSerializer < ActiveModel::Serializer
67
+ attributes :id
68
+ end
69
+
57
70
  class HypermediaSerializable
58
71
  def active_model_serializer
59
72
  HypermediaSerializer
@@ -108,6 +121,11 @@ class RenderJsonTest < ActionController::TestCase
108
121
  render :json => ActiveSupport::JSON.encode(:hello => 'world')
109
122
  end
110
123
 
124
+ def render_json_nil_with_custom_serializer
125
+ render :json => nil, :serializer => DummyCustomSerializer
126
+ end
127
+
128
+
111
129
  def render_json_with_extra_options
112
130
  render :json => JsonRenderable.new, :except => [:c, :e]
113
131
  end
@@ -152,6 +170,10 @@ class RenderJsonTest < ActionController::TestCase
152
170
  render :json => [Object.new], :each_serializer => CustomSerializer
153
171
  end
154
172
 
173
+ def render_json_array_with_wrong_option
174
+ render :json => [Object.new], :serializer => CustomSerializer
175
+ end
176
+
155
177
  def render_json_with_links
156
178
  render :json => HypermediaSerializable.new
157
179
  end
@@ -171,9 +193,13 @@ class RenderJsonTest < ActionController::TestCase
171
193
 
172
194
  private
173
195
  def default_serializer_options
174
- if params[:check_defaults]
175
- { :check_defaults => true }
176
- end
196
+ defaults = {}
197
+ defaults.merge!(:check_defaults => true) if params[:check_defaults]
198
+ defaults.merge!(:root => :awesome) if params[:check_default_root]
199
+ defaults.merge!(:scope => :current_admin) if params[:check_default_scope]
200
+ defaults.merge!(:serializer => AnotherCustomSerializer) if params[:check_default_serializer]
201
+ defaults.merge!(:each_serializer => AnotherCustomSerializer) if params[:check_default_each_serializer]
202
+ defaults
177
203
  end
178
204
  end
179
205
 
@@ -199,6 +225,11 @@ class RenderJsonTest < ActionController::TestCase
199
225
  assert_equal '[]', @response.body
200
226
  end
201
227
 
228
+ def test_render_json_nil_with_custom_serializer
229
+ get :render_json_nil_with_custom_serializer
230
+ assert_equal "{\"dummy_custom\":null}", @response.body
231
+ end
232
+
202
233
  def test_render_json
203
234
  get :render_json_hello_world
204
235
  assert_equal '{"hello":"world"}', @response.body
@@ -254,11 +285,26 @@ class RenderJsonTest < ActionController::TestCase
254
285
  assert_match '"check_defaults":true', @response.body
255
286
  end
256
287
 
288
+ def test_render_json_with_serializer_checking_default_serailizer
289
+ get :render_json_with_serializer, :check_default_serializer => true
290
+ assert_match '{"rails":"rocks"}', @response.body
291
+ end
292
+
293
+ def test_render_json_with_serializer_checking_default_scope
294
+ get :render_json_with_serializer, :check_default_scope => true
295
+ assert_match '"scope":"current_admin"', @response.body
296
+ end
297
+
257
298
  def test_render_json_with_serializer_and_implicit_root
258
299
  get :render_json_with_serializer_and_implicit_root
259
300
  assert_match '"test":[{"serializable_object":true}]', @response.body
260
301
  end
261
302
 
303
+ def test_render_json_with_serializer_and_implicit_root_checking_default_each_serailizer
304
+ get :render_json_with_serializer_and_implicit_root, :check_default_each_serializer => true
305
+ assert_match '"test":[{"rails":"rocks"}]', @response.body
306
+ end
307
+
262
308
  def test_render_json_with_serializer_and_options
263
309
  get :render_json_with_serializer_and_options
264
310
  assert_match '"scope":{"current_user":true}', @response.body
@@ -271,6 +317,11 @@ class RenderJsonTest < ActionController::TestCase
271
317
  assert_match '"scope":{"current_user":false}', @response.body
272
318
  end
273
319
 
320
+ def test_render_json_with_serializer_and_scope_option_checking_default_scope
321
+ get :render_json_with_serializer_and_scope_option, :check_default_scope => true
322
+ assert_match '"scope":{"current_user":false}', @response.body
323
+ end
324
+
274
325
  def test_render_json_with_serializer_api_but_without_serializer
275
326
  get :render_json_with_serializer_api_but_without_serializer
276
327
  assert_match '{"serializable_object":true}', @response.body
@@ -281,11 +332,27 @@ class RenderJsonTest < ActionController::TestCase
281
332
  assert_match '{"hello":true}', @response.body
282
333
  end
283
334
 
335
+ def test_render_json_with_custom_serializer_checking_default_serailizer
336
+ get :render_json_with_custom_serializer, :check_default_serializer => true
337
+ assert_match '{"hello":true}', @response.body
338
+ end
339
+
284
340
  def test_render_json_array_with_custom_serializer
285
341
  get :render_json_array_with_custom_serializer
286
342
  assert_match '{"test":[{"hello":true}]}', @response.body
287
343
  end
288
344
 
345
+ def test_render_json_array_with_wrong_option
346
+ assert_raise ArgumentError do
347
+ get :render_json_array_with_wrong_option
348
+ end
349
+ end
350
+
351
+ def test_render_json_array_with_custom_serializer_checking_default_each_serailizer
352
+ get :render_json_array_with_custom_serializer, :check_default_each_serializer => true
353
+ assert_match '{"test":[{"hello":true}]}', @response.body
354
+ end
355
+
289
356
  def test_render_json_with_links
290
357
  get :render_json_with_links
291
358
  assert_match '{"link":"http://www.nextangle.com/hypermedia"}', @response.body
@@ -296,11 +363,21 @@ class RenderJsonTest < ActionController::TestCase
296
363
  assert_equal '[]', @response.body
297
364
  end
298
365
 
366
+ def test_render_json_array_with_no_root_checking_default_root
367
+ get :render_json_array_with_no_root, :check_default_root => true
368
+ assert_equal '[]', @response.body
369
+ end
370
+
299
371
  def test_render_json_empty_array
300
372
  get :render_json_empty_array
301
373
  assert_equal '{"test":[]}', @response.body
302
374
  end
303
375
 
376
+ def test_render_json_empty_array_checking_default_root
377
+ get :render_json_empty_array, :check_default_root => true
378
+ assert_equal '{"awesome":[]}', @response.body
379
+ end
380
+
304
381
  def test_render_json_empty_arry_with_array_serializer_root_false
305
382
  ActiveModel::ArraySerializer.root = false
306
383
  get :render_json_empty_array
@@ -4,6 +4,13 @@ class RandomModel
4
4
  include ActiveModel::SerializerSupport
5
5
  end
6
6
 
7
+ class OtherRandomModel
8
+ include ActiveModel::SerializerSupport
9
+ end
10
+
11
+ class OtherRandomModelSerializer
12
+ end
13
+
7
14
  class RandomModelCollection
8
15
  include ActiveModel::ArraySerializerSupport
9
16
  end
@@ -18,6 +25,10 @@ class SerializerSupportTest < ActiveModel::TestCase
18
25
  assert_equal nil, RandomModel.new.active_model_serializer
19
26
  end
20
27
 
28
+ test "it returns a deducted serializer if it exists exists" do
29
+ assert_equal OtherRandomModelSerializer, OtherRandomModel.new.active_model_serializer
30
+ end
31
+
21
32
  test "it returns ArraySerializer for a collection" do
22
33
  assert_equal ActiveModel::ArraySerializer, RandomModelCollection.new.active_model_serializer
23
34
  end
@@ -1,119 +1,64 @@
1
1
  require "test_helper"
2
+ require "test_fakes"
2
3
 
3
4
  class SerializerTest < ActiveModel::TestCase
4
- class Model
5
- def initialize(hash={})
6
- @attributes = hash
7
- end
8
-
9
- def read_attribute_for_serialization(name)
10
- @attributes[name]
11
- end
12
-
13
- def as_json(*)
14
- { :model => "Model" }
15
- end
16
- end
17
-
18
- class User
19
- include ActiveModel::SerializerSupport
20
-
21
- attr_accessor :superuser
22
-
23
- def initialize(hash={})
24
- @attributes = hash.merge(:first_name => "Jose", :last_name => "Valim", :password => "oh noes yugive my password")
25
- end
26
-
27
- def read_attribute_for_serialization(name)
28
- @attributes[name]
29
- end
30
-
31
- def super_user?
32
- @superuser
33
- end
34
- end
35
-
36
- class Post < Model
37
- def initialize(attributes)
38
- super(attributes)
39
- self.comments ||= []
40
- self.comments_disabled = false
41
- self.author = nil
42
- end
43
-
44
- attr_accessor :comments, :comments_disabled, :author
45
- def active_model_serializer; PostSerializer; end
5
+ def test_scope_works_correct
6
+ serializer = ActiveModel::Serializer.new :foo, :scope => :bar
7
+ assert_equal serializer.scope, :bar
46
8
  end
47
9
 
48
- class Comment < Model
49
- def active_model_serializer; CommentSerializer; end
50
- end
10
+ def test_attributes
11
+ user = User.new
12
+ user_serializer = DefaultUserSerializer.new(user, {})
51
13
 
52
- class UserSerializer < ActiveModel::Serializer
53
- attributes :first_name, :last_name
14
+ hash = user_serializer.as_json
54
15
 
55
- def serializable_hash
56
- attributes.merge(:ok => true).merge(options[:scope])
57
- end
16
+ assert_equal({
17
+ :default_user => { :first_name => "Jose", :last_name => "Valim" }
18
+ }, hash)
58
19
  end
59
20
 
60
- class DefaultUserSerializer < ActiveModel::Serializer
61
- attributes :first_name, :last_name
62
- end
21
+ def test_attributes_method
22
+ user = User.new
23
+ user_serializer = UserSerializer.new(user, :scope => {})
63
24
 
64
- class MyUserSerializer < ActiveModel::Serializer
65
- attributes :first_name, :last_name
25
+ hash = user_serializer.as_json
66
26
 
67
- def serializable_hash
68
- hash = attributes
69
- hash = hash.merge(:super_user => true) if my_user.super_user?
70
- hash
71
- end
27
+ assert_equal({
28
+ :user => { :first_name => "Jose", :last_name => "Valim", :ok => true }
29
+ }, hash)
72
30
  end
73
31
 
74
- class CommentSerializer
75
- def initialize(comment, options={})
76
- @comment = comment
77
- end
78
-
79
- def serializable_hash
80
- { :title => @comment.read_attribute_for_serialization(:title) }
81
- end
32
+ def test_attributes_method_specifying_keys
33
+ user = User.new
34
+ user_serializer = UserAttributesWithKeySerializer.new(user, :scope => {})
82
35
 
83
- def as_json(options=nil)
84
- options ||= {}
85
- if options[:root] == false
86
- serializable_hash
87
- else
88
- { :comment => serializable_hash }
89
- end
90
- end
91
- end
36
+ hash = user_serializer.as_json
92
37
 
93
- def test_scope_works_correct
94
- serializer = ActiveModel::Serializer.new :foo, :scope => :bar
95
- assert_equal serializer.scope, :bar
38
+ assert_equal({
39
+ :user_attributes_with_key => { :f_name => "Jose", :l_name => "Valim", :ok => true }
40
+ }, hash)
96
41
  end
97
42
 
98
- def test_attributes
43
+ def test_attributes_method_specifying_some_keys
99
44
  user = User.new
100
- user_serializer = DefaultUserSerializer.new(user, {})
45
+ user_serializer = UserAttributesWithSomeKeySerializer.new(user, :scope => {})
101
46
 
102
47
  hash = user_serializer.as_json
103
48
 
104
49
  assert_equal({
105
- :default_user => { :first_name => "Jose", :last_name => "Valim" }
50
+ :user_attributes_with_some_key => { :first_name => "Jose", :l_name => "Valim", :ok => true }
106
51
  }, hash)
107
52
  end
108
53
 
109
- def test_attributes_method
110
- user = User.new
111
- user_serializer = UserSerializer.new(user, :scope => {})
54
+ def test_attribute_method_with_name_as_serializer_prefix
55
+ object = SomeObject.new("something")
56
+ object_serializer = SomeSerializer.new(object, {})
112
57
 
113
- hash = user_serializer.as_json
58
+ hash = object_serializer.as_json
114
59
 
115
60
  assert_equal({
116
- :user => { :first_name => "Jose", :last_name => "Valim", :ok => true }
61
+ :some => { :some => "something" }
117
62
  }, hash)
118
63
  end
119
64
 
@@ -159,11 +104,6 @@ class SerializerTest < ActiveModel::TestCase
159
104
  }, hash)
160
105
  end
161
106
 
162
- class PostSerializer < ActiveModel::Serializer
163
- attributes :title, :body
164
- has_many :comments, :serializer => CommentSerializer
165
- end
166
-
167
107
  def test_has_many
168
108
  user = User.new
169
109
 
@@ -185,16 +125,6 @@ class SerializerTest < ActiveModel::TestCase
185
125
  }, post_serializer.as_json)
186
126
  end
187
127
 
188
- class PostWithConditionalCommentsSerializer < ActiveModel::Serializer
189
- root :post
190
- attributes :title, :body
191
- has_many :comments, :serializer => CommentSerializer
192
-
193
- def include_associations!
194
- include! :comments unless object.comments_disabled
195
- end
196
- end
197
-
198
128
  def test_conditionally_included_associations
199
129
  user = User.new
200
130
 
@@ -227,20 +157,6 @@ class SerializerTest < ActiveModel::TestCase
227
157
  }, post_serializer.as_json)
228
158
  end
229
159
 
230
- class PostWithMultipleConditionalsSerializer < ActiveModel::Serializer
231
- root :post
232
- attributes :title, :body, :author
233
- has_many :comments, :serializer => CommentSerializer
234
-
235
- def include_comments?
236
- !object.comments_disabled
237
- end
238
-
239
- def include_author?
240
- scope.super_user?
241
- end
242
- end
243
-
244
160
  def test_conditionally_included_associations_and_attributes
245
161
  user = User.new
246
162
 
@@ -283,18 +199,6 @@ class SerializerTest < ActiveModel::TestCase
283
199
  }, post_serializer.as_json)
284
200
  end
285
201
 
286
- class Blog < Model
287
- attr_accessor :author
288
- end
289
-
290
- class AuthorSerializer < ActiveModel::Serializer
291
- attributes :first_name, :last_name
292
- end
293
-
294
- class BlogSerializer < ActiveModel::Serializer
295
- has_one :author, :serializer => AuthorSerializer
296
- end
297
-
298
202
  def test_has_one
299
203
  user = User.new
300
204
  blog = Blog.new
@@ -372,6 +276,28 @@ class SerializerTest < ActiveModel::TestCase
372
276
  assert_equal({ :my_blog => { :author => nil } }, serializer.new(blog, :scope => user).as_json)
373
277
  end
374
278
 
279
+ def test_nil_root_object
280
+ user = User.new
281
+ blog = nil
282
+
283
+ serializer = Class.new(BlogSerializer) do
284
+ root false
285
+ end
286
+
287
+ assert_equal(nil, serializer.new(blog, :scope => user).as_json)
288
+ end
289
+
290
+ def test_custom_root_with_nil_root_object
291
+ user = User.new
292
+ blog = nil
293
+
294
+ serializer = Class.new(BlogSerializer) do
295
+ root :my_blog
296
+ end
297
+
298
+ assert_equal({ :my_blog => nil }, serializer.new(blog, :scope => user).as_json)
299
+ end
300
+
375
301
  def test_false_root
376
302
  user = User.new
377
303
  blog = Blog.new
@@ -380,13 +306,35 @@ class SerializerTest < ActiveModel::TestCase
380
306
  root false
381
307
  end
382
308
 
309
+ another_serializer = Class.new(BlogSerializer) do
310
+ self.root = false
311
+ end
312
+
383
313
  assert_equal({ :author => nil }, serializer.new(blog, :scope => user).as_json)
314
+ assert_equal({ :author => nil }, another_serializer.new(blog, :scope => user).as_json)
384
315
 
385
316
  # test inherited false root
386
317
  serializer = Class.new(serializer)
387
318
  assert_equal({ :author => nil }, serializer.new(blog, :scope => user).as_json)
388
319
  end
389
320
 
321
+ def test_root_false_on_load_active_model_serializers
322
+ begin
323
+ ActiveSupport.on_load(:active_model_serializers) do
324
+ self.root = false
325
+ end
326
+
327
+ blog = Blog.new
328
+ serializer = BlogSerializer.new(blog)
329
+
330
+ assert_equal({ :author => nil }, serializer.as_json)
331
+ ensure
332
+ ActiveSupport.on_load(:active_model_serializers) do
333
+ self.root = nil
334
+ end
335
+ end
336
+ end
337
+
390
338
  def test_embed_ids
391
339
  serializer = post_serializer
392
340
 
@@ -405,8 +353,8 @@ class SerializerTest < ActiveModel::TestCase
405
353
  :post => {
406
354
  :title => "New Post",
407
355
  :body => "Body of new post",
408
- :comments => [1, 2],
409
- :author => nil
356
+ :comment_ids => [1, 2],
357
+ :author_id => nil
410
358
  }
411
359
  }, serializer.as_json)
412
360
  end
@@ -429,8 +377,8 @@ class SerializerTest < ActiveModel::TestCase
429
377
  :post => {
430
378
  :title => "New Post",
431
379
  :body => "Body of new post",
432
- :comments => [1, 2],
433
- :author => nil
380
+ :comment_ids => [1, 2],
381
+ :author_id => nil
434
382
  },
435
383
  :comments => [
436
384
  { :title => "Comment1" },
@@ -447,8 +395,8 @@ class SerializerTest < ActiveModel::TestCase
447
395
  :post => {
448
396
  :title => "New Post",
449
397
  :body => "Body of new post",
450
- :comments => [1, 2],
451
- :author => 1
398
+ :comment_ids => [1, 2],
399
+ :author_id => 1
452
400
  },
453
401
  :comments => [
454
402
  { :title => "Comment1" },
@@ -485,60 +433,6 @@ class SerializerTest < ActiveModel::TestCase
485
433
  }, serializer.as_json)
486
434
  end
487
435
 
488
- # serialize different typed objects
489
- def test_array_serializer
490
- model = Model.new
491
- user = User.new
492
- comments = Comment.new(:title => "Comment1", :id => 1)
493
-
494
- array = [model, user, comments]
495
- serializer = array.active_model_serializer.new(array, :scope => {:scope => true})
496
- assert_equal([
497
- { :model => "Model" },
498
- { :last_name => "Valim", :ok => true, :first_name => "Jose", :scope => true },
499
- { :title => "Comment1" }
500
- ], serializer.as_json)
501
- end
502
-
503
- def test_array_serializer_with_root
504
- comment1 = Comment.new(:title => "Comment1", :id => 1)
505
- comment2 = Comment.new(:title => "Comment2", :id => 2)
506
-
507
- array = [ comment1, comment2 ]
508
-
509
- serializer = array.active_model_serializer.new(array, :root => :comments)
510
-
511
- assert_equal({ :comments => [
512
- { :title => "Comment1" },
513
- { :title => "Comment2" }
514
- ]}, serializer.as_json)
515
- end
516
-
517
- def test_array_serializer_with_hash
518
- hash = {:value => "something"}
519
- array = [hash]
520
- serializer = array.active_model_serializer.new(array, :root => :items)
521
- assert_equal({ :items => [ hash.as_json ]}, serializer.as_json)
522
- end
523
-
524
- class CustomPostSerializer < ActiveModel::Serializer
525
- attributes :title
526
- end
527
-
528
- def test_array_serializer_with_specified_seriailizer
529
- post1 = Post.new(:title => "Post1", :author => "Author1", :id => 1)
530
- post2 = Post.new(:title => "Post2", :author => "Author2", :id => 2)
531
-
532
- array = [ post1, post2 ]
533
-
534
- serializer = array.active_model_serializer.new array, :each_serializer => CustomPostSerializer
535
-
536
- assert_equal([
537
- { :title => "Post1" },
538
- { :title => "Post2" }
539
- ], serializer.as_json)
540
- end
541
-
542
436
  def test_sets_can_be_serialized
543
437
  post1 = Post.new(:title => "Post1", :author => "Author1", :id => 1)
544
438
  post2 = Post.new(:title => "Post2", :author => "Author2", :id => 2)
@@ -555,18 +449,9 @@ class SerializerTest < ActiveModel::TestCase
555
449
  assert as_json.include?({ :title => "Post2" })
556
450
  end
557
451
 
558
- class CustomBlog < Blog
559
- attr_accessor :public_posts, :public_user
560
- end
561
-
562
- class CustomBlogSerializer < ActiveModel::Serializer
563
- has_many :public_posts, :key => :posts, :serializer => PostSerializer
564
- has_one :public_user, :key => :user, :serializer => UserSerializer
565
- end
566
-
567
452
  def test_associations_with_as
568
453
  posts = [
569
- Post.new(:title => 'First Post', :body => 'text'),
454
+ Post.new(:title => 'First Post', :body => 'text'),
570
455
  Post.new(:title => 'Second Post', :body => 'text')
571
456
  ]
572
457
  user = User.new
@@ -584,15 +469,15 @@ class SerializerTest < ActiveModel::TestCase
584
469
  {:title => 'Second Post', :body => 'text', :comments => []}
585
470
  ],
586
471
  :user => {
587
- :first_name => "Jose",
588
- :last_name => "Valim", :ok => true,
472
+ :first_name => "Jose",
473
+ :last_name => "Valim", :ok => true,
589
474
  :scope => true
590
475
  }
591
476
  }
592
477
  }, serializer.as_json)
593
478
  end
594
479
 
595
- def test_implicity_detection_for_association_serializers
480
+ def test_implicity_detection_for_association_serializers
596
481
  implicit_serializer = Class.new(ActiveModel::Serializer) do
597
482
  root :custom_blog
598
483
  const_set(:UserSerializer, UserSerializer)
@@ -603,7 +488,7 @@ class SerializerTest < ActiveModel::TestCase
603
488
  end
604
489
 
605
490
  posts = [
606
- Post.new(:title => 'First Post', :body => 'text', :comments => []),
491
+ Post.new(:title => 'First Post', :body => 'text', :comments => []),
607
492
  Post.new(:title => 'Second Post', :body => 'text', :comments => [])
608
493
  ]
609
494
  user = User.new
@@ -621,8 +506,8 @@ class SerializerTest < ActiveModel::TestCase
621
506
  {:title => 'Second Post', :body => 'text', :comments => []}
622
507
  ],
623
508
  :user => {
624
- :first_name => "Jose",
625
- :last_name => "Valim", :ok => true,
509
+ :first_name => "Jose",
510
+ :last_name => "Valim", :ok => true,
626
511
  :scope => true
627
512
  }
628
513
  }
@@ -676,15 +561,21 @@ class SerializerTest < ActiveModel::TestCase
676
561
  define_method(:model_class) do model end
677
562
  end
678
563
 
679
- attributes :name, :age
564
+ # Computed attributes (not real columns or associations).
565
+ def can_edit; end
566
+ def drafts; end
567
+
568
+ attributes :name, :age, :can_edit
680
569
  has_many :posts, :serializer => Class.new
570
+ has_many :drafts, :serializer => Class.new
681
571
  has_one :parent, :serializer => Class.new
682
572
  end
683
573
 
684
574
  assert_equal serializer.schema, {
685
- :attributes => { :name => :string, :age => :integer },
575
+ :attributes => { :name => :string, :age => :integer, :can_edit => nil },
686
576
  :associations => {
687
577
  :posts => { :has_many => :posts },
578
+ :drafts => nil,
688
579
  :parent => { :belongs_to => :parent }
689
580
  }
690
581
  }
@@ -739,7 +630,7 @@ class SerializerTest < ActiveModel::TestCase
739
630
  :post => {
740
631
  :title => "New Post",
741
632
  :body => "It's a new post!",
742
- :author => 5
633
+ :author_id => 5
743
634
  }
744
635
  }, hash.as_json)
745
636
  end
@@ -860,7 +751,7 @@ class SerializerTest < ActiveModel::TestCase
860
751
  expected = serializer_class.new(post).as_json
861
752
  assert_equal expected, hash_object
862
753
  end
863
-
754
+
864
755
  def test_embed_ids_include_true_with_root
865
756
  serializer_class = post_serializer
866
757
 
@@ -909,7 +800,7 @@ class SerializerTest < ActiveModel::TestCase
909
800
  :author => [{ :first_name => "Jose", :last_name => "Valim" }]
910
801
  }, serializer.as_json)
911
802
  end
912
-
803
+
913
804
  # the point of this test is to illustrate that deeply nested serializers
914
805
  # still side-load at the root.
915
806
  def test_embed_with_include_inserts_at_root
@@ -957,12 +848,12 @@ class SerializerTest < ActiveModel::TestCase
957
848
  actual = ActiveModel::ArraySerializer.new([post], :root => :posts).as_json
958
849
  assert_equal({
959
850
  :posts => [
960
- { :title => "New Post", :body => "NEW POST", :id => 1, :comments => [1,2] }
851
+ { :title => "New Post", :body => "NEW POST", :id => 1, :comment_ids => [1,2] }
961
852
  ],
962
853
 
963
854
  :comments => [
964
- { :body => "EWOT", :id => 1, :tags => [1,3] },
965
- { :body => "YARLY", :id => 2, :tags => [1,2] }
855
+ { :body => "EWOT", :id => 1, :tag_ids => [1,3] },
856
+ { :body => "YARLY", :id => 2, :tag_ids => [1,2] }
966
857
  ],
967
858
 
968
859
  :tags => [
@@ -1079,21 +970,6 @@ class SerializerTest < ActiveModel::TestCase
1079
970
  }, actual)
1080
971
  end
1081
972
 
1082
- # Set up some classes for polymorphic testing
1083
- class Attachment < Model
1084
- def attachable
1085
- @attributes[:attachable]
1086
- end
1087
-
1088
- def readable
1089
- @attributes[:readable]
1090
- end
1091
-
1092
- def edible
1093
- @attributes[:edible]
1094
- end
1095
- end
1096
-
1097
973
  def tests_can_handle_polymorphism
1098
974
  email_serializer = Class.new(ActiveModel::Serializer) do
1099
975
  attributes :subject, :body
@@ -1121,7 +997,7 @@ class SerializerTest < ActiveModel::TestCase
1121
997
  actual = attachment_serializer.new(attachment, {}).as_json
1122
998
 
1123
999
  assert_equal({
1124
- :name => 'logo.png',
1000
+ :name => 'logo.png',
1125
1001
  :url => 'http://example.com/logo.png',
1126
1002
  :attachable => {
1127
1003
  :type => :email,
@@ -1158,7 +1034,7 @@ class SerializerTest < ActiveModel::TestCase
1158
1034
  actual = attachment_serializer.new(attachment, {}).as_json
1159
1035
 
1160
1036
  assert_equal({
1161
- :name => 'logo.png',
1037
+ :name => 'logo.png',
1162
1038
  :url => 'http://example.com/logo.png',
1163
1039
  :attachable => {
1164
1040
  :type => :email,
@@ -1197,7 +1073,7 @@ class SerializerTest < ActiveModel::TestCase
1197
1073
 
1198
1074
  assert_equal({
1199
1075
  :attachment => {
1200
- :name => 'logo.png',
1076
+ :name => 'logo.png',
1201
1077
  :url => 'http://example.com/logo.png',
1202
1078
  :attachable => {
1203
1079
  :type => :email,
@@ -1388,4 +1264,50 @@ class SerializerTest < ActiveModel::TestCase
1388
1264
  }
1389
1265
  }, actual)
1390
1266
  end
1267
+
1268
+ def test_meta_key_serialization
1269
+ tag_serializer = Class.new(ActiveModel::Serializer) do
1270
+ attributes :name
1271
+ end
1272
+
1273
+ tag_class = Class.new(Model) do
1274
+ def name
1275
+ @attributes[:name]
1276
+ end
1277
+
1278
+ define_method :active_model_serializer do
1279
+ tag_serializer
1280
+ end
1281
+ end
1282
+
1283
+ serializable_array = Class.new(Array)
1284
+
1285
+ array = serializable_array.new
1286
+ array << tag_class.new(:name => 'Rails')
1287
+ array << tag_class.new(:name => 'Sinatra')
1288
+
1289
+ actual = array.active_model_serializer.new(array, :root => :tags, :meta => {:total => 10}).as_json
1290
+
1291
+ assert_equal({
1292
+ :meta => {
1293
+ :total => 10,
1294
+ },
1295
+ :tags => [
1296
+ { :name => "Rails" },
1297
+ { :name => "Sinatra" },
1298
+ ]
1299
+ }, actual)
1300
+
1301
+ actual = array.active_model_serializer.new(array, :root => :tags, :meta => {:total => 10}, :meta_key => 'meta_object').as_json
1302
+
1303
+ assert_equal({
1304
+ :meta_object => {
1305
+ :total => 10,
1306
+ },
1307
+ :tags => [
1308
+ { :name => "Rails" },
1309
+ { :name => "Sinatra" },
1310
+ ]
1311
+ }, actual)
1312
+ end
1391
1313
  end