active_model_serializers 0.8.3 → 0.10.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/.rubocop.yml +82 -0
- data/.rubocop_todo.yml +315 -0
- data/.simplecov +99 -0
- data/.travis.yml +26 -20
- data/CHANGELOG.md +14 -67
- data/CONTRIBUTING.md +31 -0
- data/Gemfile +45 -1
- data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
- data/README.md +186 -488
- data/Rakefile +33 -12
- data/active_model_serializers.gemspec +49 -23
- data/appveyor.yml +25 -0
- data/docs/README.md +29 -0
- data/docs/general/adapters.md +110 -0
- data/docs/general/configuration_options.md +11 -0
- data/docs/general/getting_started.md +73 -0
- data/docs/howto/add_pagination_links.md +112 -0
- data/docs/howto/add_root_key.md +51 -0
- data/docs/howto/outside_controller_use.md +42 -0
- data/lib/action_controller/serialization.rb +31 -31
- data/lib/active_model/serializable_resource.rb +70 -0
- data/lib/active_model/serializer/adapter/flatten_json.rb +12 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +75 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +5 -0
- data/lib/active_model/serializer/adapter/json.rb +47 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +13 -0
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +50 -0
- data/lib/active_model/serializer/adapter/json_api.rb +158 -0
- data/lib/active_model/serializer/adapter/null.rb +5 -0
- data/lib/active_model/serializer/adapter.rb +159 -0
- data/lib/active_model/serializer/array_serializer.rb +40 -0
- data/lib/active_model/serializer/association.rb +20 -0
- data/lib/active_model/serializer/associations.rb +83 -219
- data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
- data/lib/active_model/serializer/collection_reflection.rb +7 -0
- data/lib/active_model/serializer/configuration.rb +14 -0
- data/lib/active_model/serializer/fieldset.rb +40 -0
- data/lib/active_model/serializer/has_many_reflection.rb +10 -0
- data/lib/active_model/serializer/has_one_reflection.rb +10 -0
- data/lib/active_model/serializer/lint.rb +129 -0
- data/lib/active_model/serializer/railtie.rb +15 -0
- data/lib/active_model/serializer/reflection.rb +74 -0
- data/lib/active_model/serializer/singular_reflection.rb +7 -0
- data/lib/active_model/serializer/utils.rb +35 -0
- data/lib/active_model/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +121 -465
- data/lib/active_model_serializers.rb +26 -88
- data/lib/generators/serializer/USAGE +0 -3
- data/lib/generators/serializer/resource_override.rb +12 -0
- data/lib/generators/serializer/serializer_generator.rb +8 -13
- data/lib/generators/serializer/templates/serializer.rb.erb +8 -0
- data/lib/tasks/rubocop.rake +0 -0
- data/test/action_controller/adapter_selector_test.rb +53 -0
- data/test/action_controller/explicit_serializer_test.rb +134 -0
- data/test/action_controller/json_api/linked_test.rb +179 -0
- data/test/action_controller/json_api/pagination_test.rb +116 -0
- data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +15 -15
- data/test/action_controller/serialization_test.rb +420 -0
- data/test/active_record_test.rb +9 -0
- data/test/adapter/fragment_cache_test.rb +37 -0
- data/test/adapter/json/belongs_to_test.rb +47 -0
- data/test/adapter/json/collection_test.rb +82 -0
- data/test/adapter/json/has_many_test.rb +47 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +95 -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 +145 -0
- data/test/adapter/json_api/has_one_test.rb +81 -0
- data/test/adapter/json_api/json_api_test.rb +37 -0
- data/test/adapter/json_api/linked_test.rb +283 -0
- data/test/adapter/json_api/pagination_links_test.rb +115 -0
- data/test/adapter/json_api/resource_type_config_test.rb +59 -0
- data/test/adapter/json_test.rb +47 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +42 -0
- data/test/array_serializer_test.rb +99 -73
- data/test/capture_warnings.rb +65 -0
- data/test/fixtures/active_record.rb +56 -0
- data/test/fixtures/poro.rb +261 -0
- data/test/generators/scaffold_controller_generator_test.rb +23 -0
- data/test/generators/serializer_generator_test.rb +56 -0
- data/test/lint_test.rb +37 -0
- data/test/logger_test.rb +18 -0
- data/test/poro_test.rb +9 -0
- data/test/serializable_resource_test.rb +27 -0
- data/test/serializers/adapter_for_test.rb +170 -0
- data/test/serializers/association_macros_test.rb +36 -0
- data/test/serializers/associations_test.rb +150 -0
- data/test/serializers/attribute_test.rb +62 -0
- data/test/serializers/attributes_test.rb +57 -0
- data/test/serializers/cache_test.rb +165 -0
- data/test/serializers/configuration_test.rb +15 -0
- data/test/serializers/fieldset_test.rb +25 -0
- data/test/serializers/meta_test.rb +121 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/root_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +65 -0
- data/test/support/rails_app.rb +21 -0
- data/test/support/serialization_testing.rb +13 -0
- data/test/support/simplecov.rb +6 -0
- data/test/support/stream_capture.rb +50 -0
- data/test/support/test_case.rb +5 -0
- data/test/test_helper.rb +48 -24
- data/test/utils/include_args_to_hash_test.rb +79 -0
- metadata +219 -47
- data/DESIGN.textile +0 -586
- data/Gemfile.edge +0 -9
- data/bench/perf.rb +0 -43
- data/cruft.md +0 -19
- data/lib/active_model/array_serializer.rb +0 -104
- data/lib/active_record/serializer_override.rb +0 -16
- data/lib/generators/resource_override.rb +0 -13
- data/lib/generators/serializer/templates/serializer.rb +0 -19
- 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_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
data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb}
RENAMED
@@ -2,12 +2,12 @@ require 'test_helper'
|
|
2
2
|
require 'pathname'
|
3
3
|
|
4
4
|
class DefaultScopeNameTest < ActionController::TestCase
|
5
|
-
TestUser = Struct.new(:name, :admin)
|
6
|
-
|
7
5
|
class UserSerializer < ActiveModel::Serializer
|
8
6
|
attributes :admin?
|
9
|
-
|
10
|
-
|
7
|
+
ActiveModelSerializers.silence_warnings do
|
8
|
+
def admin?
|
9
|
+
current_user.admin
|
10
|
+
end
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
@@ -17,29 +17,29 @@ class DefaultScopeNameTest < ActionController::TestCase
|
|
17
17
|
before_filter { request.format = :json }
|
18
18
|
|
19
19
|
def current_user
|
20
|
-
|
20
|
+
User.new(id: 1, name: 'Pete', admin: false)
|
21
21
|
end
|
22
22
|
|
23
23
|
def render_new_user
|
24
|
-
render :
|
24
|
+
render json: User.new(id: 1, name: 'Pete', admin: false), serializer: UserSerializer, adapter: :json_api
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
|
28
|
+
tests UserTestController
|
29
29
|
|
30
30
|
def test_default_scope_name
|
31
31
|
get :render_new_user
|
32
|
-
assert_equal '{"
|
32
|
+
assert_equal '{"data":{"id":"1","type":"users","attributes":{"admin?":false}}}', @response.body
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
36
|
class SerializationScopeNameTest < ActionController::TestCase
|
37
|
-
TestUser = Struct.new(:name, :admin)
|
38
|
-
|
39
37
|
class AdminUserSerializer < ActiveModel::Serializer
|
40
38
|
attributes :admin?
|
41
|
-
|
42
|
-
|
39
|
+
ActiveModelSerializers.silence_warnings do
|
40
|
+
def admin?
|
41
|
+
current_admin.admin
|
42
|
+
end
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -50,11 +50,11 @@ class SerializationScopeNameTest < ActionController::TestCase
|
|
50
50
|
before_filter { request.format = :json }
|
51
51
|
|
52
52
|
def current_admin
|
53
|
-
|
53
|
+
User.new(id: 2, name: 'Bob', admin: true)
|
54
54
|
end
|
55
55
|
|
56
56
|
def render_new_user
|
57
|
-
render :
|
57
|
+
render json: User.new(id: 1, name: 'Pete', admin: false), serializer: AdminUserSerializer, adapter: :json_api
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -62,6 +62,6 @@ class SerializationScopeNameTest < ActionController::TestCase
|
|
62
62
|
|
63
63
|
def test_override_scope_name_with_controller
|
64
64
|
get :render_new_user
|
65
|
-
assert_equal '{"
|
65
|
+
assert_equal '{"data":{"id":"1","type":"users","attributes":{"admin?":true}}}', @response.body
|
66
66
|
end
|
67
67
|
end
|
@@ -0,0 +1,420 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class ImplicitSerializerTest < ActionController::TestCase
|
6
|
+
include ActiveSupport::Testing::Stream
|
7
|
+
class ImplicitSerializationTestController < ActionController::Base
|
8
|
+
def render_using_implicit_serializer
|
9
|
+
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
10
|
+
render json: @profile
|
11
|
+
end
|
12
|
+
|
13
|
+
def render_using_default_adapter_root
|
14
|
+
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
15
|
+
render json: @profile
|
16
|
+
end
|
17
|
+
|
18
|
+
def render_array_using_custom_root
|
19
|
+
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
20
|
+
render json: [@profile], root: 'custom_root'
|
21
|
+
end
|
22
|
+
|
23
|
+
def render_array_that_is_empty_using_custom_root
|
24
|
+
render json: [], root: 'custom_root'
|
25
|
+
end
|
26
|
+
|
27
|
+
def render_object_using_custom_root
|
28
|
+
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
29
|
+
render json: @profile, root: 'custom_root'
|
30
|
+
end
|
31
|
+
|
32
|
+
def render_array_using_implicit_serializer
|
33
|
+
array = [
|
34
|
+
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
35
|
+
Profile.new(name: 'Name 2', description: 'Description 2', comments: 'Comments 2')
|
36
|
+
]
|
37
|
+
render json: array
|
38
|
+
end
|
39
|
+
|
40
|
+
def render_array_using_implicit_serializer_and_meta
|
41
|
+
@profiles = [
|
42
|
+
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
43
|
+
]
|
44
|
+
render json: @profiles, meta: { total: 10 }
|
45
|
+
end
|
46
|
+
|
47
|
+
def render_object_with_cache_enabled
|
48
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
49
|
+
@author = Author.new(id: 1, name: 'Joao Moura.')
|
50
|
+
@post = Post.new(id: 1, title: 'New Post', body: 'Body', comments: [@comment], author: @author)
|
51
|
+
|
52
|
+
generate_cached_serializer(@post)
|
53
|
+
|
54
|
+
@post.title = 'ZOMG a New Post'
|
55
|
+
render json: @post
|
56
|
+
end
|
57
|
+
|
58
|
+
def render_json_object_without_serializer
|
59
|
+
render json: { error: 'Result is Invalid' }
|
60
|
+
end
|
61
|
+
|
62
|
+
def render_json_array_object_without_serializer
|
63
|
+
render json: [{ error: 'Result is Invalid' }]
|
64
|
+
end
|
65
|
+
|
66
|
+
def update_and_render_object_with_cache_enabled
|
67
|
+
@post.updated_at = Time.now
|
68
|
+
|
69
|
+
generate_cached_serializer(@post)
|
70
|
+
render json: @post
|
71
|
+
end
|
72
|
+
|
73
|
+
def render_object_expired_with_cache_enabled
|
74
|
+
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
75
|
+
author = Author.new(id: 1, name: 'Joao Moura.')
|
76
|
+
post = Post.new(id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author)
|
77
|
+
|
78
|
+
generate_cached_serializer(post)
|
79
|
+
|
80
|
+
post.title = 'ZOMG a New Post'
|
81
|
+
sleep 0.1
|
82
|
+
render json: post
|
83
|
+
end
|
84
|
+
|
85
|
+
def render_changed_object_with_cache_enabled
|
86
|
+
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
87
|
+
author = Author.new(id: 1, name: 'Joao Moura.')
|
88
|
+
post = Post.new(id: 1, title: 'ZOMG a New Post', body: 'Body', comments: [comment], author: author)
|
89
|
+
|
90
|
+
render json: post
|
91
|
+
end
|
92
|
+
|
93
|
+
def render_fragment_changed_object_with_only_cache_enabled
|
94
|
+
author = Author.new(id: 1, name: 'Joao Moura.')
|
95
|
+
role = Role.new(id: 42, name: 'ZOMG A ROLE', description: 'DESCRIPTION HERE', author: author)
|
96
|
+
|
97
|
+
generate_cached_serializer(role)
|
98
|
+
role.name = 'lol'
|
99
|
+
role.description = 'HUEHUEBRBR'
|
100
|
+
|
101
|
+
render json: role
|
102
|
+
end
|
103
|
+
|
104
|
+
def render_fragment_changed_object_with_except_cache_enabled
|
105
|
+
author = Author.new(id: 1, name: 'Joao Moura.')
|
106
|
+
bio = Bio.new(id: 42, content: 'ZOMG A ROLE', rating: 5, author: author)
|
107
|
+
|
108
|
+
generate_cached_serializer(bio)
|
109
|
+
bio.content = 'lol'
|
110
|
+
bio.rating = 0
|
111
|
+
|
112
|
+
render json: bio
|
113
|
+
end
|
114
|
+
|
115
|
+
def render_fragment_changed_object_with_relationship
|
116
|
+
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
117
|
+
comment2 = Comment.new(id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT')
|
118
|
+
like = Like.new(id: 1, likeable: comment, time: 3.days.ago)
|
119
|
+
|
120
|
+
generate_cached_serializer(like)
|
121
|
+
like.likable = comment2
|
122
|
+
like.time = Time.zone.now.to_s
|
123
|
+
|
124
|
+
render json: like
|
125
|
+
end
|
126
|
+
|
127
|
+
private
|
128
|
+
|
129
|
+
def generate_cached_serializer(obj)
|
130
|
+
ActiveModel::SerializableResource.new(obj).to_json
|
131
|
+
end
|
132
|
+
|
133
|
+
def with_adapter(adapter)
|
134
|
+
old_adapter = ActiveModel::Serializer.config.adapter
|
135
|
+
# JSON-API adapter sets root by default
|
136
|
+
ActiveModel::Serializer.config.adapter = adapter
|
137
|
+
yield
|
138
|
+
ensure
|
139
|
+
ActiveModel::Serializer.config.adapter = old_adapter
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
tests ImplicitSerializationTestController
|
144
|
+
|
145
|
+
# We just have Null for now, this will change
|
146
|
+
def test_render_using_implicit_serializer
|
147
|
+
get :render_using_implicit_serializer
|
148
|
+
|
149
|
+
expected = {
|
150
|
+
name: 'Name 1',
|
151
|
+
description: 'Description 1'
|
152
|
+
}
|
153
|
+
|
154
|
+
assert_equal 'application/json', @response.content_type
|
155
|
+
assert_equal expected.to_json, @response.body
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_render_using_default_root
|
159
|
+
with_adapter :json_api do
|
160
|
+
get :render_using_default_adapter_root
|
161
|
+
end
|
162
|
+
expected = {
|
163
|
+
data: {
|
164
|
+
id: assigns(:profile).id.to_s,
|
165
|
+
type: 'profiles',
|
166
|
+
attributes: {
|
167
|
+
name: 'Name 1',
|
168
|
+
description: 'Description 1'
|
169
|
+
}
|
170
|
+
}
|
171
|
+
}
|
172
|
+
|
173
|
+
assert_equal 'application/json', @response.content_type
|
174
|
+
assert_equal expected.to_json, @response.body
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_render_array_using_custom_root
|
178
|
+
with_adapter :json do
|
179
|
+
get :render_array_using_custom_root
|
180
|
+
end
|
181
|
+
expected = { custom_roots: [{ name: 'Name 1', description: 'Description 1' }] }
|
182
|
+
assert_equal 'application/json', @response.content_type
|
183
|
+
assert_equal expected.to_json, @response.body
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_render_array_that_is_empty_using_custom_root
|
187
|
+
with_adapter :json do
|
188
|
+
get :render_array_that_is_empty_using_custom_root
|
189
|
+
end
|
190
|
+
|
191
|
+
expected = { custom_roots: [] }
|
192
|
+
assert_equal 'application/json', @response.content_type
|
193
|
+
assert_equal expected.to_json, @response.body
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_render_object_using_custom_root
|
197
|
+
with_adapter :json do
|
198
|
+
get :render_object_using_custom_root
|
199
|
+
end
|
200
|
+
|
201
|
+
expected = { custom_root: { name: 'Name 1', description: 'Description 1' } }
|
202
|
+
assert_equal 'application/json', @response.content_type
|
203
|
+
assert_equal expected.to_json, @response.body
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_render_json_object_without_serializer
|
207
|
+
get :render_json_object_without_serializer
|
208
|
+
|
209
|
+
assert_equal 'application/json', @response.content_type
|
210
|
+
expected_body = { error: 'Result is Invalid' }
|
211
|
+
assert_equal expected_body.to_json, @response.body
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_render_json_array_object_without_serializer
|
215
|
+
get :render_json_array_object_without_serializer
|
216
|
+
|
217
|
+
assert_equal 'application/json', @response.content_type
|
218
|
+
expected_body = [{ error: 'Result is Invalid' }]
|
219
|
+
assert_equal expected_body.to_json, @response.body
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_render_array_using_implicit_serializer
|
223
|
+
get :render_array_using_implicit_serializer
|
224
|
+
assert_equal 'application/json', @response.content_type
|
225
|
+
|
226
|
+
expected = [
|
227
|
+
{
|
228
|
+
name: 'Name 1',
|
229
|
+
description: 'Description 1'
|
230
|
+
},
|
231
|
+
{
|
232
|
+
name: 'Name 2',
|
233
|
+
description: 'Description 2'
|
234
|
+
}
|
235
|
+
]
|
236
|
+
|
237
|
+
assert_equal expected.to_json, @response.body
|
238
|
+
end
|
239
|
+
|
240
|
+
def test_render_array_using_implicit_serializer_and_meta
|
241
|
+
with_adapter :json_api do
|
242
|
+
get :render_array_using_implicit_serializer_and_meta
|
243
|
+
end
|
244
|
+
expected = {
|
245
|
+
data: [
|
246
|
+
{
|
247
|
+
id: assigns(:profiles).first.id.to_s,
|
248
|
+
type: 'profiles',
|
249
|
+
attributes: {
|
250
|
+
name: 'Name 1',
|
251
|
+
description: 'Description 1'
|
252
|
+
}
|
253
|
+
}
|
254
|
+
],
|
255
|
+
meta: {
|
256
|
+
total: 10
|
257
|
+
}
|
258
|
+
}
|
259
|
+
|
260
|
+
assert_equal 'application/json', @response.content_type
|
261
|
+
assert_equal expected.to_json, @response.body
|
262
|
+
end
|
263
|
+
|
264
|
+
def test_render_with_cache_enable
|
265
|
+
expected = {
|
266
|
+
id: 1,
|
267
|
+
title: 'New Post',
|
268
|
+
body: 'Body',
|
269
|
+
comments: [
|
270
|
+
{
|
271
|
+
id: 1,
|
272
|
+
body: 'ZOMG A COMMENT' }
|
273
|
+
],
|
274
|
+
blog: {
|
275
|
+
id: 999,
|
276
|
+
name: 'Custom blog'
|
277
|
+
},
|
278
|
+
author: {
|
279
|
+
id: 1,
|
280
|
+
name: 'Joao Moura.'
|
281
|
+
}
|
282
|
+
}
|
283
|
+
|
284
|
+
ActionController::Base.cache_store.clear
|
285
|
+
Timecop.freeze(Time.zone.now) do
|
286
|
+
get :render_object_with_cache_enabled
|
287
|
+
|
288
|
+
assert_equal 'application/json', @response.content_type
|
289
|
+
assert_equal expected.to_json, @response.body
|
290
|
+
|
291
|
+
get :render_changed_object_with_cache_enabled
|
292
|
+
assert_equal expected.to_json, @response.body
|
293
|
+
end
|
294
|
+
|
295
|
+
ActionController::Base.cache_store.clear
|
296
|
+
get :render_changed_object_with_cache_enabled
|
297
|
+
assert_not_equal expected.to_json, @response.body
|
298
|
+
end
|
299
|
+
|
300
|
+
def test_render_with_cache_enable_and_expired
|
301
|
+
ActionController::Base.cache_store.clear
|
302
|
+
get :render_object_expired_with_cache_enabled
|
303
|
+
|
304
|
+
expected = {
|
305
|
+
id: 1,
|
306
|
+
title: 'ZOMG a New Post',
|
307
|
+
body: 'Body',
|
308
|
+
comments: [
|
309
|
+
{
|
310
|
+
id: 1,
|
311
|
+
body: 'ZOMG A COMMENT' }
|
312
|
+
],
|
313
|
+
blog: {
|
314
|
+
id: 999,
|
315
|
+
name: 'Custom blog'
|
316
|
+
},
|
317
|
+
author: {
|
318
|
+
id: 1,
|
319
|
+
name: 'Joao Moura.'
|
320
|
+
}
|
321
|
+
}
|
322
|
+
|
323
|
+
assert_equal 'application/json', @response.content_type
|
324
|
+
assert_equal expected.to_json, @response.body
|
325
|
+
end
|
326
|
+
|
327
|
+
def test_render_with_fragment_only_cache_enable
|
328
|
+
ActionController::Base.cache_store.clear
|
329
|
+
get :render_fragment_changed_object_with_only_cache_enabled
|
330
|
+
response = JSON.parse(@response.body)
|
331
|
+
|
332
|
+
assert_equal 'application/json', @response.content_type
|
333
|
+
assert_equal 'ZOMG A ROLE', response['name']
|
334
|
+
assert_equal 'HUEHUEBRBR', response['description']
|
335
|
+
end
|
336
|
+
|
337
|
+
def test_render_with_fragment_except_cache_enable
|
338
|
+
ActionController::Base.cache_store.clear
|
339
|
+
get :render_fragment_changed_object_with_except_cache_enabled
|
340
|
+
response = JSON.parse(@response.body)
|
341
|
+
|
342
|
+
assert_equal 'application/json', @response.content_type
|
343
|
+
assert_equal 5, response['rating']
|
344
|
+
assert_equal 'lol', response['content']
|
345
|
+
end
|
346
|
+
|
347
|
+
def test_render_fragment_changed_object_with_relationship
|
348
|
+
ActionController::Base.cache_store.clear
|
349
|
+
|
350
|
+
Timecop.freeze(Time.zone.now) do
|
351
|
+
get :render_fragment_changed_object_with_relationship
|
352
|
+
response = JSON.parse(@response.body)
|
353
|
+
|
354
|
+
expected_return = {
|
355
|
+
'id' => 1,
|
356
|
+
'time' => Time.zone.now.to_s,
|
357
|
+
'likeable' => {
|
358
|
+
'id' => 1,
|
359
|
+
'body' => 'ZOMG A COMMENT'
|
360
|
+
}
|
361
|
+
}
|
362
|
+
|
363
|
+
assert_equal 'application/json', @response.content_type
|
364
|
+
assert_equal expected_return, response
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
def test_cache_expiration_on_update
|
369
|
+
ActionController::Base.cache_store.clear
|
370
|
+
get :render_object_with_cache_enabled
|
371
|
+
|
372
|
+
expected = {
|
373
|
+
id: 1,
|
374
|
+
title: 'ZOMG a New Post',
|
375
|
+
body: 'Body',
|
376
|
+
comments: [
|
377
|
+
{
|
378
|
+
id: 1,
|
379
|
+
body: 'ZOMG A COMMENT' }
|
380
|
+
],
|
381
|
+
blog: {
|
382
|
+
id: 999,
|
383
|
+
name: 'Custom blog'
|
384
|
+
},
|
385
|
+
author: {
|
386
|
+
id: 1,
|
387
|
+
name: 'Joao Moura.'
|
388
|
+
}
|
389
|
+
}
|
390
|
+
|
391
|
+
get :update_and_render_object_with_cache_enabled
|
392
|
+
|
393
|
+
assert_equal 'application/json', @response.content_type
|
394
|
+
assert_equal expected.to_json, @response.body
|
395
|
+
end
|
396
|
+
|
397
|
+
def test_warn_overridding_use_adapter_as_falsy_on_controller_instance
|
398
|
+
controller = Class.new(ImplicitSerializationTestController) {
|
399
|
+
def use_adapter?
|
400
|
+
false
|
401
|
+
end
|
402
|
+
}.new
|
403
|
+
assert_match(/adapter: false/, (capture(:stderr) {
|
404
|
+
controller.get_serializer(Profile.new)
|
405
|
+
}))
|
406
|
+
end
|
407
|
+
|
408
|
+
def test_dont_warn_overridding_use_adapter_as_truthy_on_controller_instance
|
409
|
+
controller = Class.new(ImplicitSerializationTestController) {
|
410
|
+
def use_adapter?
|
411
|
+
true
|
412
|
+
end
|
413
|
+
}.new
|
414
|
+
assert_equal '', (capture(:stderr) {
|
415
|
+
controller.get_serializer(Profile.new)
|
416
|
+
})
|
417
|
+
end
|
418
|
+
end
|
419
|
+
end
|
420
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
module ActiveModel
|
3
|
+
class Serializer
|
4
|
+
class Adapter
|
5
|
+
class FragmentCacheTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@spam = Spam::UnrelatedLink.new(id: 'spam-id-1')
|
8
|
+
@author = Author.new(name: 'Joao M. D. Moura')
|
9
|
+
@role = Role.new(name: 'Great Author', description: nil)
|
10
|
+
@role.author = [@author]
|
11
|
+
@role_serializer = RoleSerializer.new(@role)
|
12
|
+
@spam_serializer = Spam::UnrelatedLinkSerializer.new(@spam)
|
13
|
+
@role_hash = FragmentCache.new(RoleSerializer.adapter.new(@role_serializer), @role_serializer, {})
|
14
|
+
@spam_hash = FragmentCache.new(Spam::UnrelatedLinkSerializer.adapter.new(@spam_serializer), @spam_serializer, {})
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_fragment_fetch_with_virtual_attributes
|
18
|
+
expected_result = {
|
19
|
+
id: @role.id,
|
20
|
+
description: @role.description,
|
21
|
+
slug: "#{@role.name}-#{@role.id}",
|
22
|
+
name: @role.name
|
23
|
+
}
|
24
|
+
assert_equal(@role_hash.fetch, expected_result)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_fragment_fetch_with_namespaced_object
|
28
|
+
expected_result = {
|
29
|
+
id: @spam.id
|
30
|
+
}
|
31
|
+
assert_equal(@spam_hash.fetch, expected_result)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class Adapter
|
6
|
+
class Json
|
7
|
+
class BelongsToTest < Minitest::Test
|
8
|
+
def setup
|
9
|
+
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
10
|
+
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
|
11
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
12
|
+
@post.comments = [@comment]
|
13
|
+
@anonymous_post.comments = []
|
14
|
+
@comment.post = @post
|
15
|
+
@comment.author = nil
|
16
|
+
@anonymous_post.author = nil
|
17
|
+
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
18
|
+
@post.blog = @blog
|
19
|
+
@anonymous_post.blog = nil
|
20
|
+
|
21
|
+
@serializer = CommentSerializer.new(@comment)
|
22
|
+
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
23
|
+
ActionController::Base.cache_store.clear
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_includes_post
|
27
|
+
assert_equal({ id: 42, title: 'New Post', body: 'Body' }, @adapter.serializable_hash[:comment][:post])
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_include_nil_author
|
31
|
+
serializer = PostSerializer.new(@anonymous_post)
|
32
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
33
|
+
|
34
|
+
assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], blog: { id: 999, name: 'Custom blog' }, author: nil } }, adapter.serializable_hash)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_include_nil_author_with_specified_serializer
|
38
|
+
serializer = PostPreviewSerializer.new(@anonymous_post)
|
39
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
40
|
+
|
41
|
+
assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], author: nil } }, adapter.serializable_hash)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class Adapter
|
6
|
+
class Json
|
7
|
+
class Collection < Minitest::Test
|
8
|
+
def setup
|
9
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
10
|
+
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
11
|
+
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
12
|
+
@first_post.comments = []
|
13
|
+
@second_post.comments = []
|
14
|
+
@first_post.author = @author
|
15
|
+
@second_post.author = @author
|
16
|
+
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
17
|
+
@first_post.blog = @blog
|
18
|
+
@second_post.blog = nil
|
19
|
+
|
20
|
+
ActionController::Base.cache_store.clear
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_with_serializer_option
|
24
|
+
@blog.special_attribute = 'Special'
|
25
|
+
@blog.articles = [@first_post, @second_post]
|
26
|
+
serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
|
27
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
28
|
+
|
29
|
+
expected = { blogs: [{
|
30
|
+
id: 1,
|
31
|
+
special_attribute: 'Special',
|
32
|
+
articles: [{ id: 1, title: 'Hello!!', body: 'Hello, world!!' }, { id: 2, title: 'New Post', body: 'Body' }]
|
33
|
+
}] }
|
34
|
+
assert_equal expected, adapter.serializable_hash
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_include_multiple_posts
|
38
|
+
serializer = ArraySerializer.new([@first_post, @second_post])
|
39
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
40
|
+
|
41
|
+
expected = { posts: [{
|
42
|
+
title: 'Hello!!',
|
43
|
+
body: 'Hello, world!!',
|
44
|
+
id: 1,
|
45
|
+
comments: [],
|
46
|
+
author: {
|
47
|
+
id: 1,
|
48
|
+
name: 'Steve K.'
|
49
|
+
},
|
50
|
+
blog: {
|
51
|
+
id: 999,
|
52
|
+
name: 'Custom blog'
|
53
|
+
}
|
54
|
+
}, {
|
55
|
+
title: 'New Post',
|
56
|
+
body: 'Body',
|
57
|
+
id: 2,
|
58
|
+
comments: [],
|
59
|
+
author: {
|
60
|
+
id: 1,
|
61
|
+
name: 'Steve K.'
|
62
|
+
},
|
63
|
+
blog: {
|
64
|
+
id: 999,
|
65
|
+
name: 'Custom blog'
|
66
|
+
}
|
67
|
+
}] }
|
68
|
+
assert_equal expected, adapter.serializable_hash
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_root_is_underscored
|
72
|
+
virtual_value = VirtualValue.new(id: 1)
|
73
|
+
serializer = ArraySerializer.new([virtual_value])
|
74
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
75
|
+
|
76
|
+
assert_equal 1, adapter.serializable_hash[:virtual_values].length
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|