active_model_serializers 0.8.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +122 -5
  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 +396 -95
  7. data/lib/action_controller/serialization.rb +50 -12
  8. data/lib/action_controller/serialization_test_case.rb +79 -0
  9. data/lib/active_model/array_serializer.rb +47 -78
  10. data/lib/active_model/default_serializer.rb +32 -0
  11. data/lib/active_model/serializable/utils.rb +16 -0
  12. data/lib/active_model/serializable.rb +62 -0
  13. data/lib/active_model/serializer/association/has_many.rb +39 -0
  14. data/lib/active_model/serializer/association/has_one.rb +25 -0
  15. data/lib/active_model/serializer/association.rb +58 -0
  16. data/lib/active_model/serializer/config.rb +31 -0
  17. data/lib/active_model/serializer/generators/resource_override.rb +13 -0
  18. data/lib/{generators → active_model/serializer/generators}/serializer/USAGE +0 -0
  19. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +14 -0
  20. data/lib/active_model/serializer/generators/serializer/serializer_generator.rb +37 -0
  21. data/lib/active_model/serializer/generators/serializer/templates/controller.rb +93 -0
  22. data/lib/active_model/serializer/generators/serializer/templates/serializer.rb +8 -0
  23. data/lib/active_model/serializer/railtie.rb +18 -0
  24. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  25. data/lib/active_model/serializer.rb +214 -423
  26. data/lib/active_model/serializer_support.rb +5 -0
  27. data/lib/active_model_serializers/mime_types.rb +14 -0
  28. data/lib/active_model_serializers.rb +12 -87
  29. data/test/fixtures/active_record.rb +96 -0
  30. data/test/fixtures/poro.rb +187 -0
  31. data/test/fixtures/template.html.erb +1 -0
  32. data/test/integration/action_controller/namespaced_serialization_test.rb +105 -0
  33. data/test/integration/action_controller/serialization_test.rb +287 -0
  34. data/test/integration/action_controller/serialization_test_case_test.rb +71 -0
  35. data/test/integration/active_record/active_record_test.rb +94 -0
  36. data/test/integration/generators/resource_generator_test.rb +26 -0
  37. data/test/integration/generators/scaffold_controller_generator_test.rb +64 -0
  38. data/test/integration/generators/serializer_generator_test.rb +41 -0
  39. data/test/test_app.rb +14 -0
  40. data/test/test_helper.rb +10 -18
  41. data/test/unit/active_model/array_serializer/except_test.rb +18 -0
  42. data/test/unit/active_model/array_serializer/key_format_test.rb +18 -0
  43. data/test/unit/active_model/array_serializer/meta_test.rb +53 -0
  44. data/test/unit/active_model/array_serializer/only_test.rb +18 -0
  45. data/test/unit/active_model/array_serializer/options_test.rb +16 -0
  46. data/test/unit/active_model/array_serializer/root_test.rb +102 -0
  47. data/test/unit/active_model/array_serializer/scope_test.rb +24 -0
  48. data/test/unit/active_model/array_serializer/serialization_test.rb +216 -0
  49. data/test/unit/active_model/default_serializer_test.rb +13 -0
  50. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +36 -0
  51. data/test/unit/active_model/serializer/associations_test.rb +19 -0
  52. data/test/unit/active_model/serializer/attributes_test.rb +57 -0
  53. data/test/unit/active_model/serializer/config_test.rb +91 -0
  54. data/test/unit/active_model/serializer/filter_test.rb +69 -0
  55. data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +189 -0
  56. data/test/unit/active_model/serializer/has_many_test.rb +265 -0
  57. data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +27 -0
  58. data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +196 -0
  59. data/test/unit/active_model/serializer/has_one_test.rb +253 -0
  60. data/test/unit/active_model/serializer/key_format_test.rb +25 -0
  61. data/test/unit/active_model/serializer/meta_test.rb +39 -0
  62. data/test/unit/active_model/serializer/options_test.rb +42 -0
  63. data/test/unit/active_model/serializer/root_test.rb +117 -0
  64. data/test/unit/active_model/serializer/scope_test.rb +49 -0
  65. data/test/unit/active_model/serializer/url_helpers_test.rb +35 -0
  66. metadata +107 -64
  67. data/.gitignore +0 -18
  68. data/.travis.yml +0 -28
  69. data/Gemfile +0 -4
  70. data/Gemfile.edge +0 -9
  71. data/Rakefile +0 -18
  72. data/active_model_serializers.gemspec +0 -24
  73. data/bench/perf.rb +0 -43
  74. data/cruft.md +0 -19
  75. data/lib/active_model/serializer/associations.rb +0 -233
  76. data/lib/active_record/serializer_override.rb +0 -16
  77. data/lib/generators/resource_override.rb +0 -13
  78. data/lib/generators/serializer/serializer_generator.rb +0 -42
  79. data/lib/generators/serializer/templates/serializer.rb +0 -19
  80. data/test/array_serializer_test.rb +0 -75
  81. data/test/association_test.rb +0 -592
  82. data/test/caching_test.rb +0 -96
  83. data/test/generators_test.rb +0 -85
  84. data/test/no_serialization_scope_test.rb +0 -34
  85. data/test/serialization_scope_name_test.rb +0 -67
  86. data/test/serialization_test.rb +0 -392
  87. data/test/serializer_support_test.rb +0 -51
  88. data/test/serializer_test.rb +0 -1465
  89. data/test/test_fakes.rb +0 -217
@@ -0,0 +1,69 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class FilterOptionsTest < Minitest::Test
6
+ def setup
7
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
8
+ end
9
+
10
+ def test_only_option
11
+ @profile_serializer = ProfileSerializer.new(@profile, only: :name)
12
+ assert_equal({
13
+ 'profile' => { name: 'Name 1' }
14
+ }, @profile_serializer.as_json)
15
+ end
16
+
17
+ def test_except_option
18
+ @profile_serializer = ProfileSerializer.new(@profile, except: :comments)
19
+ assert_equal({
20
+ 'profile' => { name: 'Name 1', description: 'Description 1' }
21
+ }, @profile_serializer.as_json)
22
+ end
23
+ end
24
+
25
+ class FilterAttributesTest < Minitest::Test
26
+ def setup
27
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
28
+ @profile_serializer = ProfileSerializer.new(@profile)
29
+ @profile_serializer.instance_eval do
30
+ def filter(keys)
31
+ keys - [:description]
32
+ end
33
+ end
34
+ end
35
+
36
+ def test_filtered_attributes_serialization
37
+ assert_equal({
38
+ 'profile' => { name: 'Name 1' }
39
+ }, @profile_serializer.as_json)
40
+ end
41
+ end
42
+
43
+ class FilterAssociationsTest < Minitest::Test
44
+ def setup
45
+ @association = PostSerializer._associations[:comments]
46
+ @old_association = @association.dup
47
+ @association.embed = :ids
48
+ @association.embed_in_root = true
49
+ @post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
50
+ @post_serializer = PostSerializer.new(@post)
51
+ @post_serializer.instance_eval do
52
+ def filter(keys)
53
+ keys - [:body, :comments]
54
+ end
55
+ end
56
+ end
57
+
58
+ def teardown
59
+ PostSerializer._associations[:comments] = @old_association
60
+ end
61
+
62
+ def test_filtered_associations_serialization
63
+ assert_equal({
64
+ 'post' => { title: 'Title 1' }
65
+ }, @post_serializer.as_json)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,189 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class HasManyPolymorphicTest < ActiveModel::TestCase
6
+ def setup
7
+ @association = MailSerializer._associations[:attachments]
8
+ @old_association = @association.dup
9
+
10
+ @mail = Mail.new({ body: 'Body 1' })
11
+ @mail_serializer = MailSerializer.new(@mail)
12
+ end
13
+
14
+ def teardown
15
+ MailSerializer._associations[:attachments] = @old_association
16
+ end
17
+
18
+ def model_name(object)
19
+ object.class.to_s.demodulize.underscore.to_sym
20
+ end
21
+
22
+ def test_associations_definition
23
+ assert_equal 1, MailSerializer._associations.length
24
+ assert_kind_of Association::HasMany, @association
25
+ assert_equal true, @association.polymorphic
26
+ assert_equal 'attachments', @association.name
27
+ end
28
+
29
+ def test_associations_embedding_ids_serialization_using_serializable_hash
30
+ @association.embed = :ids
31
+
32
+ assert_equal({
33
+ body: 'Body 1',
34
+ 'attachment_ids' => @mail.attachments.map do |c|
35
+ { id: c.object_id, type: model_name(c) }
36
+ end
37
+ }, @mail_serializer.serializable_hash)
38
+ end
39
+
40
+ def test_associations_embedding_ids_serialization_using_as_json
41
+ @association.embed = :ids
42
+
43
+ assert_equal({
44
+ 'mail' => {
45
+ :body => 'Body 1',
46
+ 'attachment_ids' => @mail.attachments.map do |c|
47
+ { id: c.object_id, type: model_name(c) }
48
+ end
49
+ }
50
+ }, @mail_serializer.as_json)
51
+ end
52
+
53
+ def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
54
+ @association.embed = :ids
55
+ @association.key = 'key'
56
+
57
+ assert_equal({
58
+ body: 'Body 1',
59
+ 'key' => @mail.attachments.map do |c|
60
+ { id: c.object_id, type: model_name(c) }
61
+ end
62
+ }, @mail_serializer.serializable_hash)
63
+ end
64
+
65
+ def test_associations_embedding_objects_serialization_using_serializable_hash
66
+ @association.embed = :objects
67
+
68
+ assert_equal({
69
+ body: 'Body 1',
70
+ :attachments => [
71
+ { type: :image, image: { url: 'U1' }},
72
+ { type: :video, video: { html: 'H1' }}
73
+ ]
74
+ }, @mail_serializer.serializable_hash)
75
+ end
76
+
77
+ def test_associations_embedding_objects_serialization_using_as_json
78
+ @association.embed = :objects
79
+
80
+ assert_equal({
81
+ 'mail' => {
82
+ body: 'Body 1',
83
+ attachments: [
84
+ { type: :image, image: { url: 'U1' }},
85
+ { type: :video, video: { html: 'H1' }}
86
+ ]
87
+ }
88
+ }, @mail_serializer.as_json)
89
+ end
90
+
91
+ def test_associations_embedding_nil_objects_serialization_using_as_json
92
+ @association.embed = :objects
93
+ @mail.instance_eval do
94
+ def attachments
95
+ [nil]
96
+ end
97
+ end
98
+
99
+ assert_equal({
100
+ 'mail' => {
101
+ :body => 'Body 1',
102
+ :attachments => [nil]
103
+ }
104
+ }, @mail_serializer.as_json)
105
+ end
106
+
107
+ def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
108
+ @association.embed = :objects
109
+ @association.embedded_key = 'root'
110
+
111
+ assert_equal({
112
+ body: 'Body 1',
113
+ 'root' => [
114
+ { type: :image, image: { url: 'U1' }},
115
+ { type: :video, video: { html: 'H1' }}
116
+ ]
117
+ }, @mail_serializer.serializable_hash)
118
+ end
119
+
120
+ def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
121
+ @association.embed = :ids
122
+ @association.embed_in_root = true
123
+
124
+ assert_equal({
125
+ body: 'Body 1',
126
+ 'attachment_ids' => @mail.attachments.map do |c|
127
+ { id: c.object_id, type: model_name(c) }
128
+ end
129
+ }, @mail_serializer.serializable_hash)
130
+ end
131
+
132
+ def test_associations_embedding_ids_including_objects_serialization_using_as_json
133
+ @association.embed = :ids
134
+ @association.embed_in_root = true
135
+
136
+ assert_equal({
137
+ 'mail' => {
138
+ body: 'Body 1',
139
+ 'attachment_ids' => @mail.attachments.map do |c|
140
+ { id: c.object_id, type: model_name(c) }
141
+ end,
142
+ },
143
+ 'attachments' => [
144
+ { type: :image, image: { url: 'U1' }},
145
+ { type: :video, video: { html: 'H1' }}
146
+ ]
147
+ }, @mail_serializer.as_json)
148
+ end
149
+
150
+ def test_associations_embedding_nothing_including_objects_serialization_using_as_json
151
+ @association.embed = nil
152
+ @association.embed_in_root = true
153
+
154
+ assert_equal({
155
+ 'mail' => { body: 'Body 1' },
156
+ 'attachments' => [
157
+ { type: :image, image: { url: 'U1' }},
158
+ { type: :video, video: { html: 'H1' }}
159
+ ]
160
+ }, @mail_serializer.as_json)
161
+ end
162
+
163
+ def test_associations_using_a_given_serializer
164
+ @association.embed = :ids
165
+ @association.embed_in_root = true
166
+ @association.serializer_from_options = Class.new(ActiveModel::Serializer) do
167
+ def fake
168
+ 'fake'
169
+ end
170
+
171
+ attributes :fake
172
+ end
173
+
174
+ assert_equal({
175
+ 'mail' => {
176
+ body: 'Body 1',
177
+ 'attachment_ids' => @mail.attachments.map do |c|
178
+ { id: c.object_id, type: model_name(c) }
179
+ end
180
+ },
181
+ 'attachments' => [
182
+ { type: :image, image: { fake: 'fake' }},
183
+ { type: :video, video: { fake: 'fake' }}
184
+ ]
185
+ }, @mail_serializer.as_json)
186
+ end
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,265 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class HasManyTest < Minitest::Test
6
+ def setup
7
+ @association = PostSerializer._associations[:comments]
8
+ @old_association = @association.dup
9
+
10
+ @post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
11
+ @post_serializer = PostSerializer.new(@post)
12
+ end
13
+
14
+ def teardown
15
+ PostSerializer._associations[:comments] = @old_association
16
+ end
17
+
18
+ def test_associations_definition
19
+ assert_equal 1, PostSerializer._associations.length
20
+ assert_kind_of Association::HasMany, @association
21
+ assert_equal 'comments', @association.name
22
+ end
23
+
24
+ def test_associations_inheritance
25
+ inherited_serializer_klass = Class.new(PostSerializer) do
26
+ has_many :some_associations
27
+ end
28
+ another_inherited_serializer_klass = Class.new(PostSerializer)
29
+
30
+ assert_equal(PostSerializer._associations.length + 1,
31
+ inherited_serializer_klass._associations.length)
32
+ assert_equal(PostSerializer._associations.length,
33
+ another_inherited_serializer_klass._associations.length)
34
+ end
35
+
36
+ def test_associations_embedding_ids_serialization_using_serializable_hash
37
+ @association.embed = :ids
38
+
39
+ assert_equal({
40
+ title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
41
+ }, @post_serializer.serializable_hash)
42
+ end
43
+
44
+ def test_associations_embedding_ids_serialization_using_as_json
45
+ @association.embed = :ids
46
+
47
+ assert_equal({
48
+ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }
49
+ }, @post_serializer.as_json)
50
+ end
51
+
52
+ def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
53
+ @association.embed = :ids
54
+ @association.key = 'key'
55
+
56
+ assert_equal({
57
+ title: 'Title 1', body: 'Body 1', 'key' => @post.comments.map { |c| c.object_id }
58
+ }, @post_serializer.serializable_hash)
59
+ end
60
+
61
+ def test_associations_embedding_objects_serialization_using_serializable_hash
62
+ @association.embed = :objects
63
+
64
+ assert_equal({
65
+ title: 'Title 1', body: 'Body 1', comments: [{ content: 'C1' }, { content: 'C2' }]
66
+ }, @post_serializer.serializable_hash)
67
+ end
68
+
69
+ def test_associations_embedding_objects_serialization_using_as_json
70
+ @association.embed = :objects
71
+
72
+ assert_equal({
73
+ 'post' => { title: 'Title 1', body: 'Body 1', comments: [{ content: 'C1' }, { content: 'C2' }] }
74
+ }, @post_serializer.as_json)
75
+ end
76
+
77
+ def test_associations_embedding_nil_objects_serialization_using_as_json
78
+ @association.embed = :objects
79
+ @post.instance_eval do
80
+ def comments
81
+ [nil]
82
+ end
83
+ end
84
+
85
+ assert_equal({
86
+ 'post' => { title: 'Title 1', body: 'Body 1', comments: [nil] }
87
+ }, @post_serializer.as_json)
88
+ end
89
+
90
+ def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
91
+ @association.embed = :objects
92
+ @association.embedded_key = 'root'
93
+
94
+ assert_equal({
95
+ title: 'Title 1', body: 'Body 1', 'root' => [{ content: 'C1' }, { content: 'C2' }]
96
+ }, @post_serializer.serializable_hash)
97
+ end
98
+
99
+ def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
100
+ @association.embed = :ids
101
+ @association.embed_in_root = true
102
+
103
+ assert_equal({
104
+ title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
105
+ }, @post_serializer.serializable_hash)
106
+ end
107
+
108
+ def test_associations_embedding_ids_including_objects_serialization_using_as_json
109
+ @association.embed = :ids
110
+ @association.embed_in_root = true
111
+
112
+ assert_equal({
113
+ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
114
+ 'comments' => [{ content: 'C1' }, { content: 'C2' }]
115
+ }, @post_serializer.as_json)
116
+ end
117
+
118
+ def test_associations_embedding_ids_including_objects_serialization_when_invoked_from_parent_serializer
119
+ @association.embed = :ids
120
+ @association.embed_in_root = true
121
+
122
+ category = Category.new(name: 'Name 1')
123
+ category.instance_variable_set(:@posts, [@post])
124
+ category_serializer = CategorySerializer.new(category)
125
+
126
+ assert_equal({
127
+ 'category' => {
128
+ name: 'Name 1',
129
+ posts: [{ title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }]
130
+ },
131
+ "comments" => [{ content: 'C1' }, { content: 'C2' }]
132
+ }, category_serializer.as_json)
133
+ end
134
+
135
+ def test_associations_embedding_nothing_including_objects_serialization_using_as_json
136
+ @association.embed = nil
137
+ @association.embed_in_root = true
138
+
139
+ assert_equal({
140
+ 'post' => { title: 'Title 1', body: 'Body 1' },
141
+ 'comments' => [{ content: 'C1' }, { content: 'C2' }]
142
+ }, @post_serializer.as_json)
143
+ end
144
+
145
+ def test_associations_using_a_given_serializer
146
+ @association.embed = :ids
147
+ @association.embed_in_root = true
148
+ @association.serializer_from_options = Class.new(Serializer) do
149
+ def content
150
+ object.read_attribute_for_serialization(:content) + '!'
151
+ end
152
+
153
+ attributes :content
154
+ end
155
+
156
+ assert_equal({
157
+ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
158
+ 'comments' => [{ content: 'C1!' }, { content: 'C2!' }]
159
+ }, @post_serializer.as_json)
160
+ end
161
+
162
+ def test_associations_embedding_ids_using_a_given_array_serializer
163
+ @association.embed = :ids
164
+ @association.embed_in_root = true
165
+ @association.serializer_from_options = Class.new(ArraySerializer) do
166
+ def serializable_object
167
+ { my_content: ['fake'] }
168
+ end
169
+ end
170
+
171
+ assert_equal({
172
+ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
173
+ 'comments' => { my_content: ['fake'] }
174
+ }, @post_serializer.as_json)
175
+ end
176
+
177
+ def test_associations_embedding_objects_using_a_given_array_serializer
178
+ @association.serializer_from_options = Class.new(ArraySerializer) do
179
+ def serializable_object(options={})
180
+ { my_content: ['fake'] }
181
+ end
182
+ end
183
+
184
+ assert_equal({
185
+ 'post' => { title: 'Title 1', body: 'Body 1', comments: { my_content: ['fake'] } }
186
+ }, @post_serializer.as_json)
187
+ end
188
+
189
+ def test_associations_embedding_ids_including_objects_serialization_with_embed_in_root_key
190
+ @association.embed_in_root = true
191
+ @association.embed_in_root_key = :linked
192
+ @association.embed = :ids
193
+ assert_equal({
194
+ 'post' => {
195
+ title: 'Title 1', body: 'Body 1',
196
+ 'comment_ids' => @post.comments.map(&:object_id)
197
+ },
198
+ linked: {
199
+ 'comments' => [
200
+ { content: 'C1' },
201
+ { content: 'C2' }
202
+ ]
203
+ },
204
+ }, @post_serializer.as_json)
205
+ end
206
+
207
+ def test_associations_embedding_ids_using_embed_namespace_including_object_serialization_with_embed_in_root_key
208
+ @association.embed_in_root = true
209
+ @association.embed_in_root_key = :linked
210
+ @association.embed = :ids
211
+ @association.embed_namespace = :links
212
+ @association.key = :comments
213
+ assert_equal({
214
+ 'post' => {
215
+ title: 'Title 1', body: 'Body 1',
216
+ links: {
217
+ comments: @post.comments.map(&:object_id)
218
+ }
219
+ },
220
+ linked: {
221
+ 'comments' => [
222
+ { content: 'C1' },
223
+ { content: 'C2' }
224
+ ]
225
+ },
226
+ }, @post_serializer.as_json)
227
+ end
228
+
229
+ def test_associations_embedding_objects_using_embed_namespace
230
+ @association.embed = :objects
231
+ @association.embed_namespace = :links
232
+
233
+ assert_equal({
234
+ 'post' => {
235
+ title: 'Title 1', body: 'Body 1',
236
+ links: {
237
+ comments: [
238
+ { content: 'C1' },
239
+ { content: 'C2' }
240
+ ]
241
+ }
242
+ }
243
+ }, @post_serializer.as_json)
244
+ end
245
+
246
+ def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
247
+ @association = NameKeyPostSerializer._associations[:comments]
248
+ @association.embed = :ids
249
+
250
+ assert_equal({
251
+ title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id }
252
+ }, NameKeyPostSerializer.new(@post).serializable_hash)
253
+ end
254
+
255
+ def test_associations_name_key_embedding_ids_serialization_using_as_json
256
+ @association = NameKeyPostSerializer._associations[:comments]
257
+ @association.embed = :ids
258
+
259
+ assert_equal({
260
+ 'name_key_post' => { title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id } }
261
+ }, NameKeyPostSerializer.new(@post).as_json)
262
+ end
263
+ end
264
+ end
265
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class HasOneAndHasManyTest < Minitest::Test
6
+ def setup
7
+ @post = SpecialPost.new({ title: 'T1', body: 'B1'})
8
+ @post_serializer = SpecialPostSerializer.new(@post)
9
+ end
10
+
11
+ def teardown
12
+ end
13
+
14
+ def test_side_load_has_one_and_has_many_in_same_array
15
+ assert_equal({
16
+ "post" => {
17
+ title: 'T1',
18
+ body: 'B1',
19
+ 'comment_ids' => @post.comments.map { |c| c.object_id },
20
+ 'special_comment_id' => @post_serializer.special_comment.object_id,
21
+ },
22
+ "comments" => [{ content: 'C1' }, { content: 'C2' }, { content: 'special' }]
23
+ }, @post_serializer.as_json(root: 'post'))
24
+ end
25
+ end
26
+ end
27
+ end