active_model_serializers 0.10.9 → 0.10.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -2
  3. data/lib/action_controller/serialization.rb +9 -1
  4. data/lib/active_model/serializer.rb +10 -2
  5. data/lib/active_model/serializer/version.rb +1 -1
  6. data/lib/active_model_serializers/adapter/attributes.rb +21 -0
  7. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +1 -1
  8. metadata +12 -259
  9. data/.github/ISSUE_TEMPLATE.md +0 -29
  10. data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
  11. data/.gitignore +0 -35
  12. data/.rubocop.yml +0 -109
  13. data/.simplecov +0 -110
  14. data/.travis.yml +0 -63
  15. data/CODE_OF_CONDUCT.md +0 -74
  16. data/CONTRIBUTING.md +0 -105
  17. data/Gemfile +0 -74
  18. data/Rakefile +0 -76
  19. data/active_model_serializers.gemspec +0 -64
  20. data/appveyor.yml +0 -28
  21. data/bin/bench +0 -171
  22. data/bin/bench_regression +0 -316
  23. data/bin/rubocop +0 -38
  24. data/bin/serve_benchmark +0 -39
  25. data/docs/README.md +0 -41
  26. data/docs/STYLE.md +0 -58
  27. data/docs/general/adapters.md +0 -269
  28. data/docs/general/caching.md +0 -58
  29. data/docs/general/configuration_options.md +0 -185
  30. data/docs/general/deserialization.md +0 -100
  31. data/docs/general/fields.md +0 -31
  32. data/docs/general/getting_started.md +0 -133
  33. data/docs/general/instrumentation.md +0 -40
  34. data/docs/general/key_transforms.md +0 -40
  35. data/docs/general/logging.md +0 -21
  36. data/docs/general/rendering.md +0 -293
  37. data/docs/general/serializers.md +0 -495
  38. data/docs/how-open-source-maintained.jpg +0 -0
  39. data/docs/howto/add_pagination_links.md +0 -138
  40. data/docs/howto/add_relationship_links.md +0 -140
  41. data/docs/howto/add_root_key.md +0 -62
  42. data/docs/howto/grape_integration.md +0 -42
  43. data/docs/howto/outside_controller_use.md +0 -66
  44. data/docs/howto/passing_arbitrary_options.md +0 -27
  45. data/docs/howto/serialize_poro.md +0 -73
  46. data/docs/howto/test.md +0 -154
  47. data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
  48. data/docs/integrations/ember-and-json-api.md +0 -147
  49. data/docs/integrations/grape.md +0 -19
  50. data/docs/jsonapi/errors.md +0 -56
  51. data/docs/jsonapi/schema.md +0 -151
  52. data/docs/jsonapi/schema/schema.json +0 -366
  53. data/docs/rfcs/0000-namespace.md +0 -106
  54. data/docs/rfcs/template.md +0 -15
  55. data/test/action_controller/adapter_selector_test.rb +0 -64
  56. data/test/action_controller/explicit_serializer_test.rb +0 -137
  57. data/test/action_controller/json/include_test.rb +0 -248
  58. data/test/action_controller/json_api/deserialization_test.rb +0 -114
  59. data/test/action_controller/json_api/errors_test.rb +0 -42
  60. data/test/action_controller/json_api/fields_test.rb +0 -68
  61. data/test/action_controller/json_api/linked_test.rb +0 -204
  62. data/test/action_controller/json_api/pagination_test.rb +0 -126
  63. data/test/action_controller/json_api/transform_test.rb +0 -191
  64. data/test/action_controller/lookup_proc_test.rb +0 -51
  65. data/test/action_controller/namespace_lookup_test.rb +0 -234
  66. data/test/action_controller/serialization_scope_name_test.rb +0 -237
  67. data/test/action_controller/serialization_test.rb +0 -480
  68. data/test/active_model_serializers/adapter_for_test.rb +0 -210
  69. data/test/active_model_serializers/json_pointer_test.rb +0 -24
  70. data/test/active_model_serializers/logging_test.rb +0 -79
  71. data/test/active_model_serializers/model_test.rb +0 -144
  72. data/test/active_model_serializers/railtie_test_isolated.rb +0 -70
  73. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -163
  74. data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -73
  75. data/test/active_model_serializers/test/schema_test.rb +0 -133
  76. data/test/active_model_serializers/test/serializer_test.rb +0 -64
  77. data/test/active_record_test.rb +0 -11
  78. data/test/adapter/attributes_test.rb +0 -42
  79. data/test/adapter/deprecation_test.rb +0 -102
  80. data/test/adapter/json/belongs_to_test.rb +0 -47
  81. data/test/adapter/json/collection_test.rb +0 -106
  82. data/test/adapter/json/has_many_test.rb +0 -55
  83. data/test/adapter/json/transform_test.rb +0 -95
  84. data/test/adapter/json_api/belongs_to_test.rb +0 -157
  85. data/test/adapter/json_api/collection_test.rb +0 -98
  86. data/test/adapter/json_api/errors_test.rb +0 -78
  87. data/test/adapter/json_api/fields_test.rb +0 -98
  88. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -98
  89. data/test/adapter/json_api/has_many_test.rb +0 -175
  90. data/test/adapter/json_api/has_one_test.rb +0 -82
  91. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -215
  92. data/test/adapter/json_api/json_api_test.rb +0 -35
  93. data/test/adapter/json_api/linked_test.rb +0 -415
  94. data/test/adapter/json_api/links_test.rb +0 -112
  95. data/test/adapter/json_api/pagination_links_test.rb +0 -208
  96. data/test/adapter/json_api/parse_test.rb +0 -139
  97. data/test/adapter/json_api/relationship_test.rb +0 -399
  98. data/test/adapter/json_api/resource_meta_test.rb +0 -102
  99. data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -84
  100. data/test/adapter/json_api/transform_test.rb +0 -514
  101. data/test/adapter/json_api/type_test.rb +0 -195
  102. data/test/adapter/json_test.rb +0 -48
  103. data/test/adapter/null_test.rb +0 -24
  104. data/test/adapter/polymorphic_test.rb +0 -220
  105. data/test/adapter_test.rb +0 -69
  106. data/test/array_serializer_test.rb +0 -24
  107. data/test/benchmark/app.rb +0 -67
  108. data/test/benchmark/benchmarking_support.rb +0 -69
  109. data/test/benchmark/bm_active_record.rb +0 -83
  110. data/test/benchmark/bm_adapter.rb +0 -40
  111. data/test/benchmark/bm_caching.rb +0 -121
  112. data/test/benchmark/bm_lookup_chain.rb +0 -85
  113. data/test/benchmark/bm_transform.rb +0 -47
  114. data/test/benchmark/config.ru +0 -3
  115. data/test/benchmark/controllers.rb +0 -85
  116. data/test/benchmark/fixtures.rb +0 -221
  117. data/test/cache_test.rb +0 -717
  118. data/test/collection_serializer_test.rb +0 -129
  119. data/test/fixtures/active_record.rb +0 -115
  120. data/test/fixtures/poro.rb +0 -227
  121. data/test/generators/scaffold_controller_generator_test.rb +0 -26
  122. data/test/generators/serializer_generator_test.rb +0 -77
  123. data/test/grape_test.rb +0 -198
  124. data/test/lint_test.rb +0 -51
  125. data/test/logger_test.rb +0 -22
  126. data/test/poro_test.rb +0 -11
  127. data/test/serializable_resource_test.rb +0 -81
  128. data/test/serializers/association_macros_test.rb +0 -39
  129. data/test/serializers/associations_test.rb +0 -520
  130. data/test/serializers/attribute_test.rb +0 -155
  131. data/test/serializers/attributes_test.rb +0 -54
  132. data/test/serializers/caching_configuration_test_isolated.rb +0 -172
  133. data/test/serializers/configuration_test.rb +0 -34
  134. data/test/serializers/fieldset_test.rb +0 -16
  135. data/test/serializers/meta_test.rb +0 -204
  136. data/test/serializers/options_test.rb +0 -34
  137. data/test/serializers/read_attribute_for_serialization_test.rb +0 -81
  138. data/test/serializers/reflection_test.rb +0 -481
  139. data/test/serializers/root_test.rb +0 -23
  140. data/test/serializers/serialization_test.rb +0 -57
  141. data/test/serializers/serializer_for_test.rb +0 -138
  142. data/test/serializers/serializer_for_with_namespace_test.rb +0 -90
  143. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  144. data/test/support/isolated_unit.rb +0 -86
  145. data/test/support/rails5_shims.rb +0 -55
  146. data/test/support/rails_app.rb +0 -40
  147. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  148. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
  149. data/test/support/schemas/custom/show.json +0 -7
  150. data/test/support/schemas/hyper_schema.json +0 -93
  151. data/test/support/schemas/render_using_json_api.json +0 -43
  152. data/test/support/schemas/simple_json_pointers.json +0 -10
  153. data/test/support/serialization_testing.rb +0 -81
  154. data/test/test_helper.rb +0 -72
@@ -1,114 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActionController
6
- module Serialization
7
- class JsonApi
8
- class DeserializationTest < ActionController::TestCase
9
- class DeserializationTestController < ActionController::Base
10
- def render_parsed_payload
11
- parsed_hash = ActiveModelSerializers::Deserialization.jsonapi_parse(params)
12
- render json: parsed_hash
13
- end
14
-
15
- def render_polymorphic_parsed_payload
16
- parsed_hash = ActiveModelSerializers::Deserialization.jsonapi_parse(
17
- params,
18
- polymorphic: [:restriction_for, :restricted_to]
19
- )
20
- render json: parsed_hash
21
- end
22
- end
23
-
24
- tests DeserializationTestController
25
-
26
- def test_deserialization_of_relationship_only_object
27
- hash = {
28
- 'data' => {
29
- 'type' => 'restraints',
30
- 'relationships' => {
31
- 'restriction_for' => {
32
- 'data' => {
33
- 'type' => 'discounts',
34
- 'id' => '67'
35
- }
36
- },
37
- 'restricted_to' => {
38
- 'data' => nil
39
- }
40
- }
41
- },
42
- 'restraint' => {}
43
- }
44
-
45
- post :render_polymorphic_parsed_payload, params: hash
46
-
47
- response = JSON.parse(@response.body)
48
- expected = {
49
- 'restriction_for_id' => '67',
50
- 'restriction_for_type' => 'Discount',
51
- 'restricted_to_id' => nil,
52
- 'restricted_to_type' => nil
53
- }
54
-
55
- assert_equal(expected, response)
56
- end
57
-
58
- def test_deserialization
59
- hash = {
60
- 'data' => {
61
- 'type' => 'photos',
62
- 'id' => 'zorglub',
63
- 'attributes' => {
64
- 'title' => 'Ember Hamster',
65
- 'src' => 'http://example.com/images/productivity.png',
66
- 'image-width' => '200',
67
- 'imageHeight' => '200',
68
- 'ImageSize' => '1024'
69
- },
70
- 'relationships' => {
71
- 'author' => {
72
- 'data' => nil
73
- },
74
- 'photographer' => {
75
- 'data' => { 'type' => 'people', 'id' => '9' }
76
- },
77
- 'comments' => {
78
- 'data' => [
79
- { 'type' => 'comments', 'id' => '1' },
80
- { 'type' => 'comments', 'id' => '2' }
81
- ]
82
- },
83
- 'related-images' => {
84
- 'data' => [
85
- { 'type' => 'image', 'id' => '7' },
86
- { 'type' => 'image', 'id' => '8' }
87
- ]
88
- }
89
- }
90
- }
91
- }
92
-
93
- post :render_parsed_payload, params: hash
94
-
95
- response = JSON.parse(@response.body)
96
- expected = {
97
- 'id' => 'zorglub',
98
- 'title' => 'Ember Hamster',
99
- 'src' => 'http://example.com/images/productivity.png',
100
- 'image_width' => '200',
101
- 'image_height' => '200',
102
- 'image_size' => '1024',
103
- 'author_id' => nil,
104
- 'photographer_id' => '9',
105
- 'comment_ids' => %w(1 2),
106
- 'related_image_ids' => %w(7 8)
107
- }
108
-
109
- assert_equal(expected, response)
110
- end
111
- end
112
- end
113
- end
114
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActionController
6
- module Serialization
7
- class JsonApi
8
- class ErrorsTest < ActionController::TestCase
9
- def test_active_model_with_multiple_errors
10
- get :render_resource_with_errors
11
-
12
- expected_errors_object = {
13
- errors: [
14
- { source: { pointer: '/data/attributes/name' }, detail: 'cannot be nil' },
15
- { source: { pointer: '/data/attributes/name' }, detail: 'must be longer' },
16
- { source: { pointer: '/data/attributes/id' }, detail: 'must be a uuid' }
17
- ]
18
- }.to_json
19
- assert_equal json_response_body.to_json, expected_errors_object
20
- end
21
-
22
- def json_response_body
23
- JSON.load(@response.body)
24
- end
25
-
26
- class ErrorsTestController < ActionController::Base
27
- def render_resource_with_errors
28
- resource = Profile.new(name: 'Name 1',
29
- description: 'Description 1',
30
- comments: 'Comments 1')
31
- resource.errors.add(:name, 'cannot be nil')
32
- resource.errors.add(:name, 'must be longer')
33
- resource.errors.add(:id, 'must be a uuid')
34
- render json: resource, adapter: :json_api, serializer: ActiveModel::Serializer::ErrorSerializer
35
- end
36
- end
37
-
38
- tests ErrorsTestController
39
- end
40
- end
41
- end
42
- end
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActionController
6
- module Serialization
7
- class JsonApi
8
- class FieldsTest < ActionController::TestCase
9
- class FieldsTestController < ActionController::Base
10
- class AuthorWithName < Author
11
- attributes :first_name, :last_name
12
- end
13
- class AuthorWithNameSerializer < AuthorSerializer
14
- type 'authors'
15
- end
16
- class PostWithPublishAt < Post
17
- attributes :publish_at
18
- end
19
- class PostWithPublishAtSerializer < ActiveModel::Serializer
20
- type 'posts'
21
- attributes :title, :body, :publish_at
22
- belongs_to :author
23
- has_many :comments
24
- end
25
-
26
- def setup_post
27
- ActionController::Base.cache_store.clear
28
- @author = AuthorWithName.new(id: 1, first_name: 'Bob', last_name: 'Jones')
29
- @comment1 = Comment.new(id: 7, body: 'cool', author: @author)
30
- @comment2 = Comment.new(id: 12, body: 'awesome', author: @author)
31
- @post = PostWithPublishAt.new(id: 1337, title: 'Title 1', body: 'Body 1',
32
- author: @author, comments: [@comment1, @comment2],
33
- publish_at: '2020-03-16T03:55:25.291Z')
34
- @comment1.post = @post
35
- @comment2.post = @post
36
- end
37
-
38
- def render_fields_works_on_relationships
39
- setup_post
40
- render json: @post, serializer: PostWithPublishAtSerializer, adapter: :json_api, fields: { posts: [:author] }
41
- end
42
- end
43
-
44
- tests FieldsTestController
45
-
46
- test 'fields works on relationships' do
47
- get :render_fields_works_on_relationships
48
- response = JSON.parse(@response.body)
49
- expected = {
50
- 'data' => {
51
- 'id' => '1337',
52
- 'type' => 'posts',
53
- 'relationships' => {
54
- 'author' => {
55
- 'data' => {
56
- 'id' => '1',
57
- 'type' => 'authors'
58
- }
59
- }
60
- }
61
- }
62
- }
63
- assert_equal expected, response
64
- end
65
- end
66
- end
67
- end
68
- end
@@ -1,204 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActionController
6
- module Serialization
7
- class JsonApi
8
- class LinkedTest < ActionDispatch::IntegrationTest
9
- class LinkedTestController < ActionController::Base
10
- def setup_post
11
- ActionController::Base.cache_store.clear
12
- @role1 = Role.new(id: 1, name: 'admin')
13
- @role2 = Role.new(id: 2, name: 'colab')
14
- @author = Author.new(id: 1, name: 'Steve K.')
15
- @author.posts = []
16
- @author.bio = nil
17
- @author.roles = [@role1, @role2]
18
- @role1.author = @author
19
- @role2.author = @author
20
- @author2 = Author.new(id: 2, name: 'Anonymous')
21
- @author2.posts = []
22
- @author2.bio = nil
23
- @author2.roles = []
24
- @post = Post.new(id: 1, title: 'New Post', body: 'Body')
25
- @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
26
- @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
27
- @post.comments = [@first_comment, @second_comment]
28
- @post.author = @author
29
- @first_comment.post = @post
30
- @first_comment.author = @author2
31
- @second_comment.post = @post
32
- @second_comment.author = nil
33
- @post2 = Post.new(id: 2, title: 'Another Post', body: 'Body')
34
- @post2.author = @author
35
- @post2.comments = []
36
- @blog = Blog.new(id: 1, name: 'My Blog!!')
37
- @post.blog = @blog
38
- @post2.blog = @blog
39
- end
40
-
41
- def render_resource_without_include
42
- setup_post
43
- render json: @post
44
- end
45
-
46
- def render_resource_with_include
47
- setup_post
48
- render json: @post, adapter: :json_api, include: [:author]
49
- end
50
-
51
- def render_resource_with_include_of_custom_key_by_original
52
- setup_post
53
- render json: @post, adapter: :json_api, include: [:reviews], serializer: PostWithCustomKeysSerializer
54
- end
55
-
56
- def render_resource_with_nested_include
57
- setup_post
58
- render json: @post, adapter: :json_api, include: [comments: [:author]]
59
- end
60
-
61
- def render_resource_with_nested_has_many_include_wildcard
62
- setup_post
63
- render json: @post, adapter: :json_api, include: 'author.*'
64
- end
65
-
66
- def render_resource_with_missing_nested_has_many_include
67
- setup_post
68
- @post.author = @author2 # author2 has no roles.
69
- render json: @post, adapter: :json_api, include: [author: [:roles]]
70
- end
71
-
72
- def render_collection_with_missing_nested_has_many_include
73
- setup_post
74
- @post.author = @author2
75
- render json: [@post, @post2], adapter: :json_api, include: [author: [:roles]]
76
- end
77
-
78
- def render_collection_without_include
79
- setup_post
80
- render json: [@post], adapter: :json_api
81
- end
82
-
83
- def render_collection_with_include
84
- setup_post
85
- render json: [@post], adapter: :json_api, include: 'author, comments'
86
- end
87
- end
88
-
89
- setup do
90
- @routes = Rails.application.routes.draw do
91
- ActiveSupport::Deprecation.silence do
92
- match ':action', to: LinkedTestController, via: [:get, :post]
93
- end
94
- end
95
- end
96
-
97
- def test_render_resource_without_include
98
- get '/render_resource_without_include'
99
- response = JSON.parse(@response.body)
100
- refute response.key? 'included'
101
- end
102
-
103
- def test_render_resource_with_include
104
- get '/render_resource_with_include'
105
- response = JSON.parse(@response.body)
106
- assert response.key? 'included'
107
- assert_equal 1, response['included'].size
108
- assert_equal 'Steve K.', response['included'].first['attributes']['name']
109
- end
110
-
111
- def test_render_resource_with_nested_has_many_include
112
- get '/render_resource_with_nested_has_many_include_wildcard'
113
- response = JSON.parse(@response.body)
114
- expected_linked = [
115
- {
116
- 'id' => '1',
117
- 'type' => 'authors',
118
- 'attributes' => {
119
- 'name' => 'Steve K.'
120
- },
121
- 'relationships' => {
122
- 'posts' => { 'data' => [] },
123
- 'roles' => { 'data' => [{ 'type' => 'roles', 'id' => '1' }, { 'type' => 'roles', 'id' => '2' }] },
124
- 'bio' => { 'data' => nil }
125
- }
126
- }, {
127
- 'id' => '1',
128
- 'type' => 'roles',
129
- 'attributes' => {
130
- 'name' => 'admin',
131
- 'description' => nil,
132
- 'slug' => 'admin-1'
133
- },
134
- 'relationships' => {
135
- 'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } }
136
- }
137
- }, {
138
- 'id' => '2',
139
- 'type' => 'roles',
140
- 'attributes' => {
141
- 'name' => 'colab',
142
- 'description' => nil,
143
- 'slug' => 'colab-2'
144
- },
145
- 'relationships' => {
146
- 'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } }
147
- }
148
- }
149
- ]
150
- assert_equal expected_linked, response['included']
151
- end
152
-
153
- def test_render_resource_with_include_of_custom_key_by_original
154
- get '/render_resource_with_include_of_custom_key_by_original'
155
- response = JSON.parse(@response.body)
156
- assert response.key? 'included'
157
-
158
- relationships = response['data']['relationships']
159
-
160
- assert_includes relationships, 'reviews'
161
- assert_includes relationships, 'writer'
162
- assert_includes relationships, 'site'
163
- end
164
-
165
- def test_render_resource_with_nested_include
166
- get '/render_resource_with_nested_include'
167
- response = JSON.parse(@response.body)
168
- assert response.key? 'included'
169
- assert_equal 3, response['included'].size
170
- end
171
-
172
- def test_render_collection_without_include
173
- get '/render_collection_without_include'
174
- response = JSON.parse(@response.body)
175
- refute response.key? 'included'
176
- end
177
-
178
- def test_render_collection_with_include
179
- get '/render_collection_with_include'
180
- response = JSON.parse(@response.body)
181
- assert response.key? 'included'
182
- end
183
-
184
- def test_render_resource_with_nested_attributes_even_when_missing_associations
185
- get '/render_resource_with_missing_nested_has_many_include'
186
- response = JSON.parse(@response.body)
187
- assert response.key? 'included'
188
- refute include_type?(response['included'], 'roles')
189
- end
190
-
191
- def test_render_collection_with_missing_nested_has_many_include
192
- get '/render_collection_with_missing_nested_has_many_include'
193
- response = JSON.parse(@response.body)
194
- assert response.key? 'included'
195
- assert include_type?(response['included'], 'roles')
196
- end
197
-
198
- def include_type?(collection, value)
199
- collection.detect { |i| i['type'] == value }
200
- end
201
- end
202
- end
203
- end
204
- end
@@ -1,126 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
- require 'will_paginate/array'
5
- require 'kaminari'
6
- require 'kaminari/hooks'
7
- ::Kaminari::Hooks.init
8
-
9
- module ActionController
10
- module Serialization
11
- class JsonApi
12
- class PaginationTest < ActionController::TestCase
13
- KAMINARI_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari'.freeze
14
- WILL_PAGINATE_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate'.freeze
15
-
16
- class PaginationTestController < ActionController::Base
17
- def setup
18
- @array = [
19
- Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
20
- Profile.new(name: 'Name 2', description: 'Description 2', comments: 'Comments 2'),
21
- Profile.new(name: 'Name 3', description: 'Description 3', comments: 'Comments 3')
22
- ]
23
- end
24
-
25
- def using_kaminari
26
- setup
27
- Kaminari.paginate_array(@array).page(params[:page][:number]).per(params[:page][:size])
28
- end
29
-
30
- def using_will_paginate
31
- setup
32
- @array.paginate(page: params[:page][:number], per_page: params[:page][:size])
33
- end
34
-
35
- def render_pagination_using_kaminari
36
- render json: using_kaminari, adapter: :json_api
37
- end
38
-
39
- def render_pagination_using_will_paginate
40
- render json: using_will_paginate, adapter: :json_api
41
- end
42
-
43
- def render_array_without_pagination_links
44
- setup
45
- render json: @array, adapter: :json_api
46
- end
47
- end
48
-
49
- tests PaginationTestController
50
-
51
- def test_render_pagination_links_with_will_paginate
52
- expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
53
- 'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
54
- 'prev' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
55
- 'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
56
- 'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
57
-
58
- get :render_pagination_using_will_paginate, params: { page: { number: 2, size: 1 } }
59
- response = JSON.parse(@response.body)
60
- assert_equal expected_links, response['links']
61
- end
62
-
63
- def test_render_only_first_last_and_next_pagination_links
64
- expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
65
- 'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
66
- 'prev' => nil,
67
- 'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
68
- 'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" }
69
- get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 } }
70
- response = JSON.parse(@response.body)
71
- assert_equal expected_links, response['links']
72
- end
73
-
74
- def test_render_pagination_links_with_kaminari
75
- expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
76
- 'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
77
- 'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
78
- 'next' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
79
- 'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
80
- get :render_pagination_using_kaminari, params: { page: { number: 2, size: 1 } }
81
- response = JSON.parse(@response.body)
82
- assert_equal expected_links, response['links']
83
- end
84
-
85
- def test_render_only_prev_first_and_last_pagination_links
86
- expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
87
- 'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
88
- 'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
89
- 'next' => nil,
90
- 'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
91
- get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 } }
92
- response = JSON.parse(@response.body)
93
- assert_equal expected_links, response['links']
94
- end
95
-
96
- def test_render_only_first_last_and_next_pagination_links_with_additional_params
97
- expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
98
- 'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
99
- 'prev' => nil,
100
- 'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
101
- 'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" }
102
- get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 }, teste: 'additional' }
103
- response = JSON.parse(@response.body)
104
- assert_equal expected_links, response['links']
105
- end
106
-
107
- def test_render_only_prev_first_and_last_pagination_links_with_additional_params
108
- expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
109
- 'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
110
- 'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional",
111
- 'next' => nil,
112
- 'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional" }
113
- get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 }, teste: 'additional' }
114
- response = JSON.parse(@response.body)
115
- assert_equal expected_links, response['links']
116
- end
117
-
118
- def test_array_without_pagination_links
119
- get :render_array_without_pagination_links, params: { page: { number: 2, size: 1 } }
120
- response = JSON.parse(@response.body)
121
- refute response.key? 'links'
122
- end
123
- end
124
- end
125
- end
126
- end