active_model_serializers 0.8.3 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +31 -5
- data/CONTRIBUTING.md +20 -0
- data/DESIGN.textile +4 -4
- data/{MIT-LICENSE.txt → MIT-LICENSE} +0 -0
- data/README.md +395 -95
- data/lib/action_controller/serialization.rb +50 -12
- data/lib/action_controller/serialization_test_case.rb +79 -0
- data/lib/active_model/array_serializer.rb +46 -78
- data/lib/active_model/default_serializer.rb +32 -0
- data/lib/active_model/serializable/utils.rb +16 -0
- data/lib/active_model/serializable.rb +62 -0
- data/lib/active_model/serializer/association/has_many.rb +39 -0
- data/lib/active_model/serializer/association/has_one.rb +25 -0
- data/lib/active_model/serializer/association.rb +58 -0
- data/lib/active_model/serializer/config.rb +31 -0
- data/lib/active_model/serializer/generators/resource_override.rb +13 -0
- data/lib/{generators → active_model/serializer/generators}/serializer/USAGE +0 -0
- data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +14 -0
- data/lib/active_model/serializer/generators/serializer/serializer_generator.rb +37 -0
- data/lib/active_model/serializer/generators/serializer/templates/controller.rb +93 -0
- data/lib/active_model/serializer/generators/serializer/templates/serializer.rb +8 -0
- data/lib/active_model/serializer/railtie.rb +18 -0
- data/lib/active_model/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +209 -422
- data/lib/active_model/serializer_support.rb +5 -0
- data/lib/active_model_serializers.rb +12 -87
- data/test/fixtures/active_record.rb +92 -0
- data/test/fixtures/poro.rb +184 -0
- data/test/fixtures/template.html.erb +1 -0
- data/test/integration/action_controller/namespaced_serialization_test.rb +96 -0
- data/test/integration/action_controller/serialization_test.rb +303 -0
- data/test/integration/action_controller/serialization_test_case_test.rb +71 -0
- data/test/integration/active_record/active_record_test.rb +77 -0
- data/test/integration/generators/resource_generator_test.rb +26 -0
- data/test/integration/generators/scaffold_controller_generator_test.rb +64 -0
- data/test/integration/generators/serializer_generator_test.rb +41 -0
- data/test/test_app.rb +14 -0
- data/test/test_helper.rb +10 -18
- data/test/tmp/app/serializers/account_serializer.rb +3 -0
- data/test/unit/active_model/array_serializer/except_test.rb +18 -0
- data/test/unit/active_model/array_serializer/key_format_test.rb +18 -0
- data/test/unit/active_model/array_serializer/meta_test.rb +53 -0
- data/test/unit/active_model/array_serializer/only_test.rb +18 -0
- data/test/unit/active_model/array_serializer/root_test.rb +102 -0
- data/test/unit/active_model/array_serializer/scope_test.rb +24 -0
- data/test/unit/active_model/array_serializer/serialization_test.rb +199 -0
- data/test/unit/active_model/default_serializer_test.rb +13 -0
- data/test/unit/active_model/serializer/associations/build_serializer_test.rb +36 -0
- data/test/unit/active_model/serializer/associations_test.rb +19 -0
- data/test/unit/active_model/serializer/attributes_test.rb +57 -0
- data/test/unit/active_model/serializer/config_test.rb +91 -0
- data/test/unit/active_model/serializer/filter_test.rb +69 -0
- data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +189 -0
- data/test/unit/active_model/serializer/has_many_test.rb +265 -0
- data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +27 -0
- data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +196 -0
- data/test/unit/active_model/serializer/has_one_test.rb +239 -0
- data/test/unit/active_model/serializer/key_format_test.rb +25 -0
- data/test/unit/active_model/serializer/meta_test.rb +39 -0
- data/test/unit/active_model/serializer/options_test.rb +34 -0
- data/test/unit/active_model/serializer/root_test.rb +117 -0
- data/test/unit/active_model/serializer/scope_test.rb +49 -0
- data/test/unit/active_model/serializer/url_helpers_test.rb +35 -0
- metadata +104 -63
- data/.gitignore +0 -18
- data/.travis.yml +0 -28
- data/Gemfile +0 -4
- data/Gemfile.edge +0 -9
- data/Rakefile +0 -18
- data/active_model_serializers.gemspec +0 -24
- data/bench/perf.rb +0 -43
- data/cruft.md +0 -19
- data/lib/active_model/serializer/associations.rb +0 -233
- data/lib/active_record/serializer_override.rb +0 -16
- data/lib/generators/resource_override.rb +0 -13
- data/lib/generators/serializer/serializer_generator.rb +0 -42
- data/lib/generators/serializer/templates/serializer.rb +0 -19
- data/test/array_serializer_test.rb +0 -75
- data/test/association_test.rb +0 -592
- data/test/caching_test.rb +0 -96
- data/test/generators_test.rb +0 -85
- data/test/no_serialization_scope_test.rb +0 -34
- data/test/serialization_scope_name_test.rb +0 -67
- data/test/serialization_test.rb +0 -392
- data/test/serializer_support_test.rb +0 -51
- data/test/serializer_test.rb +0 -1465
- data/test/test_fakes.rb +0 -217
@@ -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
|
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
|
@@ -0,0 +1,196 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class HasOnePolymorphicTest < ActiveModel::TestCase
|
6
|
+
def setup
|
7
|
+
@association = InterviewSerializer._associations[:attachment]
|
8
|
+
@old_association = @association.dup
|
9
|
+
|
10
|
+
@interview = Interview.new({ text: 'Text 1' })
|
11
|
+
@interview_serializer = InterviewSerializer.new(@interview)
|
12
|
+
end
|
13
|
+
|
14
|
+
def teardown
|
15
|
+
InterviewSerializer._associations[:attachment] = @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, InterviewSerializer._associations.length
|
24
|
+
assert_kind_of Association::HasOne, @association
|
25
|
+
assert_equal true, @association.polymorphic
|
26
|
+
assert_equal 'attachment', @association.name
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_associations_embedding_ids_serialization_using_serializable_hash
|
30
|
+
@association.embed = :ids
|
31
|
+
|
32
|
+
assert_equal({
|
33
|
+
text: 'Text 1',
|
34
|
+
'attachment_id' => {
|
35
|
+
type: model_name(@interview.attachment),
|
36
|
+
id: @interview.attachment.object_id
|
37
|
+
}
|
38
|
+
}, @interview_serializer.serializable_hash)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_associations_embedding_ids_serialization_using_as_json
|
42
|
+
@association.embed = :ids
|
43
|
+
|
44
|
+
assert_equal({
|
45
|
+
'interview' => {
|
46
|
+
text: 'Text 1',
|
47
|
+
'attachment_id' => {
|
48
|
+
type: model_name(@interview.attachment),
|
49
|
+
id: @interview.attachment.object_id
|
50
|
+
}
|
51
|
+
}
|
52
|
+
}, @interview_serializer.as_json)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
|
56
|
+
@association.embed = :ids
|
57
|
+
@association.key = 'key'
|
58
|
+
|
59
|
+
assert_equal({
|
60
|
+
text: 'Text 1',
|
61
|
+
'key' => {
|
62
|
+
type: model_name(@interview.attachment),
|
63
|
+
id: @interview.attachment.object_id
|
64
|
+
}
|
65
|
+
}, @interview_serializer.serializable_hash)
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_associations_embedding_objects_serialization_using_serializable_hash
|
69
|
+
@association.embed = :objects
|
70
|
+
|
71
|
+
assert_equal({
|
72
|
+
text: 'Text 1',
|
73
|
+
attachment: {
|
74
|
+
type: model_name(@interview.attachment),
|
75
|
+
model_name(@interview.attachment) => { url: 'U1'}
|
76
|
+
}
|
77
|
+
}, @interview_serializer.serializable_hash)
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_associations_embedding_objects_serialization_using_as_json
|
81
|
+
@association.embed = :objects
|
82
|
+
|
83
|
+
assert_equal({
|
84
|
+
'interview' => {
|
85
|
+
text: 'Text 1',
|
86
|
+
attachment: {
|
87
|
+
type: model_name(@interview.attachment),
|
88
|
+
model_name(@interview.attachment) => { url: 'U1'}
|
89
|
+
}
|
90
|
+
}
|
91
|
+
}, @interview_serializer.as_json)
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_associations_embedding_nil_ids_serialization_using_as_json
|
95
|
+
@association.embed = :ids
|
96
|
+
@interview.instance_eval do
|
97
|
+
def attachment
|
98
|
+
nil
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
assert_equal({
|
103
|
+
'interview' => { text: 'Text 1', 'attachment_id' => nil }
|
104
|
+
}, @interview_serializer.as_json)
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_associations_embedding_nil_objects_serialization_using_as_json
|
108
|
+
@association.embed = :objects
|
109
|
+
@interview.instance_eval do
|
110
|
+
def attachment
|
111
|
+
nil
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
assert_equal({
|
116
|
+
'interview' => { text: 'Text 1', attachment: nil }
|
117
|
+
}, @interview_serializer.as_json)
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
121
|
+
@association.embed = :objects
|
122
|
+
@association.embedded_key = 'root'
|
123
|
+
|
124
|
+
assert_equal({
|
125
|
+
text: 'Text 1',
|
126
|
+
'root' => {
|
127
|
+
type: model_name(@interview.attachment),
|
128
|
+
model_name(@interview.attachment) => { url: 'U1'}
|
129
|
+
}
|
130
|
+
}, @interview_serializer.serializable_hash)
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
134
|
+
@association.embed = :ids
|
135
|
+
@association.embed_in_root = true
|
136
|
+
|
137
|
+
assert_equal({
|
138
|
+
text: 'Text 1',
|
139
|
+
'attachment_id' => {
|
140
|
+
type: model_name(@interview.attachment),
|
141
|
+
id: @interview.attachment.object_id
|
142
|
+
}
|
143
|
+
}, @interview_serializer.serializable_hash)
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
147
|
+
@association.embed = :ids
|
148
|
+
@association.embed_in_root = true
|
149
|
+
|
150
|
+
assert_equal({
|
151
|
+
'interview' => {
|
152
|
+
text: 'Text 1',
|
153
|
+
'attachment_id' => {
|
154
|
+
type: model_name(@interview.attachment),
|
155
|
+
id: @interview.attachment.object_id
|
156
|
+
}
|
157
|
+
},
|
158
|
+
"attachments" => [{
|
159
|
+
type: model_name(@interview.attachment),
|
160
|
+
model_name(@interview.attachment) => {
|
161
|
+
url: 'U1'
|
162
|
+
}
|
163
|
+
}]
|
164
|
+
}, @interview_serializer.as_json)
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_associations_using_a_given_serializer
|
168
|
+
@association.embed = :ids
|
169
|
+
@association.embed_in_root = true
|
170
|
+
@association.serializer_from_options = Class.new(ActiveModel::Serializer) do
|
171
|
+
def name
|
172
|
+
'fake'
|
173
|
+
end
|
174
|
+
|
175
|
+
attributes :name
|
176
|
+
end
|
177
|
+
|
178
|
+
assert_equal({
|
179
|
+
'interview' => {
|
180
|
+
text: 'Text 1',
|
181
|
+
'attachment_id' => {
|
182
|
+
type: model_name(@interview.attachment),
|
183
|
+
id: @interview.attachment.object_id
|
184
|
+
}
|
185
|
+
},
|
186
|
+
"attachments" => [{
|
187
|
+
type: model_name(@interview.attachment),
|
188
|
+
model_name(@interview.attachment) => {
|
189
|
+
name: 'fake'
|
190
|
+
}
|
191
|
+
}]
|
192
|
+
}, @interview_serializer.as_json)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|