active_model_serializers 0.10.3 → 0.10.12

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