active_model_serializers 0.9.8 → 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 -200
- 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 -75
- data/lib/active_model/serializer.rb +192 -252
- data/lib/active_model/serializer/adapter.rb +87 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +78 -0
- data/lib/active_model/serializer/adapter/json.rb +52 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +15 -0
- data/lib/active_model/serializer/adapter/json_api.rb +152 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +22 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -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 -1
- data/lib/active_model_serializers.rb +5 -7
- data/lib/generators/serializer/USAGE +6 -0
- data/lib/{active_model/serializer/generators → generators}/serializer/serializer_generator.rb +8 -8
- 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 +125 -142
- 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 +21 -4
- metadata +121 -159
- data/DESIGN.textile +0 -586
- data/lib/action_controller/serialization_test_case.rb +0 -79
- data/lib/active_model/array_serializer.rb +0 -68
- data/lib/active_model/default_serializer.rb +0 -28
- data/lib/active_model/serializable.rb +0 -59
- data/lib/active_model/serializable/utils.rb +0 -16
- data/lib/active_model/serializer/association.rb +0 -58
- data/lib/active_model/serializer/association/has_many.rb +0 -39
- data/lib/active_model/serializer/association/has_one.rb +0 -25
- data/lib/active_model/serializer/config.rb +0 -31
- data/lib/active_model/serializer/generators/resource_override.rb +0 -13
- data/lib/active_model/serializer/generators/serializer/USAGE +0 -9
- data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +0 -14
- data/lib/active_model/serializer/generators/serializer/templates/controller.rb +0 -93
- data/lib/active_model/serializer/railtie.rb +0 -22
- data/lib/active_model/serializer_support.rb +0 -5
- data/lib/active_model_serializers/model/caching.rb +0 -25
- 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 -14
- 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 -2
- data/test/tmp/app/helpers/accounts_helper.rb +0 -2
- data/test/tmp/app/serializers/account_serializer.rb +0 -3
- data/test/tmp/config/routes.rb +0 -1
- 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 -216
- 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 -35
- data/test/unit/active_model/serilizable_test.rb +0 -50
@@ -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 < 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
|
@@ -1,253 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class HasOneTest < Minitest::Test
|
6
|
-
def setup
|
7
|
-
@association = UserSerializer._associations[:profile]
|
8
|
-
@old_association = @association.dup
|
9
|
-
|
10
|
-
@user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
|
11
|
-
@user_serializer = UserSerializer.new(@user)
|
12
|
-
end
|
13
|
-
|
14
|
-
def teardown
|
15
|
-
UserSerializer._associations[:profile] = @old_association
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_associations_definition
|
19
|
-
assert_equal 1, UserSerializer._associations.length
|
20
|
-
assert_kind_of Association::HasOne, @association
|
21
|
-
assert_equal 'profile', @association.name
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_associations_embedding_ids_serialization_using_serializable_hash
|
25
|
-
@association.embed = :ids
|
26
|
-
|
27
|
-
assert_equal({
|
28
|
-
name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id
|
29
|
-
}, @user_serializer.serializable_hash)
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_associations_embedding_ids_serialization_using_as_json
|
33
|
-
@association.embed = :ids
|
34
|
-
|
35
|
-
assert_equal({
|
36
|
-
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id }
|
37
|
-
}, @user_serializer.as_json)
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
|
41
|
-
@association.embed = :ids
|
42
|
-
@association.key = 'key'
|
43
|
-
|
44
|
-
assert_equal({
|
45
|
-
name: 'Name 1', email: 'mail@server.com', 'key' => @user.profile.object_id
|
46
|
-
}, @user_serializer.serializable_hash)
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_associations_embedding_objects_serialization_using_serializable_hash
|
50
|
-
@association.embed = :objects
|
51
|
-
|
52
|
-
assert_equal({
|
53
|
-
name: 'Name 1', email: 'mail@server.com', profile: { name: 'N1', description: 'D1' }
|
54
|
-
}, @user_serializer.serializable_hash)
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_associations_embedding_objects_serialization_using_as_json
|
58
|
-
@association.embed = :objects
|
59
|
-
|
60
|
-
assert_equal({
|
61
|
-
'user' => { name: 'Name 1', email: 'mail@server.com', profile: { name: 'N1', description: 'D1' } }
|
62
|
-
}, @user_serializer.as_json)
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_associations_embedding_nil_ids_serialization_using_as_json
|
66
|
-
@association.embed = :ids
|
67
|
-
@user.instance_eval do
|
68
|
-
def profile
|
69
|
-
nil
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
assert_equal({
|
74
|
-
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => nil }
|
75
|
-
}, @user_serializer.as_json)
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_associations_embedding_nil_objects_serialization_using_as_json
|
79
|
-
@association.embed = :objects
|
80
|
-
@user.instance_eval do
|
81
|
-
def profile
|
82
|
-
nil
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
assert_equal({
|
87
|
-
'user' => { name: 'Name 1', email: 'mail@server.com', profile: nil }
|
88
|
-
}, @user_serializer.as_json)
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
92
|
-
@association.embed = :objects
|
93
|
-
@association.embedded_key = 'root'
|
94
|
-
|
95
|
-
assert_equal({
|
96
|
-
name: 'Name 1', email: 'mail@server.com', 'root' => { name: 'N1', description: 'D1' }
|
97
|
-
}, @user_serializer.serializable_hash)
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
101
|
-
@association.embed = :ids
|
102
|
-
@association.embed_in_root = true
|
103
|
-
|
104
|
-
assert_equal({
|
105
|
-
name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id
|
106
|
-
}, @user_serializer.serializable_hash)
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_associations_embedding_nil_ids_including_objects_serialization_using_as_json
|
110
|
-
@association.embed = :ids
|
111
|
-
@association.embed_in_root = true
|
112
|
-
@user.instance_eval do
|
113
|
-
def profile
|
114
|
-
nil
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
assert_equal({
|
119
|
-
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => nil },
|
120
|
-
'profiles' => []
|
121
|
-
}, @user_serializer.as_json)
|
122
|
-
end
|
123
|
-
|
124
|
-
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
125
|
-
@association.embed = :ids
|
126
|
-
@association.embed_in_root = true
|
127
|
-
|
128
|
-
assert_equal({
|
129
|
-
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id },
|
130
|
-
'profiles' => [{ name: 'N1', description: 'D1' }]
|
131
|
-
}, @user_serializer.as_json)
|
132
|
-
end
|
133
|
-
|
134
|
-
def test_associations_embedding_ids_including_objects_serialization_when_invoked_from_parent_serializer
|
135
|
-
@association.embed = :ids
|
136
|
-
@association.embed_in_root = true
|
137
|
-
|
138
|
-
user_info = UserInfo.new
|
139
|
-
user_info.instance_variable_set(:@user, @user)
|
140
|
-
user_info_serializer = UserInfoSerializer.new(user_info)
|
141
|
-
|
142
|
-
assert_equal({
|
143
|
-
'user_info' => { user: { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id } },
|
144
|
-
'profiles' => [{ name: 'N1', description: 'D1' }]
|
145
|
-
}, user_info_serializer.as_json)
|
146
|
-
end
|
147
|
-
|
148
|
-
def test_associations_embedding_ids_using_a_given_serializer
|
149
|
-
@association.embed = :ids
|
150
|
-
@association.embed_in_root = true
|
151
|
-
@association.serializer_from_options = Class.new(Serializer) do
|
152
|
-
def name
|
153
|
-
'fake'
|
154
|
-
end
|
155
|
-
|
156
|
-
attributes :name
|
157
|
-
end
|
158
|
-
|
159
|
-
assert_equal({
|
160
|
-
'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id },
|
161
|
-
'profiles' => [{ name: 'fake' }]
|
162
|
-
}, @user_serializer.as_json)
|
163
|
-
end
|
164
|
-
|
165
|
-
def test_associations_embedding_objects_using_a_given_serializer
|
166
|
-
@association.serializer_from_options = Class.new(Serializer) do
|
167
|
-
def name
|
168
|
-
'fake'
|
169
|
-
end
|
170
|
-
|
171
|
-
attributes :name
|
172
|
-
end
|
173
|
-
|
174
|
-
assert_equal({
|
175
|
-
'user' => { name: 'Name 1', email: 'mail@server.com', profile: { name: 'fake' } }
|
176
|
-
}, @user_serializer.as_json)
|
177
|
-
end
|
178
|
-
|
179
|
-
def test_associations_embedding_objects_with_nil_values
|
180
|
-
user_info = UserInfo.new
|
181
|
-
user_info.instance_eval do
|
182
|
-
def user
|
183
|
-
nil
|
184
|
-
end
|
185
|
-
end
|
186
|
-
user_info_serializer = UserInfoSerializer.new(user_info)
|
187
|
-
|
188
|
-
assert_equal({
|
189
|
-
'user_info' => { user: nil }
|
190
|
-
}, user_info_serializer.as_json)
|
191
|
-
end
|
192
|
-
|
193
|
-
def test_associations_embedding_ids_using_embed_namespace
|
194
|
-
@association.embed_namespace = :links
|
195
|
-
@association.embed = :ids
|
196
|
-
@association.key = :profile
|
197
|
-
assert_equal({
|
198
|
-
'user' => {
|
199
|
-
name: 'Name 1', email: 'mail@server.com',
|
200
|
-
links: {
|
201
|
-
profile: @user.profile.object_id
|
202
|
-
}
|
203
|
-
}
|
204
|
-
}, @user_serializer.as_json)
|
205
|
-
end
|
206
|
-
|
207
|
-
def test_asociations_embedding_objects_using_embed_namespace
|
208
|
-
@association.embed_namespace = :links
|
209
|
-
@association.embed = :objects
|
210
|
-
assert_equal({
|
211
|
-
'user' => {
|
212
|
-
name: 'Name 1', email: 'mail@server.com',
|
213
|
-
links: {
|
214
|
-
profile: { name: 'N1', description: 'D1' }
|
215
|
-
}
|
216
|
-
}
|
217
|
-
}, @user_serializer.as_json)
|
218
|
-
end
|
219
|
-
|
220
|
-
def test_associations_embedding_ids_using_embed_namespace_and_embed_in_root_key
|
221
|
-
@association.embed_in_root = true
|
222
|
-
@association.embed_in_root_key = :linked
|
223
|
-
@association.embed = :ids
|
224
|
-
assert_equal({
|
225
|
-
'user' => {
|
226
|
-
name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id
|
227
|
-
},
|
228
|
-
linked: {
|
229
|
-
'profiles' => [ { name: 'N1', description: 'D1' } ]
|
230
|
-
}
|
231
|
-
}, @user_serializer.as_json)
|
232
|
-
end
|
233
|
-
|
234
|
-
def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
|
235
|
-
@association = NameKeyUserSerializer._associations[:profile]
|
236
|
-
@association.embed = :ids
|
237
|
-
|
238
|
-
assert_equal({
|
239
|
-
name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id
|
240
|
-
}, NameKeyUserSerializer.new(@user).serializable_hash)
|
241
|
-
end
|
242
|
-
|
243
|
-
def test_associations_name_key_embedding_ids_serialization_using_as_json
|
244
|
-
@association = NameKeyUserSerializer._associations[:profile]
|
245
|
-
@association.embed = :ids
|
246
|
-
|
247
|
-
assert_equal({
|
248
|
-
'name_key_user' => { name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id }
|
249
|
-
}, NameKeyUserSerializer.new(@user).as_json)
|
250
|
-
end
|
251
|
-
end
|
252
|
-
end
|
253
|
-
end
|