active_model_serializers 0.9.13 → 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 -236
- 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 +193 -276
- 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 -148
- 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/serializers/account_serializer.rb +0 -3
- 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,239 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class ArraySerializer
|
5
|
-
class BasicObjectsSerializationTest < Minitest::Test
|
6
|
-
def setup
|
7
|
-
array = [1, 2, 3]
|
8
|
-
@serializer = Serializer.serializer_for(array).new(array)
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_serializer_for_array_returns_appropriate_type
|
12
|
-
assert_kind_of ActiveModel::ArraySerializer, @serializer
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_array_serializer_serializes_simple_objects
|
16
|
-
assert_equal [1, 2, 3], @serializer.serializable_array
|
17
|
-
assert_equal [1, 2, 3], @serializer.as_json
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
class CustomArraySerializerSupport < Minitest::Test
|
22
|
-
def setup
|
23
|
-
Object.const_set(:ArraySerializer, Class.new)
|
24
|
-
|
25
|
-
array = [1, 2, 3]
|
26
|
-
@serializer_class = Serializer.serializer_for(array)
|
27
|
-
end
|
28
|
-
|
29
|
-
def teardown
|
30
|
-
Object.send(:remove_const, :ArraySerializer)
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_serializer_for_array_returns_appropriate_type
|
34
|
-
assert_equal ::ArraySerializer, @serializer_class
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
class CustomSerializerClassTest < Minitest::Test
|
39
|
-
def setup
|
40
|
-
Object.const_set(:CustomSerializer, Class.new)
|
41
|
-
end
|
42
|
-
|
43
|
-
def teardown
|
44
|
-
Object.send(:remove_const, :CustomSerializer)
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_serializer_for_array_returns_appropriate_type
|
48
|
-
object = {}
|
49
|
-
def object.serializer_class; CustomSerializer; end
|
50
|
-
|
51
|
-
assert_equal CustomSerializer, Serializer.serializer_for(object)
|
52
|
-
assert_nil Serializer.serializer_for('Custom')
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
class ModelSerializationNamespaceTest < Minitest::Test
|
57
|
-
def test_namespace
|
58
|
-
test1 = TestNamespace2::Test.new(name: 'Test 1', email: 'test1@test.com', gender: 'M')
|
59
|
-
test2 = TestNamespace2::Test.new(name: 'Test 2', email: 'test2@test.com', gender: 'M')
|
60
|
-
sub_test1 = TestNamespace2::SubTest.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
61
|
-
sub_test2 = TestNamespace2::SubTest.new(name: 'Name 2', description: 'Description 2', comments: 'Comments 2')
|
62
|
-
test1.sub_test = sub_test1
|
63
|
-
test2.sub_test = sub_test2
|
64
|
-
|
65
|
-
array = [test1, test2]
|
66
|
-
serializer = ArraySerializer.new(array)
|
67
|
-
|
68
|
-
expected = [
|
69
|
-
{ name: 'Test 1', email: 'test1@test.com', sub_test: { name: 'Name 1', description: 'Description 1' } },
|
70
|
-
{ name: 'Test 2', email: 'test2@test.com', sub_test: { name: 'Name 2', description: 'Description 2' } }
|
71
|
-
]
|
72
|
-
|
73
|
-
assert_equal expected, serializer.serializable_array
|
74
|
-
assert_equal expected, serializer.as_json
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
class ModelSerializationTest < Minitest::Test
|
79
|
-
def test_array_serializer_serializes_models
|
80
|
-
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
81
|
-
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
82
|
-
serializer = ArraySerializer.new(array)
|
83
|
-
|
84
|
-
expected = [{ name: 'Name 1', description: 'Description 1' },
|
85
|
-
{ name: 'Name 2', description: 'Description 2' }]
|
86
|
-
|
87
|
-
assert_equal expected, serializer.serializable_array
|
88
|
-
assert_equal expected, serializer.as_json
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_array_serializers_each_serializer
|
92
|
-
array = [::Model.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
93
|
-
::Model.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
94
|
-
serializer = ArraySerializer.new(array, each_serializer: ProfileSerializer)
|
95
|
-
|
96
|
-
expected = [{ name: 'Name 1', description: 'Description 1' },
|
97
|
-
{ name: 'Name 2', description: 'Description 2' }]
|
98
|
-
|
99
|
-
assert_equal expected, serializer.serializable_array
|
100
|
-
assert_equal expected, serializer.as_json
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_associated_objects_of_multiple_instances_embedded_in_root
|
104
|
-
@association = PostSerializer._associations[:comments]
|
105
|
-
@old_association = @association.dup
|
106
|
-
|
107
|
-
@association.embed = :ids
|
108
|
-
@association.embed_in_root = true
|
109
|
-
|
110
|
-
@post1 = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
|
111
|
-
@post2 = Post.new({ title: 'Title 2', body: 'Body 2', date: '1/1/2000' })
|
112
|
-
|
113
|
-
class << @post2
|
114
|
-
attr_writer :comments
|
115
|
-
end
|
116
|
-
|
117
|
-
@post2.comments = [
|
118
|
-
Comment.new(content: 'C3'),
|
119
|
-
Comment.new(content: 'C4')
|
120
|
-
]
|
121
|
-
|
122
|
-
@serializer = ArraySerializer.new([@post1, @post2], root: :posts)
|
123
|
-
assert_equal({
|
124
|
-
posts: [
|
125
|
-
{title: "Title 1", body: "Body 1", "comment_ids" => @post1.comments.map(&:object_id) },
|
126
|
-
{title: "Title 2", body: "Body 2", "comment_ids" => @post2.comments.map(&:object_id) }
|
127
|
-
],
|
128
|
-
'comments' => [
|
129
|
-
{content: "C1"},
|
130
|
-
{content: "C2"},
|
131
|
-
{content: "C3"},
|
132
|
-
{content: "C4"}
|
133
|
-
]
|
134
|
-
}, @serializer.as_json)
|
135
|
-
ensure
|
136
|
-
PostSerializer._associations[:comments] = @old_association
|
137
|
-
end
|
138
|
-
|
139
|
-
def test_embed_object_for_has_one_association_with_nil_value
|
140
|
-
@association = UserSerializer._associations[:profile]
|
141
|
-
@old_association = @association.dup
|
142
|
-
|
143
|
-
@association.embed = :objects
|
144
|
-
|
145
|
-
@user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
|
146
|
-
@user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
|
147
|
-
|
148
|
-
class << @user1
|
149
|
-
def profile
|
150
|
-
nil
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
class << @user2
|
155
|
-
def profile
|
156
|
-
@profile ||= Profile.new(name: 'Name 1', description: 'Desc 1')
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
@serializer = ArraySerializer.new([@user1, @user2]) #, root: :posts)
|
161
|
-
assert_equal([
|
162
|
-
{ name: "User 1", email: "email1@server.com", profile: nil },
|
163
|
-
{ name: "User 2", email: "email2@server.com", profile: { name: 'Name 1', description: 'Desc 1' } }
|
164
|
-
], @serializer.as_json)
|
165
|
-
ensure
|
166
|
-
UserSerializer._associations[:profile] = @old_association
|
167
|
-
end
|
168
|
-
|
169
|
-
def test_embed_object_in_root_for_has_one_association_with_nil_value
|
170
|
-
@association = UserSerializer._associations[:profile]
|
171
|
-
@old_association = @association.dup
|
172
|
-
|
173
|
-
@association.embed = :ids
|
174
|
-
@association.embed_in_root = true
|
175
|
-
|
176
|
-
@user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
|
177
|
-
@user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
|
178
|
-
|
179
|
-
class << @user1
|
180
|
-
def profile
|
181
|
-
nil
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
class << @user2
|
186
|
-
def profile
|
187
|
-
@profile ||= Profile.new(name: 'Name 1', description: 'Desc 1')
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
@serializer = ArraySerializer.new([@user1, @user2], root: :users)
|
192
|
-
assert_equal({
|
193
|
-
users: [
|
194
|
-
{ name: "User 1", email: "email1@server.com", 'profile_id' => nil },
|
195
|
-
{ name: "User 2", email: "email2@server.com", 'profile_id' => @user2.profile.object_id }
|
196
|
-
],
|
197
|
-
'profiles' => [
|
198
|
-
{ name: 'Name 1', description: 'Desc 1' }
|
199
|
-
]
|
200
|
-
}, @serializer.as_json)
|
201
|
-
ensure
|
202
|
-
UserSerializer._associations[:profile] = @old_association
|
203
|
-
end
|
204
|
-
|
205
|
-
def test_embed_object_in_root_for_has_one_association_with_all_nil_values
|
206
|
-
@association = UserSerializer._associations[:profile]
|
207
|
-
@old_association = @association.dup
|
208
|
-
|
209
|
-
@association.embed = :ids
|
210
|
-
@association.embed_in_root = true
|
211
|
-
|
212
|
-
@user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
|
213
|
-
@user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
|
214
|
-
|
215
|
-
class << @user1
|
216
|
-
def profile
|
217
|
-
nil
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
class << @user2
|
222
|
-
def profile
|
223
|
-
nil
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
@serializer = ArraySerializer.new([@user1, @user2], root: :users)
|
228
|
-
assert_equal({
|
229
|
-
users: [
|
230
|
-
{ name: "User 1", email: "email1@server.com", 'profile_id' => nil },
|
231
|
-
{ name: "User 2", email: "email2@server.com", 'profile_id' => nil }
|
232
|
-
]
|
233
|
-
}, @serializer.as_json)
|
234
|
-
ensure
|
235
|
-
UserSerializer._associations[:profile] = @old_association
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
239
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class DefaultSerializer
|
5
|
-
class Test < Minitest::Test
|
6
|
-
def test_serialize_objects
|
7
|
-
assert_nil(DefaultSerializer.new(nil).serializable_object)
|
8
|
-
assert_equal(1, DefaultSerializer.new(1).serializable_object)
|
9
|
-
assert_equal('hi', DefaultSerializer.new('hi').serializable_object)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class Association
|
6
|
-
class BuildSerializerTest < Minitest::Test
|
7
|
-
def setup
|
8
|
-
@association = Association::HasOne.new('post', serializer: PostSerializer)
|
9
|
-
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
|
10
|
-
@user = User.new
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_build_serializer_for_array_called_twice
|
14
|
-
2.times do
|
15
|
-
serializer = @association.build_serializer(@post)
|
16
|
-
assert_instance_of(PostSerializer, serializer)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_build_serializer_from_in_a_namespace
|
21
|
-
assoc = Association::HasOne.new('profile')
|
22
|
-
serializer = TestNamespace::UserSerializer.new(@user).build_serializer(assoc)
|
23
|
-
|
24
|
-
assert_instance_of(TestNamespace::ProfileSerializer, serializer)
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_build_serializer_with_prefix
|
28
|
-
assoc = Association::HasOne.new('profile', prefix: :short)
|
29
|
-
serializer = UserSerializer.new(@user).build_serializer(assoc)
|
30
|
-
|
31
|
-
assert_instance_of(ShortProfileSerializer, serializer)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class AssociationsTest < Minitest::Test
|
6
|
-
def test_associations_inheritance
|
7
|
-
inherited_serializer_klass = Class.new(PostSerializer) do
|
8
|
-
has_many :users
|
9
|
-
end
|
10
|
-
another_inherited_serializer_klass = Class.new(PostSerializer)
|
11
|
-
|
12
|
-
assert_equal([:comments, :users],
|
13
|
-
inherited_serializer_klass._associations.keys)
|
14
|
-
assert_equal([:comments],
|
15
|
-
another_inherited_serializer_klass._associations.keys)
|
16
|
-
end
|
17
|
-
def test_multiple_nested_associations
|
18
|
-
parent = SelfReferencingUserParent.new(name: "The Parent")
|
19
|
-
child = SelfReferencingUser.new(name: "The child", parent: parent)
|
20
|
-
self_referencing_user_serializer = SelfReferencingUserSerializer.new(child)
|
21
|
-
result = self_referencing_user_serializer.as_json
|
22
|
-
expected_result = {
|
23
|
-
"self_referencing_user"=>{
|
24
|
-
:name=>"The child",
|
25
|
-
"type_id"=>child.type.object_id,
|
26
|
-
"parent_id"=>child.parent.object_id
|
27
|
-
|
28
|
-
},
|
29
|
-
"types"=>[
|
30
|
-
{
|
31
|
-
:name=>"N1",
|
32
|
-
},
|
33
|
-
{
|
34
|
-
:name=>"N2",
|
35
|
-
}
|
36
|
-
],
|
37
|
-
"parents"=>[
|
38
|
-
{
|
39
|
-
:name=>"N1",
|
40
|
-
"type_id"=>child.parent.type.object_id,
|
41
|
-
"parent_id"=>nil
|
42
|
-
}
|
43
|
-
]
|
44
|
-
}
|
45
|
-
assert_equal(expected_result, result)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class AttributesTest < Minitest::Test
|
6
|
-
def setup
|
7
|
-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
8
|
-
@profile_serializer = ProfileSerializer.new(@profile)
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_attributes_definition
|
12
|
-
assert_equal([:name, :description],
|
13
|
-
@profile_serializer.class._attributes)
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_attributes_serialization_using_serializable_hash
|
17
|
-
assert_equal({
|
18
|
-
name: 'Name 1', description: 'Description 1'
|
19
|
-
}, @profile_serializer.serializable_hash)
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_attributes_serialization_using_as_json
|
23
|
-
assert_equal({
|
24
|
-
'profile' => { name: 'Name 1', description: 'Description 1' }
|
25
|
-
}, @profile_serializer.as_json)
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_attributes_inheritance
|
29
|
-
inherited_serializer_klass = Class.new(ProfileSerializer) do
|
30
|
-
attributes :comments
|
31
|
-
end
|
32
|
-
another_inherited_serializer_klass = Class.new(ProfileSerializer)
|
33
|
-
|
34
|
-
assert_equal([:name, :description, :comments],
|
35
|
-
inherited_serializer_klass._attributes)
|
36
|
-
assert_equal([:name, :description],
|
37
|
-
another_inherited_serializer_klass._attributes)
|
38
|
-
end
|
39
|
-
|
40
|
-
def tests_query_attributes_strip_question_mark
|
41
|
-
model = Class.new(::Model) do
|
42
|
-
def strip?
|
43
|
-
true
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
serializer = Class.new(ActiveModel::Serializer) do
|
48
|
-
attributes :strip?
|
49
|
-
end
|
50
|
-
|
51
|
-
actual = serializer.new(model.new).as_json
|
52
|
-
|
53
|
-
assert_equal({ strip: true }, actual)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
@@ -1,91 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class Config
|
6
|
-
class Test < Minitest::Test
|
7
|
-
def test_config_const_is_an_instance_of_config
|
8
|
-
assert_kind_of Config, CONFIG
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_config_instance
|
12
|
-
config = Config.new
|
13
|
-
config.setting1 = 'value1'
|
14
|
-
|
15
|
-
assert_equal 'value1', config.setting1
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_each_config
|
19
|
-
config = Config.new
|
20
|
-
config.setting1 = 'value1'
|
21
|
-
config.setting2 = 'value2'
|
22
|
-
|
23
|
-
actual = {}
|
24
|
-
|
25
|
-
config.each do |k, v|
|
26
|
-
actual[k] = v
|
27
|
-
end
|
28
|
-
|
29
|
-
assert_equal({ 'setting1' => 'value1', 'setting2' => 'value2' }, actual)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
class ConfigTest < Minitest::Test
|
34
|
-
def test_setup
|
35
|
-
Serializer.setup do |config|
|
36
|
-
config.a = 'v1'
|
37
|
-
config.b = 'v2'
|
38
|
-
end
|
39
|
-
|
40
|
-
assert_equal 'v1', CONFIG.a
|
41
|
-
assert_equal 'v2', CONFIG.b
|
42
|
-
ensure
|
43
|
-
CONFIG.clear
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_config_accessors
|
47
|
-
Serializer.setup do |config|
|
48
|
-
config.foo = 'v1'
|
49
|
-
config.bar = 'v2'
|
50
|
-
end
|
51
|
-
|
52
|
-
assert_equal 'v1', CONFIG.foo
|
53
|
-
assert_equal 'v2', CONFIG.bar
|
54
|
-
ensure
|
55
|
-
CONFIG.clear
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_acessor_when_nil
|
59
|
-
assert_nil CONFIG.foo
|
60
|
-
CONFIG.foo = 1
|
61
|
-
assert_equal 1, CONFIG.foo
|
62
|
-
assert_nil CONFIG.bar
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
class ApplyConfigTest < Minitest::Test
|
67
|
-
def test_apply_config_to_associations
|
68
|
-
CONFIG.embed = :ids
|
69
|
-
CONFIG.embed_in_root = true
|
70
|
-
CONFIG.key_format = :lower_camel
|
71
|
-
|
72
|
-
association = PostSerializer._associations[:comments]
|
73
|
-
old_association = association.dup
|
74
|
-
|
75
|
-
association.send :initialize, association.name, association.options
|
76
|
-
|
77
|
-
assert association.embed_ids?
|
78
|
-
assert !association.embed_objects?
|
79
|
-
assert association.embed_in_root
|
80
|
-
assert_equal :lower_camel, association.key_format
|
81
|
-
assert_equal 'post', PostSerializer.root_name
|
82
|
-
CONFIG.plural_default_root = true
|
83
|
-
assert_equal 'posts', PostSerializer.root_name
|
84
|
-
ensure
|
85
|
-
PostSerializer._associations[:comments] = old_association
|
86
|
-
CONFIG.clear
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
@@ -1,69 +0,0 @@
|
|
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
|