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,197 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class JsonApi
|
6
|
+
class LinkedTest < ActionController::TestCase
|
7
|
+
class LinkedTestController < ActionController::Base
|
8
|
+
require 'active_model_serializers/register_jsonapi_renderer'
|
9
|
+
def setup_post
|
10
|
+
ActionController::Base.cache_store.clear
|
11
|
+
@role1 = Role.new(id: 1, name: 'admin')
|
12
|
+
@role2 = Role.new(id: 2, name: 'colab')
|
13
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
14
|
+
@author.posts = []
|
15
|
+
@author.bio = nil
|
16
|
+
@author.roles = [@role1, @role2]
|
17
|
+
@role1.author = @author
|
18
|
+
@role2.author = @author
|
19
|
+
@author2 = Author.new(id: 2, name: 'Anonymous')
|
20
|
+
@author2.posts = []
|
21
|
+
@author2.bio = nil
|
22
|
+
@author2.roles = []
|
23
|
+
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
|
24
|
+
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
25
|
+
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
26
|
+
@post.comments = [@first_comment, @second_comment]
|
27
|
+
@post.author = @author
|
28
|
+
@first_comment.post = @post
|
29
|
+
@first_comment.author = @author2
|
30
|
+
@second_comment.post = @post
|
31
|
+
@second_comment.author = nil
|
32
|
+
@post2 = Post.new(id: 2, title: 'Another Post', body: 'Body')
|
33
|
+
@post2.author = @author
|
34
|
+
@post2.comments = []
|
35
|
+
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
36
|
+
@post.blog = @blog
|
37
|
+
@post2.blog = @blog
|
38
|
+
end
|
39
|
+
|
40
|
+
def render_resource_without_include
|
41
|
+
setup_post
|
42
|
+
render jsonapi: @post
|
43
|
+
end
|
44
|
+
|
45
|
+
def render_resource_with_include
|
46
|
+
setup_post
|
47
|
+
render jsonapi: @post, include: [:author]
|
48
|
+
end
|
49
|
+
|
50
|
+
def render_resource_with_include_of_custom_key_by_original
|
51
|
+
setup_post
|
52
|
+
render jsonapi: @post, include: [:reviews], serializer: PostWithCustomKeysSerializer
|
53
|
+
end
|
54
|
+
|
55
|
+
def render_resource_with_nested_include
|
56
|
+
setup_post
|
57
|
+
render jsonapi: @post, include: [comments: [:author]]
|
58
|
+
end
|
59
|
+
|
60
|
+
def render_resource_with_nested_has_many_include_wildcard
|
61
|
+
setup_post
|
62
|
+
render jsonapi: @post, include: 'author.*'
|
63
|
+
end
|
64
|
+
|
65
|
+
def render_resource_with_missing_nested_has_many_include
|
66
|
+
setup_post
|
67
|
+
@post.author = @author2 # author2 has no roles.
|
68
|
+
render jsonapi: @post, include: [author: [:roles]]
|
69
|
+
end
|
70
|
+
|
71
|
+
def render_collection_with_missing_nested_has_many_include
|
72
|
+
setup_post
|
73
|
+
@post.author = @author2
|
74
|
+
render jsonapi: [@post, @post2], include: [author: [:roles]]
|
75
|
+
end
|
76
|
+
|
77
|
+
def render_collection_without_include
|
78
|
+
setup_post
|
79
|
+
render jsonapi: [@post]
|
80
|
+
end
|
81
|
+
|
82
|
+
def render_collection_with_include
|
83
|
+
setup_post
|
84
|
+
render jsonapi: [@post], include: 'author, comments'
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
tests LinkedTestController
|
89
|
+
|
90
|
+
def test_render_resource_without_include
|
91
|
+
get :render_resource_without_include
|
92
|
+
response = JSON.parse(@response.body)
|
93
|
+
refute response.key? 'included'
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_render_resource_with_include
|
97
|
+
get :render_resource_with_include
|
98
|
+
response = JSON.parse(@response.body)
|
99
|
+
assert response.key? 'included'
|
100
|
+
assert_equal 1, response['included'].size
|
101
|
+
assert_equal 'Steve K.', response['included'].first['attributes']['name']
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_render_resource_with_nested_has_many_include
|
105
|
+
get :render_resource_with_nested_has_many_include_wildcard
|
106
|
+
response = JSON.parse(@response.body)
|
107
|
+
expected_linked = [
|
108
|
+
{
|
109
|
+
'id' => '1',
|
110
|
+
'type' => 'authors',
|
111
|
+
'attributes' => {
|
112
|
+
'name' => 'Steve K.'
|
113
|
+
},
|
114
|
+
'relationships' => {
|
115
|
+
'posts' => { 'data' => [] },
|
116
|
+
'roles' => { 'data' => [{ 'type' => 'roles', 'id' => '1' }, { 'type' => 'roles', 'id' => '2' }] },
|
117
|
+
'bio' => { 'data' => nil }
|
118
|
+
}
|
119
|
+
}, {
|
120
|
+
'id' => '1',
|
121
|
+
'type' => 'roles',
|
122
|
+
'attributes' => {
|
123
|
+
'name' => 'admin',
|
124
|
+
'description' => nil,
|
125
|
+
'slug' => 'admin-1'
|
126
|
+
},
|
127
|
+
'relationships' => {
|
128
|
+
'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } }
|
129
|
+
}
|
130
|
+
}, {
|
131
|
+
'id' => '2',
|
132
|
+
'type' => 'roles',
|
133
|
+
'attributes' => {
|
134
|
+
'name' => 'colab',
|
135
|
+
'description' => nil,
|
136
|
+
'slug' => 'colab-2'
|
137
|
+
},
|
138
|
+
'relationships' => {
|
139
|
+
'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } }
|
140
|
+
}
|
141
|
+
}
|
142
|
+
]
|
143
|
+
assert_equal expected_linked, response['included']
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_render_resource_with_include_of_custom_key_by_original
|
147
|
+
get :render_resource_with_include_of_custom_key_by_original
|
148
|
+
response = JSON.parse(@response.body)
|
149
|
+
assert response.key? 'included'
|
150
|
+
|
151
|
+
relationships = response['data']['relationships']
|
152
|
+
|
153
|
+
assert_includes relationships, 'reviews'
|
154
|
+
assert_includes relationships, 'writer'
|
155
|
+
assert_includes relationships, 'site'
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_render_resource_with_nested_include
|
159
|
+
get :render_resource_with_nested_include
|
160
|
+
response = JSON.parse(@response.body)
|
161
|
+
assert response.key? 'included'
|
162
|
+
assert_equal 3, response['included'].size
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_render_collection_without_include
|
166
|
+
get :render_collection_without_include
|
167
|
+
response = JSON.parse(@response.body)
|
168
|
+
refute response.key? 'included'
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_render_collection_with_include
|
172
|
+
get :render_collection_with_include
|
173
|
+
response = JSON.parse(@response.body)
|
174
|
+
assert response.key? 'included'
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_render_resource_with_nested_attributes_even_when_missing_associations
|
178
|
+
get :render_resource_with_missing_nested_has_many_include
|
179
|
+
response = JSON.parse(@response.body)
|
180
|
+
assert response.key? 'included'
|
181
|
+
refute has_type?(response['included'], 'roles')
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_render_collection_with_missing_nested_has_many_include
|
185
|
+
get :render_collection_with_missing_nested_has_many_include
|
186
|
+
response = JSON.parse(@response.body)
|
187
|
+
assert response.key? 'included'
|
188
|
+
assert has_type?(response['included'], 'roles')
|
189
|
+
end
|
190
|
+
|
191
|
+
def has_type?(collection, value)
|
192
|
+
collection.detect { |i| i['type'] == value }
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'will_paginate/array'
|
3
|
+
require 'kaminari'
|
4
|
+
require 'kaminari/hooks'
|
5
|
+
::Kaminari::Hooks.init
|
6
|
+
|
7
|
+
module ActionController
|
8
|
+
module Serialization
|
9
|
+
class JsonApi
|
10
|
+
class PaginationTest < ActionController::TestCase
|
11
|
+
KAMINARI_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari'.freeze
|
12
|
+
WILL_PAGINATE_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate'.freeze
|
13
|
+
|
14
|
+
class PaginationTestController < ActionController::Base
|
15
|
+
def setup
|
16
|
+
@array = [
|
17
|
+
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
18
|
+
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
|
19
|
+
Profile.new({ name: 'Name 3', description: 'Description 3', comments: 'Comments 3' })
|
20
|
+
]
|
21
|
+
end
|
22
|
+
|
23
|
+
def using_kaminari
|
24
|
+
setup
|
25
|
+
Kaminari.paginate_array(@array).page(params[:page][:number]).per(params[:page][:size])
|
26
|
+
end
|
27
|
+
|
28
|
+
def using_will_paginate
|
29
|
+
setup
|
30
|
+
@array.paginate(page: params[:page][:number], per_page: params[:page][:size])
|
31
|
+
end
|
32
|
+
|
33
|
+
def render_pagination_using_kaminari
|
34
|
+
render json: using_kaminari, adapter: :json_api
|
35
|
+
end
|
36
|
+
|
37
|
+
def render_pagination_using_will_paginate
|
38
|
+
render json: using_will_paginate, adapter: :json_api
|
39
|
+
end
|
40
|
+
|
41
|
+
def render_array_without_pagination_links
|
42
|
+
setup
|
43
|
+
render json: @array, adapter: :json_api
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
tests PaginationTestController
|
48
|
+
|
49
|
+
def test_render_pagination_links_with_will_paginate
|
50
|
+
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
51
|
+
'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
52
|
+
'prev' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
53
|
+
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
54
|
+
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
|
55
|
+
|
56
|
+
get :render_pagination_using_will_paginate, params: { page: { number: 2, size: 1 } }
|
57
|
+
response = JSON.parse(@response.body)
|
58
|
+
assert_equal expected_links, response['links']
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_render_only_last_and_next_pagination_links
|
62
|
+
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
63
|
+
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
|
64
|
+
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" }
|
65
|
+
get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 } }
|
66
|
+
response = JSON.parse(@response.body)
|
67
|
+
assert_equal expected_links, response['links']
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_render_pagination_links_with_kaminari
|
71
|
+
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
72
|
+
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
73
|
+
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
74
|
+
'next' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
75
|
+
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
|
76
|
+
get :render_pagination_using_kaminari, params: { page: { number: 2, size: 1 } }
|
77
|
+
response = JSON.parse(@response.body)
|
78
|
+
assert_equal expected_links, response['links']
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_render_only_prev_and_first_pagination_links
|
82
|
+
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
83
|
+
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
84
|
+
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1" }
|
85
|
+
get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 } }
|
86
|
+
response = JSON.parse(@response.body)
|
87
|
+
assert_equal expected_links, response['links']
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_render_only_last_and_next_pagination_links_with_additional_params
|
91
|
+
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
|
92
|
+
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
|
93
|
+
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" }
|
94
|
+
get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 }, teste: 'additional' }
|
95
|
+
response = JSON.parse(@response.body)
|
96
|
+
assert_equal expected_links, response['links']
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_render_only_prev_and_first_pagination_links_with_additional_params
|
100
|
+
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
|
101
|
+
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
|
102
|
+
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional" }
|
103
|
+
get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 }, teste: 'additional' }
|
104
|
+
response = JSON.parse(@response.body)
|
105
|
+
assert_equal expected_links, response['links']
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_array_without_pagination_links
|
109
|
+
get :render_array_without_pagination_links, params: { page: { number: 2, size: 1 } }
|
110
|
+
response = JSON.parse(@response.body)
|
111
|
+
refute response.key? 'links'
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class JsonApi
|
6
|
+
class KeyTransformTest < ActionController::TestCase
|
7
|
+
class KeyTransformTestController < ActionController::Base
|
8
|
+
Post = Class.new(::Model)
|
9
|
+
class PostSerializer < ActiveModel::Serializer
|
10
|
+
type 'posts'
|
11
|
+
attributes :title, :body, :publish_at
|
12
|
+
belongs_to :author
|
13
|
+
has_many :top_comments
|
14
|
+
|
15
|
+
link(:post_authors) { 'https://example.com/post_authors' }
|
16
|
+
|
17
|
+
meta do
|
18
|
+
{
|
19
|
+
rating: 5,
|
20
|
+
favorite_count: 10
|
21
|
+
}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
Author = Class.new(::Model)
|
26
|
+
class AuthorSerializer < ActiveModel::Serializer
|
27
|
+
type 'authors'
|
28
|
+
attributes :first_name, :last_name
|
29
|
+
end
|
30
|
+
|
31
|
+
TopComment = Class.new(::Model)
|
32
|
+
class TopCommentSerializer < ActiveModel::Serializer
|
33
|
+
type 'top_comments'
|
34
|
+
attributes :body
|
35
|
+
belongs_to :author
|
36
|
+
end
|
37
|
+
|
38
|
+
def setup_post
|
39
|
+
ActionController::Base.cache_store.clear
|
40
|
+
@author = Author.new(id: 1, first_name: 'Bob', last_name: 'Jones')
|
41
|
+
@comment1 = TopComment.new(id: 7, body: 'cool', author: @author)
|
42
|
+
@comment2 = TopComment.new(id: 12, body: 'awesome', author: @author)
|
43
|
+
@post = Post.new(id: 1337, title: 'Title 1', body: 'Body 1',
|
44
|
+
author: @author, top_comments: [@comment1, @comment2],
|
45
|
+
publish_at: '2020-03-16T03:55:25.291Z')
|
46
|
+
@comment1.post = @post
|
47
|
+
@comment2.post = @post
|
48
|
+
end
|
49
|
+
|
50
|
+
def render_resource_with_transform
|
51
|
+
setup_post
|
52
|
+
render json: @post, serializer: PostSerializer, adapter: :json_api,
|
53
|
+
key_transform: :camel
|
54
|
+
end
|
55
|
+
|
56
|
+
def render_resource_with_transform_nil
|
57
|
+
setup_post
|
58
|
+
render json: @post, serializer: PostSerializer, adapter: :json_api,
|
59
|
+
key_transform: nil
|
60
|
+
end
|
61
|
+
|
62
|
+
def render_resource_with_transform_with_global_config
|
63
|
+
setup_post
|
64
|
+
old_transform = ActiveModelSerializers.config.key_transform
|
65
|
+
ActiveModelSerializers.config.key_transform = :camel_lower
|
66
|
+
render json: @post, serializer: PostSerializer, adapter: :json_api
|
67
|
+
ActiveModelSerializers.config.key_transform = old_transform
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
tests KeyTransformTestController
|
72
|
+
|
73
|
+
def test_render_resource_with_transform
|
74
|
+
get :render_resource_with_transform
|
75
|
+
response = JSON.parse(@response.body)
|
76
|
+
expected = {
|
77
|
+
'Data' => {
|
78
|
+
'Id' => '1337',
|
79
|
+
'Type' => 'Posts',
|
80
|
+
'Attributes' => {
|
81
|
+
'Title' => 'Title 1',
|
82
|
+
'Body' => 'Body 1',
|
83
|
+
'PublishAt' => '2020-03-16T03:55:25.291Z'
|
84
|
+
},
|
85
|
+
'Relationships' => {
|
86
|
+
'Author' => {
|
87
|
+
'Data' => {
|
88
|
+
'Id' => '1',
|
89
|
+
'Type' => 'Authors'
|
90
|
+
}
|
91
|
+
},
|
92
|
+
'TopComments' => {
|
93
|
+
'Data' => [
|
94
|
+
{ 'Id' => '7', 'Type' => 'TopComments' },
|
95
|
+
{ 'Id' => '12', 'Type' => 'TopComments' }
|
96
|
+
]
|
97
|
+
}
|
98
|
+
},
|
99
|
+
'Links' => {
|
100
|
+
'PostAuthors' => 'https://example.com/post_authors'
|
101
|
+
},
|
102
|
+
'Meta' => { 'Rating' => 5, 'FavoriteCount' => 10 }
|
103
|
+
}
|
104
|
+
}
|
105
|
+
assert_equal expected, response
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_render_resource_with_transform_nil
|
109
|
+
get :render_resource_with_transform_nil
|
110
|
+
response = JSON.parse(@response.body)
|
111
|
+
expected = {
|
112
|
+
'data' => {
|
113
|
+
'id' => '1337',
|
114
|
+
'type' => 'posts',
|
115
|
+
'attributes' => {
|
116
|
+
'title' => 'Title 1',
|
117
|
+
'body' => 'Body 1',
|
118
|
+
'publish-at' => '2020-03-16T03:55:25.291Z'
|
119
|
+
},
|
120
|
+
'relationships' => {
|
121
|
+
'author' => {
|
122
|
+
'data' => {
|
123
|
+
'id' => '1',
|
124
|
+
'type' => 'authors'
|
125
|
+
}
|
126
|
+
},
|
127
|
+
'top-comments' => {
|
128
|
+
'data' => [
|
129
|
+
{ 'id' => '7', 'type' => 'top-comments' },
|
130
|
+
{ 'id' => '12', 'type' => 'top-comments' }
|
131
|
+
]
|
132
|
+
}
|
133
|
+
},
|
134
|
+
'links' => {
|
135
|
+
'post-authors' => 'https://example.com/post_authors'
|
136
|
+
},
|
137
|
+
'meta' => { 'rating' => 5, 'favorite-count' => 10 }
|
138
|
+
}
|
139
|
+
}
|
140
|
+
assert_equal expected, response
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_render_resource_with_transform_with_global_config
|
144
|
+
get :render_resource_with_transform_with_global_config
|
145
|
+
response = JSON.parse(@response.body)
|
146
|
+
expected = {
|
147
|
+
'data' => {
|
148
|
+
'id' => '1337',
|
149
|
+
'type' => 'posts',
|
150
|
+
'attributes' => {
|
151
|
+
'title' => 'Title 1',
|
152
|
+
'body' => 'Body 1',
|
153
|
+
'publishAt' => '2020-03-16T03:55:25.291Z'
|
154
|
+
},
|
155
|
+
'relationships' => {
|
156
|
+
'author' => {
|
157
|
+
'data' => {
|
158
|
+
'id' => '1',
|
159
|
+
'type' => 'authors'
|
160
|
+
}
|
161
|
+
},
|
162
|
+
'topComments' => {
|
163
|
+
'data' => [
|
164
|
+
{ 'id' => '7', 'type' => 'topComments' },
|
165
|
+
{ 'id' => '12', 'type' => 'topComments' }
|
166
|
+
]
|
167
|
+
}
|
168
|
+
},
|
169
|
+
'links' => {
|
170
|
+
'postAuthors' => 'https://example.com/post_authors'
|
171
|
+
},
|
172
|
+
'meta' => { 'rating' => 5, 'favoriteCount' => 10 }
|
173
|
+
}
|
174
|
+
}
|
175
|
+
assert_equal expected, response
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|