active_model_serializers 0.9.12 → 0.10.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +21 -0
- data/.travis.yml +27 -0
- data/CHANGELOG.md +8 -231
- data/CONTRIBUTING.md +23 -12
- data/Gemfile +17 -0
- data/{MIT-LICENSE → LICENSE.txt} +3 -2
- data/README.md +151 -781
- data/Rakefile +12 -0
- data/active_model_serializers.gemspec +26 -0
- data/lib/action_controller/serialization.rb +30 -84
- data/lib/active_model/serializer/adapter/fragment_cache.rb +78 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +15 -0
- data/lib/active_model/serializer/adapter/json.rb +52 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +22 -0
- data/lib/active_model/serializer/adapter/json_api.rb +152 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -0
- data/lib/active_model/serializer/adapter.rb +87 -0
- data/lib/active_model/serializer/array_serializer.rb +32 -0
- data/lib/active_model/serializer/configuration.rb +13 -0
- data/lib/active_model/serializer/fieldset.rb +40 -0
- data/lib/active_model/serializer/version.rb +1 -3
- data/lib/active_model/serializer.rb +192 -263
- data/lib/active_model_serializers.rb +5 -19
- data/lib/generators/serializer/USAGE +6 -0
- data/lib/{active_model/serializer/generators → generators}/serializer/serializer_generator.rb +8 -10
- data/lib/{active_model/serializer/generators → generators}/serializer/templates/serializer.rb +2 -2
- data/test/action_controller/adapter_selector_test.rb +51 -0
- data/test/action_controller/explicit_serializer_test.rb +110 -0
- data/test/action_controller/json_api_linked_test.rb +173 -0
- data/test/action_controller/serialization_scope_name_test.rb +63 -0
- data/test/action_controller/serialization_test.rb +365 -0
- data/test/adapter/fragment_cache_test.rb +27 -0
- data/test/adapter/json/belongs_to_test.rb +41 -0
- data/test/adapter/json/collection_test.rb +59 -0
- data/test/adapter/json/has_many_test.rb +36 -0
- data/test/adapter/json_api/belongs_to_test.rb +147 -0
- data/test/adapter/json_api/collection_test.rb +89 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +106 -0
- data/test/adapter/json_api/has_one_test.rb +59 -0
- data/test/adapter/json_api/linked_test.rb +257 -0
- data/test/adapter/json_test.rb +34 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +43 -0
- data/test/array_serializer_test.rb +29 -0
- data/test/fixtures/poro.rb +123 -172
- data/test/serializers/adapter_for_test.rb +50 -0
- data/test/serializers/associations_test.rb +106 -0
- data/test/serializers/attribute_test.rb +23 -0
- data/test/serializers/attributes_test.rb +28 -0
- data/test/serializers/cache_test.rb +128 -0
- data/test/serializers/configuration_test.rb +15 -0
- data/test/serializers/fieldset_test.rb +26 -0
- data/test/serializers/generators_test.rb +59 -0
- data/test/serializers/meta_test.rb +78 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +56 -0
- data/test/serializers/urls_test.rb +26 -0
- data/test/test_helper.rb +20 -10
- metadata +121 -158
- data/DESIGN.textile +0 -586
- data/lib/action_controller/serialization_test_case.rb +0 -82
- data/lib/active_model/array_serializer.rb +0 -70
- data/lib/active_model/default_serializer.rb +0 -30
- data/lib/active_model/serializable/utils.rb +0 -18
- data/lib/active_model/serializable.rb +0 -61
- data/lib/active_model/serializer/association/has_many.rb +0 -41
- data/lib/active_model/serializer/association/has_one.rb +0 -27
- data/lib/active_model/serializer/association.rb +0 -58
- data/lib/active_model/serializer/config.rb +0 -33
- data/lib/active_model/serializer/generators/resource_override.rb +0 -15
- data/lib/active_model/serializer/generators/serializer/USAGE +0 -9
- data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +0 -16
- data/lib/active_model/serializer/generators/serializer/templates/controller.rb +0 -93
- data/lib/active_model/serializer/railtie.rb +0 -24
- data/lib/active_model/serializer_support.rb +0 -7
- data/test/benchmark/app.rb +0 -60
- data/test/benchmark/benchmarking_support.rb +0 -67
- data/test/benchmark/bm_active_record.rb +0 -41
- data/test/benchmark/setup.rb +0 -75
- data/test/benchmark/tmp/miniprofiler/mp_timers_6eqewtfgrhitvq5gqm25 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_8083sx03hu72pxz1a4d0 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_fyz2gsml4z0ph9kpoy1c +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_hjry5rc32imd42oxoi48 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_m8fpoz2cvt3g9agz0bs3 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_p92m2drnj1i568u3sta0 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_qg52tpca3uesdfguee9i +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_s15t1a6mvxe0z7vjv790 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_x8kal3d17nfds6vp4kcj +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_views_127.0.0.1 +0 -0
- data/test/fixtures/active_record.rb +0 -96
- data/test/fixtures/template.html.erb +0 -1
- data/test/integration/action_controller/namespaced_serialization_test.rb +0 -105
- data/test/integration/action_controller/serialization_test.rb +0 -287
- data/test/integration/action_controller/serialization_test_case_test.rb +0 -71
- data/test/integration/active_record/active_record_test.rb +0 -94
- data/test/integration/generators/resource_generator_test.rb +0 -26
- data/test/integration/generators/scaffold_controller_generator_test.rb +0 -64
- data/test/integration/generators/serializer_generator_test.rb +0 -41
- data/test/test_app.rb +0 -18
- data/test/tmp/app/assets/javascripts/accounts.js +0 -2
- data/test/tmp/app/assets/stylesheets/accounts.css +0 -4
- data/test/tmp/app/controllers/accounts_controller.rb +0 -3
- data/test/tmp/app/helpers/accounts_helper.rb +0 -3
- data/test/tmp/app/serializers/account_serializer.rb +0 -4
- data/test/tmp/config/routes.rb +0 -2
- data/test/unit/active_model/array_serializer/except_test.rb +0 -18
- data/test/unit/active_model/array_serializer/key_format_test.rb +0 -18
- data/test/unit/active_model/array_serializer/meta_test.rb +0 -53
- data/test/unit/active_model/array_serializer/only_test.rb +0 -18
- data/test/unit/active_model/array_serializer/options_test.rb +0 -16
- data/test/unit/active_model/array_serializer/root_test.rb +0 -102
- data/test/unit/active_model/array_serializer/scope_test.rb +0 -24
- data/test/unit/active_model/array_serializer/serialization_test.rb +0 -239
- data/test/unit/active_model/default_serializer_test.rb +0 -13
- data/test/unit/active_model/serializer/associations/build_serializer_test.rb +0 -36
- data/test/unit/active_model/serializer/associations_test.rb +0 -49
- data/test/unit/active_model/serializer/attributes_test.rb +0 -57
- data/test/unit/active_model/serializer/config_test.rb +0 -91
- data/test/unit/active_model/serializer/filter_test.rb +0 -69
- data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +0 -189
- data/test/unit/active_model/serializer/has_many_test.rb +0 -265
- data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +0 -27
- data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +0 -196
- data/test/unit/active_model/serializer/has_one_test.rb +0 -253
- data/test/unit/active_model/serializer/key_format_test.rb +0 -25
- data/test/unit/active_model/serializer/meta_test.rb +0 -39
- data/test/unit/active_model/serializer/options_test.rb +0 -42
- data/test/unit/active_model/serializer/root_test.rb +0 -117
- data/test/unit/active_model/serializer/scope_test.rb +0 -49
- data/test/unit/active_model/serializer/url_helpers_test.rb +0 -36
- data/test/unit/active_model/serilizable_test.rb +0 -50
@@ -1,189 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class HasManyPolymorphicTest < Minitest::Test
|
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
|
@@ -1,265 +0,0 @@
|
|
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
|
@@ -1,27 +0,0 @@
|
|
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
|
@@ -1,196 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class HasOnePolymorphicTest < Minitest::Test
|
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
|