active_model_serializers 0.8.3 → 0.10.0.rc5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +7 -0
- data/.rubocop.yml +104 -0
- data/.rubocop_todo.yml +167 -0
- data/.simplecov +110 -0
- data/.travis.yml +46 -23
- data/CHANGELOG.md +442 -6
- data/CONTRIBUTING.md +95 -0
- data/Gemfile +51 -1
- data/{MIT-LICENSE.txt → MIT-LICENSE} +3 -2
- data/README.md +102 -590
- data/Rakefile +93 -8
- data/active_model_serializers.gemspec +65 -23
- data/appveyor.yml +28 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/serve_benchmark +39 -0
- data/docs/ARCHITECTURE.md +126 -0
- data/docs/README.md +39 -0
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +245 -0
- data/docs/general/caching.md +52 -0
- data/docs/general/configuration_options.md +100 -0
- data/docs/general/deserialization.md +100 -0
- data/docs/general/getting_started.md +133 -0
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/logging.md +14 -0
- data/docs/general/rendering.md +255 -0
- data/docs/general/serializers.md +339 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +139 -0
- data/docs/howto/add_root_key.md +51 -0
- data/docs/howto/outside_controller_use.md +58 -0
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/howto/test.md +152 -0
- data/docs/integrations/ember-and-json-api.md +112 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/docs/jsonapi/schema.md +151 -0
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +31 -36
- data/lib/active_model/serializable_resource.rb +11 -0
- data/lib/active_model/serializer/adapter/attributes.rb +15 -0
- data/lib/active_model/serializer/adapter/base.rb +16 -0
- data/lib/active_model/serializer/adapter/json.rb +15 -0
- data/lib/active_model/serializer/adapter/json_api.rb +15 -0
- data/lib/active_model/serializer/adapter/null.rb +15 -0
- data/lib/active_model/serializer/adapter.rb +24 -0
- data/lib/active_model/serializer/array_serializer.rb +9 -0
- data/lib/active_model/serializer/association.rb +19 -0
- data/lib/active_model/serializer/associations.rb +87 -220
- data/lib/active_model/serializer/attribute.rb +25 -0
- data/lib/active_model/serializer/attributes.rb +82 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
- data/lib/active_model/serializer/caching.rb +151 -0
- data/lib/active_model/serializer/collection_reflection.rb +7 -0
- data/lib/active_model/serializer/collection_serializer.rb +64 -0
- data/lib/active_model/serializer/configuration.rb +35 -0
- data/lib/active_model/serializer/error_serializer.rb +10 -0
- data/lib/active_model/serializer/errors_serializer.rb +27 -0
- data/lib/active_model/serializer/field.rb +56 -0
- data/lib/active_model/serializer/fieldset.rb +31 -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/include_tree.rb +111 -0
- data/lib/active_model/serializer/links.rb +35 -0
- data/lib/active_model/serializer/lint.rb +156 -0
- data/lib/active_model/serializer/meta.rb +29 -0
- data/lib/active_model/serializer/null.rb +17 -0
- data/lib/active_model/serializer/reflection.rb +147 -0
- data/lib/active_model/serializer/singular_reflection.rb +7 -0
- data/lib/active_model/serializer/type.rb +25 -0
- data/lib/active_model/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +156 -481
- data/lib/active_model_serializers/adapter/attributes.rb +94 -0
- data/lib/active_model_serializers/adapter/base.rb +90 -0
- data/lib/active_model_serializers/adapter/json.rb +11 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +57 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
- data/lib/active_model_serializers/adapter/json_api.rb +513 -0
- data/lib/active_model_serializers/adapter/null.rb +10 -0
- data/lib/active_model_serializers/adapter.rb +92 -0
- data/lib/active_model_serializers/cached_serializer.rb +87 -0
- data/lib/active_model_serializers/callbacks.rb +55 -0
- data/lib/active_model_serializers/deprecate.rb +55 -0
- data/lib/active_model_serializers/deserialization.rb +13 -0
- data/lib/active_model_serializers/fragment_cache.rb +118 -0
- data/lib/active_model_serializers/json_pointer.rb +14 -0
- data/lib/active_model_serializers/key_transform.rb +70 -0
- data/lib/active_model_serializers/logging.rb +122 -0
- data/lib/active_model_serializers/model.rb +49 -0
- data/lib/active_model_serializers/railtie.rb +46 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
- data/lib/active_model_serializers/serializable_resource.rb +81 -0
- data/lib/active_model_serializers/serialization_context.rb +32 -0
- data/lib/active_model_serializers/test/schema.rb +103 -0
- data/lib/active_model_serializers/test/serializer.rb +125 -0
- data/lib/active_model_serializers/test.rb +7 -0
- data/lib/active_model_serializers.rb +34 -89
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +10 -0
- data/lib/generators/rails/serializer_generator.rb +36 -0
- data/lib/generators/rails/templates/serializer.rb.erb +8 -0
- data/lib/grape/active_model_serializers.rb +14 -0
- data/lib/grape/formatters/active_model_serializers.rb +15 -0
- data/lib/grape/helpers/active_model_serializers.rb +16 -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/include_test.rb +167 -0
- data/test/action_controller/json_api/deserialization_test.rb +112 -0
- data/test/action_controller/json_api/errors_test.rb +41 -0
- data/test/action_controller/json_api/linked_test.rb +197 -0
- data/test/action_controller/json_api/pagination_test.rb +116 -0
- data/test/action_controller/json_api/transform_test.rb +180 -0
- data/test/action_controller/serialization_scope_name_test.rb +229 -0
- data/test/action_controller/serialization_test.rb +467 -0
- data/test/active_model_serializers/adapter_for_test.rb +208 -0
- data/test/active_model_serializers/cached_serializer_test.rb +80 -0
- data/test/active_model_serializers/fragment_cache_test.rb +34 -0
- data/test/active_model_serializers/json_pointer_test.rb +20 -0
- data/test/active_model_serializers/key_transform_test.rb +263 -0
- data/test/active_model_serializers/logging_test.rb +77 -0
- data/test/active_model_serializers/model_test.rb +9 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +63 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
- data/test/active_model_serializers/test/schema_test.rb +128 -0
- data/test/active_model_serializers/test/serializer_test.rb +63 -0
- data/test/active_record_test.rb +9 -0
- data/test/adapter/deprecation_test.rb +100 -0
- data/test/adapter/json/belongs_to_test.rb +45 -0
- data/test/adapter/json/collection_test.rb +90 -0
- data/test/adapter/json/has_many_test.rb +45 -0
- data/test/adapter/json/transform_test.rb +93 -0
- data/test/adapter/json_api/belongs_to_test.rb +155 -0
- data/test/adapter/json_api/collection_test.rb +95 -0
- data/test/adapter/json_api/errors_test.rb +78 -0
- data/test/adapter/json_api/fields_test.rb +87 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +43 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +96 -0
- data/test/adapter/json_api/has_many_test.rb +143 -0
- data/test/adapter/json_api/has_one_test.rb +79 -0
- data/test/adapter/json_api/json_api_test.rb +35 -0
- data/test/adapter/json_api/linked_test.rb +392 -0
- data/test/adapter/json_api/links_test.rb +93 -0
- data/test/adapter/json_api/pagination_links_test.rb +148 -0
- data/test/adapter/json_api/parse_test.rb +137 -0
- data/test/adapter/json_api/relationship_test.rb +161 -0
- data/test/adapter/json_api/relationships_test.rb +199 -0
- data/test/adapter/json_api/resource_identifier_test.rb +85 -0
- data/test/adapter/json_api/resource_meta_test.rb +100 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +82 -0
- data/test/adapter/json_api/transform_test.rb +500 -0
- data/test/adapter/json_api/type_test.rb +61 -0
- data/test/adapter/json_test.rb +45 -0
- data/test/adapter/null_test.rb +23 -0
- data/test/adapter/polymorphic_test.rb +72 -0
- data/test/adapter_test.rb +40 -0
- data/test/array_serializer_test.rb +35 -73
- data/test/benchmark/app.rb +65 -0
- data/test/benchmark/benchmarking_support.rb +67 -0
- data/test/benchmark/bm_caching.rb +117 -0
- data/test/benchmark/bm_transform.rb +34 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +77 -0
- data/test/benchmark/fixtures.rb +167 -0
- data/test/cache_test.rb +388 -0
- data/test/collection_serializer_test.rb +110 -0
- data/test/fixtures/active_record.rb +68 -0
- data/test/fixtures/poro.rb +254 -0
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/generators/serializer_generator_test.rb +57 -0
- data/test/grape_test.rb +82 -0
- data/test/include_tree/from_include_args_test.rb +26 -0
- data/test/include_tree/from_string_test.rb +94 -0
- data/test/include_tree/include_args_to_hash_test.rb +64 -0
- data/test/lint_test.rb +49 -0
- data/test/logger_test.rb +18 -0
- data/test/poro_test.rb +9 -0
- data/test/serializable_resource_test.rb +83 -0
- data/test/serializers/association_macros_test.rb +36 -0
- data/test/serializers/associations_test.rb +267 -0
- data/test/serializers/attribute_test.rb +123 -0
- data/test/serializers/attributes_test.rb +52 -0
- data/test/serializers/caching_configuration_test_isolated.rb +170 -0
- data/test/serializers/configuration_test.rb +32 -0
- data/test/serializers/fieldset_test.rb +14 -0
- data/test/serializers/meta_test.rb +198 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
- data/test/serializers/root_test.rb +21 -0
- data/test/serializers/serialization_test.rb +55 -0
- data/test/serializers/serializer_for_test.rb +134 -0
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +80 -0
- data/test/support/rails5_shims.rb +47 -0
- data/test/support/rails_app.rb +45 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
- data/test/support/schemas/custom/show.json +7 -0
- data/test/support/schemas/hyper_schema.json +93 -0
- data/test/support/schemas/render_using_json_api.json +43 -0
- data/test/support/schemas/simple_json_pointers.json +10 -0
- data/test/support/serialization_testing.rb +53 -0
- data/test/test_helper.rb +51 -24
- metadata +456 -45
- 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/USAGE +0 -9
- data/lib/generators/serializer/serializer_generator.rb +0 -42
- 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_scope_name_test.rb +0 -67
- data/test/serialization_test.rb +0 -392
- data/test/serializer_support_test.rb +0 -51
- data/test/serializer_test.rb +0 -1465
- data/test/test_fakes.rb +0 -217
@@ -0,0 +1,229 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module SerializationScopeTesting
|
4
|
+
class User < ActiveModelSerializers::Model
|
5
|
+
attr_accessor :id, :name, :admin
|
6
|
+
def admin?
|
7
|
+
admin
|
8
|
+
end
|
9
|
+
end
|
10
|
+
class Comment < ActiveModelSerializers::Model
|
11
|
+
attr_accessor :id, :body
|
12
|
+
end
|
13
|
+
class Post < ActiveModelSerializers::Model
|
14
|
+
attr_accessor :id, :title, :body, :comments
|
15
|
+
end
|
16
|
+
class PostSerializer < ActiveModel::Serializer
|
17
|
+
attributes :id, :title, :body, :comments
|
18
|
+
|
19
|
+
def body
|
20
|
+
"The 'scope' is the 'current_user': #{scope == current_user}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def comments
|
24
|
+
if current_user.admin?
|
25
|
+
[Comment.new(id: 1, body: 'Admin')]
|
26
|
+
else
|
27
|
+
[Comment.new(id: 2, body: 'Scoped')]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def json_key
|
32
|
+
'post'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
class PostTestController < ActionController::Base
|
36
|
+
attr_accessor :current_user
|
37
|
+
def render_post_by_non_admin
|
38
|
+
self.current_user = User.new(id: 3, name: 'Pete', admin: false)
|
39
|
+
render json: new_post, serializer: serializer, adapter: :json
|
40
|
+
end
|
41
|
+
|
42
|
+
def render_post_by_admin
|
43
|
+
self.current_user = User.new(id: 3, name: 'Pete', admin: true)
|
44
|
+
render json: new_post, serializer: serializer, adapter: :json
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def new_post
|
50
|
+
Post.new(id: 4, title: 'Title')
|
51
|
+
end
|
52
|
+
|
53
|
+
def serializer
|
54
|
+
PostSerializer
|
55
|
+
end
|
56
|
+
end
|
57
|
+
class PostViewContextSerializer < PostSerializer
|
58
|
+
def body
|
59
|
+
"The 'scope' is the 'view_context': #{scope == view_context}"
|
60
|
+
end
|
61
|
+
|
62
|
+
def comments
|
63
|
+
if view_context.controller.current_user.admin?
|
64
|
+
[Comment.new(id: 1, body: 'Admin')]
|
65
|
+
else
|
66
|
+
[Comment.new(id: 2, body: 'Scoped')]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
class DefaultScopeTest < ActionController::TestCase
|
71
|
+
tests PostTestController
|
72
|
+
|
73
|
+
def test_default_serialization_scope
|
74
|
+
assert_equal :current_user, @controller._serialization_scope
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_default_serialization_scope_object
|
78
|
+
assert_equal @controller.current_user, @controller.serialization_scope
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_default_scope_non_admin
|
82
|
+
get :render_post_by_non_admin
|
83
|
+
expected_json = {
|
84
|
+
post: {
|
85
|
+
id: 4,
|
86
|
+
title: 'Title',
|
87
|
+
body: "The 'scope' is the 'current_user': true",
|
88
|
+
comments: [
|
89
|
+
{ id: 2, body: 'Scoped' }
|
90
|
+
]
|
91
|
+
}
|
92
|
+
}.to_json
|
93
|
+
assert_equal expected_json, @response.body
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_default_scope_admin
|
97
|
+
get :render_post_by_admin
|
98
|
+
expected_json = {
|
99
|
+
post: {
|
100
|
+
id: 4,
|
101
|
+
title: 'Title',
|
102
|
+
body: "The 'scope' is the 'current_user': true",
|
103
|
+
comments: [
|
104
|
+
{ id: 1, body: 'Admin' }
|
105
|
+
]
|
106
|
+
}
|
107
|
+
}.to_json
|
108
|
+
assert_equal expected_json, @response.body
|
109
|
+
end
|
110
|
+
end
|
111
|
+
class SerializationScopeTest < ActionController::TestCase
|
112
|
+
class PostViewContextTestController < PostTestController
|
113
|
+
serialization_scope :view_context
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
def serializer
|
118
|
+
PostViewContextSerializer
|
119
|
+
end
|
120
|
+
end
|
121
|
+
tests PostViewContextTestController
|
122
|
+
|
123
|
+
def test_defined_serialization_scope
|
124
|
+
assert_equal :view_context, @controller._serialization_scope
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_defined_serialization_scope_object
|
128
|
+
assert_equal @controller.view_context.class, @controller.serialization_scope.class
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_serialization_scope_non_admin
|
132
|
+
get :render_post_by_non_admin
|
133
|
+
expected_json = {
|
134
|
+
post: {
|
135
|
+
id: 4,
|
136
|
+
title: 'Title',
|
137
|
+
body: "The 'scope' is the 'view_context': true",
|
138
|
+
comments: [
|
139
|
+
{ id: 2, body: 'Scoped' }
|
140
|
+
]
|
141
|
+
}
|
142
|
+
}.to_json
|
143
|
+
assert_equal expected_json, @response.body
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_serialization_scope_admin
|
147
|
+
get :render_post_by_admin
|
148
|
+
expected_json = {
|
149
|
+
post: {
|
150
|
+
id: 4,
|
151
|
+
title: 'Title',
|
152
|
+
body: "The 'scope' is the 'view_context': true",
|
153
|
+
comments: [
|
154
|
+
{ id: 1, body: 'Admin' }
|
155
|
+
]
|
156
|
+
}
|
157
|
+
}.to_json
|
158
|
+
assert_equal expected_json, @response.body
|
159
|
+
end
|
160
|
+
end
|
161
|
+
class NilSerializationScopeTest < ActionController::TestCase
|
162
|
+
class PostViewContextTestController < ActionController::Base
|
163
|
+
serialization_scope nil
|
164
|
+
|
165
|
+
attr_accessor :current_user
|
166
|
+
|
167
|
+
def render_post_with_no_scope
|
168
|
+
self.current_user = User.new(id: 3, name: 'Pete', admin: false)
|
169
|
+
render json: new_post, serializer: PostSerializer, adapter: :json
|
170
|
+
end
|
171
|
+
|
172
|
+
def render_post_with_passed_in_scope
|
173
|
+
self.current_user = User.new(id: 3, name: 'Pete', admin: false)
|
174
|
+
render json: new_post, serializer: PostSerializer, adapter: :json, scope: current_user, scope_name: :current_user
|
175
|
+
end
|
176
|
+
|
177
|
+
def render_post_with_passed_in_scope_without_scope_name
|
178
|
+
self.current_user = User.new(id: 3, name: 'Pete', admin: false)
|
179
|
+
render json: new_post, serializer: PostSerializer, adapter: :json, scope: current_user
|
180
|
+
end
|
181
|
+
|
182
|
+
private
|
183
|
+
|
184
|
+
def new_post
|
185
|
+
Post.new(id: 4, title: 'Title')
|
186
|
+
end
|
187
|
+
end
|
188
|
+
tests PostViewContextTestController
|
189
|
+
|
190
|
+
def test_nil_serialization_scope
|
191
|
+
assert_nil @controller._serialization_scope
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_nil_serialization_scope_object
|
195
|
+
assert_nil @controller.serialization_scope
|
196
|
+
end
|
197
|
+
|
198
|
+
def test_nil_scope
|
199
|
+
exception_matcher = /current_user/
|
200
|
+
exception = assert_raises(NameError) do
|
201
|
+
get :render_post_with_no_scope
|
202
|
+
end
|
203
|
+
assert_match exception_matcher, exception.message
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_serialization_scope_is_and_nil_scope_passed_in_current_user
|
207
|
+
get :render_post_with_passed_in_scope
|
208
|
+
expected_json = {
|
209
|
+
post: {
|
210
|
+
id: 4,
|
211
|
+
title: 'Title',
|
212
|
+
body: "The 'scope' is the 'current_user': true",
|
213
|
+
comments: [
|
214
|
+
{ id: 2, body: 'Scoped' }
|
215
|
+
]
|
216
|
+
}
|
217
|
+
}.to_json
|
218
|
+
assert_equal expected_json, @response.body
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_serialization_scope_is_nil_and_scope_passed_in_current_user_without_scope_name
|
222
|
+
exception_matcher = /current_user/
|
223
|
+
exception = assert_raises(NameError) do
|
224
|
+
get :render_post_with_passed_in_scope_without_scope_name
|
225
|
+
end
|
226
|
+
assert_match exception_matcher, exception.message
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
@@ -0,0 +1,467 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class ImplicitSerializerTest < ActionController::TestCase
|
6
|
+
class ImplicitSerializationTestController < ActionController::Base
|
7
|
+
include SerializationTesting
|
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_array_using_implicit_serializer_and_links
|
48
|
+
with_adapter ActiveModelSerializers::Adapter::JsonApi do
|
49
|
+
@profiles = [
|
50
|
+
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
51
|
+
]
|
52
|
+
|
53
|
+
render json: @profiles, links: { self: 'http://example.com/api/profiles/1' }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def render_object_with_cache_enabled
|
58
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
59
|
+
@author = Author.new(id: 1, name: 'Joao Moura.')
|
60
|
+
@post = Post.new(id: 1, title: 'New Post', body: 'Body', comments: [@comment], author: @author)
|
61
|
+
|
62
|
+
generate_cached_serializer(@post)
|
63
|
+
|
64
|
+
@post.title = 'ZOMG a New Post'
|
65
|
+
render json: @post
|
66
|
+
end
|
67
|
+
|
68
|
+
def render_json_object_without_serializer
|
69
|
+
render json: { error: 'Result is Invalid' }
|
70
|
+
end
|
71
|
+
|
72
|
+
def render_json_array_object_without_serializer
|
73
|
+
render json: [{ error: 'Result is Invalid' }]
|
74
|
+
end
|
75
|
+
|
76
|
+
def update_and_render_object_with_cache_enabled
|
77
|
+
@post.updated_at = Time.now
|
78
|
+
|
79
|
+
generate_cached_serializer(@post)
|
80
|
+
render json: @post
|
81
|
+
end
|
82
|
+
|
83
|
+
def render_object_expired_with_cache_enabled
|
84
|
+
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
85
|
+
author = Author.new(id: 1, name: 'Joao Moura.')
|
86
|
+
post = Post.new(id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author)
|
87
|
+
|
88
|
+
generate_cached_serializer(post)
|
89
|
+
|
90
|
+
post.title = 'ZOMG a New Post'
|
91
|
+
|
92
|
+
expires_in = [
|
93
|
+
PostSerializer._cache_options[:expires_in],
|
94
|
+
CommentSerializer._cache_options[:expires_in]
|
95
|
+
].max + 200
|
96
|
+
|
97
|
+
Timecop.travel(Time.zone.now + expires_in) do
|
98
|
+
render json: post
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def render_changed_object_with_cache_enabled
|
103
|
+
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
104
|
+
author = Author.new(id: 1, name: 'Joao Moura.')
|
105
|
+
post = Post.new(id: 1, title: 'ZOMG a New Post', body: 'Body', comments: [comment], author: author)
|
106
|
+
|
107
|
+
render json: post
|
108
|
+
end
|
109
|
+
|
110
|
+
def render_fragment_changed_object_with_only_cache_enabled
|
111
|
+
author = Author.new(id: 1, name: 'Joao Moura.')
|
112
|
+
role = Role.new(id: 42, name: 'ZOMG A ROLE', description: 'DESCRIPTION HERE', author: author)
|
113
|
+
|
114
|
+
generate_cached_serializer(role)
|
115
|
+
role.name = 'lol'
|
116
|
+
role.description = 'HUEHUEBRBR'
|
117
|
+
|
118
|
+
render json: role
|
119
|
+
end
|
120
|
+
|
121
|
+
def render_fragment_changed_object_with_except_cache_enabled
|
122
|
+
author = Author.new(id: 1, name: 'Joao Moura.')
|
123
|
+
bio = Bio.new(id: 42, content: 'ZOMG A ROLE', rating: 5, author: author)
|
124
|
+
|
125
|
+
generate_cached_serializer(bio)
|
126
|
+
bio.content = 'lol'
|
127
|
+
bio.rating = 0
|
128
|
+
|
129
|
+
render json: bio
|
130
|
+
end
|
131
|
+
|
132
|
+
def render_fragment_changed_object_with_relationship
|
133
|
+
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
134
|
+
comment2 = Comment.new(id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT')
|
135
|
+
like = Like.new(id: 1, likeable: comment, time: 3.days.ago)
|
136
|
+
|
137
|
+
generate_cached_serializer(like)
|
138
|
+
like.likable = comment2
|
139
|
+
like.time = Time.zone.now.to_s
|
140
|
+
|
141
|
+
render json: like
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
tests ImplicitSerializationTestController
|
146
|
+
|
147
|
+
# We just have Null for now, this will change
|
148
|
+
def test_render_using_implicit_serializer
|
149
|
+
get :render_using_implicit_serializer
|
150
|
+
|
151
|
+
expected = {
|
152
|
+
name: 'Name 1',
|
153
|
+
description: 'Description 1'
|
154
|
+
}
|
155
|
+
|
156
|
+
assert_equal 'application/json', @response.content_type
|
157
|
+
assert_equal expected.to_json, @response.body
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_render_using_default_root
|
161
|
+
with_adapter :json_api do
|
162
|
+
get :render_using_default_adapter_root
|
163
|
+
end
|
164
|
+
expected = {
|
165
|
+
data: {
|
166
|
+
id: assigns(:profile).id.to_s,
|
167
|
+
type: 'profiles',
|
168
|
+
attributes: {
|
169
|
+
name: 'Name 1',
|
170
|
+
description: 'Description 1'
|
171
|
+
}
|
172
|
+
}
|
173
|
+
}
|
174
|
+
|
175
|
+
assert_equal 'application/json', @response.content_type
|
176
|
+
assert_equal expected.to_json, @response.body
|
177
|
+
end
|
178
|
+
|
179
|
+
def test_render_array_using_custom_root
|
180
|
+
with_adapter :json do
|
181
|
+
get :render_array_using_custom_root
|
182
|
+
end
|
183
|
+
expected = { custom_root: [{ name: 'Name 1', description: 'Description 1' }] }
|
184
|
+
assert_equal 'application/json', @response.content_type
|
185
|
+
assert_equal expected.to_json, @response.body
|
186
|
+
end
|
187
|
+
|
188
|
+
def test_render_array_that_is_empty_using_custom_root
|
189
|
+
with_adapter :json do
|
190
|
+
get :render_array_that_is_empty_using_custom_root
|
191
|
+
end
|
192
|
+
|
193
|
+
expected = { custom_root: [] }
|
194
|
+
assert_equal 'application/json', @response.content_type
|
195
|
+
assert_equal expected.to_json, @response.body
|
196
|
+
end
|
197
|
+
|
198
|
+
def test_render_object_using_custom_root
|
199
|
+
with_adapter :json do
|
200
|
+
get :render_object_using_custom_root
|
201
|
+
end
|
202
|
+
|
203
|
+
expected = { custom_root: { name: 'Name 1', description: 'Description 1' } }
|
204
|
+
assert_equal 'application/json', @response.content_type
|
205
|
+
assert_equal expected.to_json, @response.body
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_render_json_object_without_serializer
|
209
|
+
get :render_json_object_without_serializer
|
210
|
+
|
211
|
+
assert_equal 'application/json', @response.content_type
|
212
|
+
expected_body = { error: 'Result is Invalid' }
|
213
|
+
assert_equal expected_body.to_json, @response.body
|
214
|
+
end
|
215
|
+
|
216
|
+
def test_render_json_array_object_without_serializer
|
217
|
+
get :render_json_array_object_without_serializer
|
218
|
+
|
219
|
+
assert_equal 'application/json', @response.content_type
|
220
|
+
expected_body = [{ error: 'Result is Invalid' }]
|
221
|
+
assert_equal expected_body.to_json, @response.body
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_render_array_using_implicit_serializer
|
225
|
+
get :render_array_using_implicit_serializer
|
226
|
+
assert_equal 'application/json', @response.content_type
|
227
|
+
|
228
|
+
expected = [
|
229
|
+
{
|
230
|
+
name: 'Name 1',
|
231
|
+
description: 'Description 1'
|
232
|
+
},
|
233
|
+
{
|
234
|
+
name: 'Name 2',
|
235
|
+
description: 'Description 2'
|
236
|
+
}
|
237
|
+
]
|
238
|
+
|
239
|
+
assert_equal expected.to_json, @response.body
|
240
|
+
end
|
241
|
+
|
242
|
+
def test_render_array_using_implicit_serializer_and_meta
|
243
|
+
with_adapter :json_api do
|
244
|
+
get :render_array_using_implicit_serializer_and_meta
|
245
|
+
end
|
246
|
+
expected = {
|
247
|
+
data: [
|
248
|
+
{
|
249
|
+
id: assigns(:profiles).first.id.to_s,
|
250
|
+
type: 'profiles',
|
251
|
+
attributes: {
|
252
|
+
name: 'Name 1',
|
253
|
+
description: 'Description 1'
|
254
|
+
}
|
255
|
+
}
|
256
|
+
],
|
257
|
+
meta: {
|
258
|
+
total: 10
|
259
|
+
}
|
260
|
+
}
|
261
|
+
|
262
|
+
assert_equal 'application/json', @response.content_type
|
263
|
+
assert_equal expected.to_json, @response.body
|
264
|
+
end
|
265
|
+
|
266
|
+
def test_render_array_using_implicit_serializer_and_links
|
267
|
+
get :render_array_using_implicit_serializer_and_links
|
268
|
+
|
269
|
+
expected = {
|
270
|
+
data: [
|
271
|
+
{
|
272
|
+
id: assigns(:profiles).first.id.to_s,
|
273
|
+
type: 'profiles',
|
274
|
+
attributes: {
|
275
|
+
name: 'Name 1',
|
276
|
+
description: 'Description 1'
|
277
|
+
}
|
278
|
+
}
|
279
|
+
],
|
280
|
+
links: {
|
281
|
+
self: 'http://example.com/api/profiles/1'
|
282
|
+
}
|
283
|
+
}
|
284
|
+
|
285
|
+
assert_equal 'application/json', @response.content_type
|
286
|
+
assert_equal expected.to_json, @response.body
|
287
|
+
end
|
288
|
+
|
289
|
+
def test_render_with_cache_enable
|
290
|
+
expected = {
|
291
|
+
id: 1,
|
292
|
+
title: 'New Post',
|
293
|
+
body: 'Body',
|
294
|
+
comments: [
|
295
|
+
{
|
296
|
+
id: 1,
|
297
|
+
body: 'ZOMG A COMMENT' }
|
298
|
+
],
|
299
|
+
blog: {
|
300
|
+
id: 999,
|
301
|
+
name: 'Custom blog'
|
302
|
+
},
|
303
|
+
author: {
|
304
|
+
id: 1,
|
305
|
+
name: 'Joao Moura.'
|
306
|
+
}
|
307
|
+
}
|
308
|
+
|
309
|
+
ActionController::Base.cache_store.clear
|
310
|
+
Timecop.freeze(Time.zone.now) do
|
311
|
+
get :render_object_with_cache_enabled
|
312
|
+
|
313
|
+
assert_equal 'application/json', @response.content_type
|
314
|
+
assert_equal expected.to_json, @response.body
|
315
|
+
|
316
|
+
get :render_changed_object_with_cache_enabled
|
317
|
+
assert_equal expected.to_json, @response.body
|
318
|
+
end
|
319
|
+
|
320
|
+
ActionController::Base.cache_store.clear
|
321
|
+
get :render_changed_object_with_cache_enabled
|
322
|
+
assert_not_equal expected.to_json, @response.body
|
323
|
+
end
|
324
|
+
|
325
|
+
def test_render_with_cache_enable_and_expired
|
326
|
+
ActionController::Base.cache_store.clear
|
327
|
+
get :render_object_expired_with_cache_enabled
|
328
|
+
|
329
|
+
expected = {
|
330
|
+
id: 1,
|
331
|
+
title: 'ZOMG a New Post',
|
332
|
+
body: 'Body',
|
333
|
+
comments: [
|
334
|
+
{
|
335
|
+
id: 1,
|
336
|
+
body: 'ZOMG A COMMENT' }
|
337
|
+
],
|
338
|
+
blog: {
|
339
|
+
id: 999,
|
340
|
+
name: 'Custom blog'
|
341
|
+
},
|
342
|
+
author: {
|
343
|
+
id: 1,
|
344
|
+
name: 'Joao Moura.'
|
345
|
+
}
|
346
|
+
}
|
347
|
+
|
348
|
+
assert_equal 'application/json', @response.content_type
|
349
|
+
actual = @response.body
|
350
|
+
expected = expected.to_json
|
351
|
+
if ENV['APPVEYOR'] && actual != expected
|
352
|
+
skip('Cache expiration tests sometimes fail on Appveyor. FIXME :)')
|
353
|
+
else
|
354
|
+
assert_equal actual, expected
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
def test_render_with_fragment_only_cache_enable
|
359
|
+
ActionController::Base.cache_store.clear
|
360
|
+
get :render_fragment_changed_object_with_only_cache_enabled
|
361
|
+
response = JSON.parse(@response.body)
|
362
|
+
|
363
|
+
assert_equal 'application/json', @response.content_type
|
364
|
+
assert_equal 'ZOMG A ROLE', response['name']
|
365
|
+
assert_equal 'HUEHUEBRBR', response['description']
|
366
|
+
end
|
367
|
+
|
368
|
+
def test_render_with_fragment_except_cache_enable
|
369
|
+
ActionController::Base.cache_store.clear
|
370
|
+
get :render_fragment_changed_object_with_except_cache_enabled
|
371
|
+
response = JSON.parse(@response.body)
|
372
|
+
|
373
|
+
assert_equal 'application/json', @response.content_type
|
374
|
+
assert_equal 5, response['rating']
|
375
|
+
assert_equal 'lol', response['content']
|
376
|
+
end
|
377
|
+
|
378
|
+
def test_render_fragment_changed_object_with_relationship
|
379
|
+
ActionController::Base.cache_store.clear
|
380
|
+
|
381
|
+
Timecop.freeze(Time.zone.now) do
|
382
|
+
get :render_fragment_changed_object_with_relationship
|
383
|
+
response = JSON.parse(@response.body)
|
384
|
+
|
385
|
+
expected_return = {
|
386
|
+
'id' => 1,
|
387
|
+
'time' => Time.zone.now.to_s,
|
388
|
+
'likeable' => {
|
389
|
+
'id' => 1,
|
390
|
+
'body' => 'ZOMG A COMMENT'
|
391
|
+
}
|
392
|
+
}
|
393
|
+
|
394
|
+
assert_equal 'application/json', @response.content_type
|
395
|
+
assert_equal expected_return, response
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
def test_cache_expiration_on_update
|
400
|
+
ActionController::Base.cache_store.clear
|
401
|
+
get :render_object_with_cache_enabled
|
402
|
+
|
403
|
+
expected = {
|
404
|
+
id: 1,
|
405
|
+
title: 'ZOMG a New Post',
|
406
|
+
body: 'Body',
|
407
|
+
comments: [
|
408
|
+
{
|
409
|
+
id: 1,
|
410
|
+
body: 'ZOMG A COMMENT' }
|
411
|
+
],
|
412
|
+
blog: {
|
413
|
+
id: 999,
|
414
|
+
name: 'Custom blog'
|
415
|
+
},
|
416
|
+
author: {
|
417
|
+
id: 1,
|
418
|
+
name: 'Joao Moura.'
|
419
|
+
}
|
420
|
+
}
|
421
|
+
|
422
|
+
get :update_and_render_object_with_cache_enabled
|
423
|
+
|
424
|
+
assert_equal 'application/json', @response.content_type
|
425
|
+
actual = @response.body
|
426
|
+
expected = expected.to_json
|
427
|
+
if ENV['APPVEYOR'] && actual != expected
|
428
|
+
skip('Cache expiration tests sometimes fail on Appveyor. FIXME :)')
|
429
|
+
else
|
430
|
+
assert_equal actual, expected
|
431
|
+
end
|
432
|
+
end
|
433
|
+
|
434
|
+
def test_warn_overridding_use_adapter_as_falsy_on_controller_instance
|
435
|
+
controller = Class.new(ImplicitSerializationTestController) do
|
436
|
+
def use_adapter?
|
437
|
+
false
|
438
|
+
end
|
439
|
+
end.new
|
440
|
+
assert_output(nil, /adapter: false/) do
|
441
|
+
controller.get_serializer(Profile.new)
|
442
|
+
end
|
443
|
+
end
|
444
|
+
|
445
|
+
def test_dont_warn_overridding_use_adapter_as_truthy_on_controller_instance
|
446
|
+
controller = Class.new(ImplicitSerializationTestController) do
|
447
|
+
def use_adapter?
|
448
|
+
true
|
449
|
+
end
|
450
|
+
end.new
|
451
|
+
assert_output(nil, '') do
|
452
|
+
controller.get_serializer(Profile.new)
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
def test_render_event_is_emmited
|
457
|
+
::ActiveSupport::Notifications.subscribe('render.active_model_serializers') do |name|
|
458
|
+
@name = name
|
459
|
+
end
|
460
|
+
|
461
|
+
get :render_using_implicit_serializer
|
462
|
+
|
463
|
+
assert_equal 'render.active_model_serializers', @name
|
464
|
+
end
|
465
|
+
end
|
466
|
+
end
|
467
|
+
end
|