active_model_serializers 0.10.0 → 0.10.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (168) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -5
  3. data/.travis.yml +17 -5
  4. data/CHANGELOG.md +126 -2
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +5 -2
  7. data/README.md +166 -26
  8. data/Rakefile +3 -32
  9. data/active_model_serializers.gemspec +22 -25
  10. data/appveyor.yml +9 -3
  11. data/bin/rubocop +38 -0
  12. data/docs/README.md +2 -1
  13. data/docs/general/adapters.md +29 -11
  14. data/docs/general/caching.md +7 -1
  15. data/docs/general/configuration_options.md +70 -1
  16. data/docs/general/deserialization.md +1 -1
  17. data/docs/general/fields.md +31 -0
  18. data/docs/general/getting_started.md +1 -1
  19. data/docs/general/logging.md +7 -0
  20. data/docs/general/rendering.md +62 -24
  21. data/docs/general/serializers.md +121 -13
  22. data/docs/howto/add_pagination_links.md +16 -17
  23. data/docs/howto/add_relationship_links.md +140 -0
  24. data/docs/howto/add_root_key.md +4 -0
  25. data/docs/howto/grape_integration.md +42 -0
  26. data/docs/howto/outside_controller_use.md +12 -4
  27. data/docs/howto/passing_arbitrary_options.md +2 -2
  28. data/docs/howto/serialize_poro.md +46 -5
  29. data/docs/howto/test.md +2 -0
  30. data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
  31. data/docs/integrations/ember-and-json-api.md +67 -32
  32. data/docs/jsonapi/schema.md +1 -1
  33. data/lib/action_controller/serialization.rb +13 -3
  34. data/lib/active_model/serializer/adapter/base.rb +2 -0
  35. data/lib/active_model/serializer/array_serializer.rb +8 -5
  36. data/lib/active_model/serializer/association.rb +62 -10
  37. data/lib/active_model/serializer/belongs_to_reflection.rb +4 -3
  38. data/lib/active_model/serializer/collection_serializer.rb +35 -12
  39. data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +82 -115
  40. data/lib/active_model/serializer/error_serializer.rb +11 -7
  41. data/lib/active_model/serializer/errors_serializer.rb +25 -20
  42. data/lib/active_model/serializer/has_many_reflection.rb +3 -3
  43. data/lib/active_model/serializer/has_one_reflection.rb +1 -4
  44. data/lib/active_model/serializer/lazy_association.rb +95 -0
  45. data/lib/active_model/serializer/lint.rb +134 -130
  46. data/lib/active_model/serializer/reflection.rb +127 -67
  47. data/lib/active_model/serializer/version.rb +1 -1
  48. data/lib/active_model/serializer.rb +296 -79
  49. data/lib/active_model_serializers/adapter/attributes.rb +3 -66
  50. data/lib/active_model_serializers/adapter/base.rb +39 -39
  51. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +1 -1
  52. data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
  53. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
  54. data/lib/active_model_serializers/adapter/json_api/relationship.rb +63 -23
  55. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +32 -9
  56. data/lib/active_model_serializers/adapter/json_api.rb +71 -57
  57. data/lib/active_model_serializers/adapter.rb +6 -0
  58. data/lib/active_model_serializers/deprecate.rb +1 -2
  59. data/lib/active_model_serializers/deserialization.rb +2 -0
  60. data/lib/active_model_serializers/lookup_chain.rb +80 -0
  61. data/lib/active_model_serializers/model.rb +109 -28
  62. data/lib/active_model_serializers/railtie.rb +3 -1
  63. data/lib/active_model_serializers/register_jsonapi_renderer.rb +44 -31
  64. data/lib/active_model_serializers/serializable_resource.rb +6 -5
  65. data/lib/active_model_serializers/serialization_context.rb +10 -3
  66. data/lib/active_model_serializers/test/schema.rb +2 -2
  67. data/lib/active_model_serializers.rb +15 -0
  68. data/lib/generators/rails/resource_override.rb +1 -1
  69. data/lib/generators/rails/serializer_generator.rb +4 -4
  70. data/lib/grape/active_model_serializers.rb +7 -5
  71. data/lib/grape/formatters/active_model_serializers.rb +19 -2
  72. data/lib/grape/helpers/active_model_serializers.rb +1 -0
  73. data/lib/tasks/rubocop.rake +53 -0
  74. data/test/action_controller/adapter_selector_test.rb +14 -5
  75. data/test/action_controller/explicit_serializer_test.rb +5 -4
  76. data/test/action_controller/json/include_test.rb +106 -27
  77. data/test/action_controller/json_api/errors_test.rb +8 -9
  78. data/test/action_controller/json_api/fields_test.rb +66 -0
  79. data/test/action_controller/json_api/linked_test.rb +29 -24
  80. data/test/action_controller/json_api/pagination_test.rb +19 -19
  81. data/test/action_controller/json_api/transform_test.rb +11 -3
  82. data/test/action_controller/lookup_proc_test.rb +49 -0
  83. data/test/action_controller/namespace_lookup_test.rb +232 -0
  84. data/test/action_controller/serialization_scope_name_test.rb +12 -6
  85. data/test/action_controller/serialization_test.rb +12 -9
  86. data/test/active_model_serializers/json_pointer_test.rb +15 -13
  87. data/test/active_model_serializers/model_test.rb +137 -4
  88. data/test/active_model_serializers/railtie_test_isolated.rb +12 -7
  89. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
  90. data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
  91. data/test/active_model_serializers/test/schema_test.rb +3 -2
  92. data/test/adapter/attributes_test.rb +40 -0
  93. data/test/adapter/json/collection_test.rb +14 -0
  94. data/test/adapter/json/has_many_test.rb +10 -2
  95. data/test/adapter/json/transform_test.rb +15 -15
  96. data/test/adapter/json_api/collection_test.rb +4 -3
  97. data/test/adapter/json_api/errors_test.rb +17 -19
  98. data/test/adapter/json_api/fields_test.rb +12 -3
  99. data/test/adapter/json_api/has_many_test.rb +49 -20
  100. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +183 -0
  101. data/test/adapter/json_api/json_api_test.rb +5 -7
  102. data/test/adapter/json_api/linked_test.rb +33 -12
  103. data/test/adapter/json_api/links_test.rb +4 -2
  104. data/test/adapter/json_api/pagination_links_test.rb +35 -8
  105. data/test/adapter/json_api/relationship_test.rb +309 -73
  106. data/test/adapter/json_api/resource_identifier_test.rb +27 -2
  107. data/test/adapter/json_api/resource_meta_test.rb +3 -3
  108. data/test/adapter/json_api/transform_test.rb +263 -253
  109. data/test/adapter/json_api/type_test.rb +1 -1
  110. data/test/adapter/json_test.rb +8 -7
  111. data/test/adapter/null_test.rb +1 -2
  112. data/test/adapter/polymorphic_test.rb +5 -5
  113. data/test/adapter_test.rb +1 -1
  114. data/test/benchmark/app.rb +1 -1
  115. data/test/benchmark/benchmarking_support.rb +1 -1
  116. data/test/benchmark/bm_active_record.rb +81 -0
  117. data/test/benchmark/bm_adapter.rb +38 -0
  118. data/test/benchmark/bm_caching.rb +16 -16
  119. data/test/benchmark/bm_lookup_chain.rb +83 -0
  120. data/test/benchmark/bm_transform.rb +21 -10
  121. data/test/benchmark/controllers.rb +16 -17
  122. data/test/benchmark/fixtures.rb +72 -72
  123. data/test/cache_test.rb +235 -69
  124. data/test/collection_serializer_test.rb +25 -12
  125. data/test/fixtures/active_record.rb +45 -10
  126. data/test/fixtures/poro.rb +124 -181
  127. data/test/generators/serializer_generator_test.rb +23 -5
  128. data/test/grape_test.rb +170 -56
  129. data/test/lint_test.rb +1 -1
  130. data/test/logger_test.rb +13 -11
  131. data/test/serializable_resource_test.rb +18 -22
  132. data/test/serializers/association_macros_test.rb +3 -2
  133. data/test/serializers/associations_test.rb +178 -49
  134. data/test/serializers/attribute_test.rb +5 -3
  135. data/test/serializers/attributes_test.rb +1 -1
  136. data/test/serializers/caching_configuration_test_isolated.rb +6 -6
  137. data/test/serializers/fieldset_test.rb +1 -1
  138. data/test/serializers/meta_test.rb +12 -6
  139. data/test/serializers/options_test.rb +17 -6
  140. data/test/serializers/read_attribute_for_serialization_test.rb +3 -3
  141. data/test/serializers/reflection_test.rb +427 -0
  142. data/test/serializers/root_test.rb +1 -1
  143. data/test/serializers/serialization_test.rb +2 -2
  144. data/test/serializers/serializer_for_test.rb +12 -10
  145. data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
  146. data/test/support/isolated_unit.rb +5 -2
  147. data/test/support/rails5_shims.rb +8 -2
  148. data/test/support/rails_app.rb +2 -9
  149. data/test/support/serialization_testing.rb +23 -5
  150. data/test/test_helper.rb +13 -0
  151. metadata +105 -42
  152. data/.rubocop_todo.yml +0 -167
  153. data/docs/ARCHITECTURE.md +0 -126
  154. data/lib/active_model/serializer/associations.rb +0 -100
  155. data/lib/active_model/serializer/attributes.rb +0 -82
  156. data/lib/active_model/serializer/collection_reflection.rb +0 -7
  157. data/lib/active_model/serializer/configuration.rb +0 -35
  158. data/lib/active_model/serializer/include_tree.rb +0 -111
  159. data/lib/active_model/serializer/links.rb +0 -35
  160. data/lib/active_model/serializer/meta.rb +0 -29
  161. data/lib/active_model/serializer/singular_reflection.rb +0 -7
  162. data/lib/active_model/serializer/type.rb +0 -25
  163. data/lib/active_model_serializers/key_transform.rb +0 -70
  164. data/test/active_model_serializers/key_transform_test.rb +0 -263
  165. data/test/adapter/json_api/relationships_test.rb +0 -199
  166. data/test/include_tree/from_include_args_test.rb +0 -26
  167. data/test/include_tree/from_string_test.rb +0 -94
  168. data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -0,0 +1,183 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ module Adapter
6
+ class JsonApi
7
+ class IncludeParamTest < ActiveSupport::TestCase
8
+ IncludeParamAuthor = Class.new(::Model) do
9
+ associations :tags, :posts
10
+ end
11
+
12
+ class CustomCommentLoader
13
+ def all
14
+ [{ foo: 'bar' }]
15
+ end
16
+ end
17
+ class Tag < ::Model
18
+ attributes :id, :name
19
+ end
20
+
21
+ class TagSerializer < ActiveModel::Serializer
22
+ type 'tags'
23
+ attributes :id, :name
24
+ end
25
+
26
+ class PostWithTagsSerializer < ActiveModel::Serializer
27
+ type 'posts'
28
+ attributes :id
29
+ has_many :tags
30
+ end
31
+
32
+ class IncludeParamAuthorSerializer < ActiveModel::Serializer
33
+ class_attribute :comment_loader
34
+
35
+ has_many :tags, serializer: TagSerializer do
36
+ link :self, '//example.com/link_author/relationships/tags'
37
+ include_data :if_sideloaded
38
+ end
39
+
40
+ has_many :unlinked_tags, serializer: TagSerializer do
41
+ include_data :if_sideloaded
42
+ end
43
+
44
+ has_many :posts, serializer: PostWithTagsSerializer do
45
+ include_data :if_sideloaded
46
+ end
47
+ has_many :locations do
48
+ include_data :if_sideloaded
49
+ end
50
+ has_many :comments do
51
+ include_data :if_sideloaded
52
+ IncludeParamAuthorSerializer.comment_loader.all
53
+ end
54
+ end
55
+
56
+ def setup
57
+ IncludeParamAuthorSerializer.comment_loader = Class.new(CustomCommentLoader).new
58
+ @tag = Tag.new(id: 1337, name: 'mytag')
59
+ @author = IncludeParamAuthor.new(
60
+ id: 1337,
61
+ tags: [@tag]
62
+ )
63
+ end
64
+
65
+ def test_relationship_not_loaded_when_not_included
66
+ expected = {
67
+ links: {
68
+ self: '//example.com/link_author/relationships/tags'
69
+ }
70
+ }
71
+
72
+ @author.define_singleton_method(:read_attribute_for_serialization) do |attr|
73
+ fail 'should not be called' if attr == :tags
74
+ super(attr)
75
+ end
76
+
77
+ assert_relationship(:tags, expected)
78
+ end
79
+
80
+ def test_relationship_included
81
+ expected = {
82
+ data: [
83
+ {
84
+ id: '1337',
85
+ type: 'tags'
86
+ }
87
+ ],
88
+ links: {
89
+ self: '//example.com/link_author/relationships/tags'
90
+ }
91
+ }
92
+
93
+ assert_relationship(:tags, expected, include: :tags)
94
+ end
95
+
96
+ def test_sideloads_included
97
+ expected = [
98
+ {
99
+ id: '1337',
100
+ type: 'tags',
101
+ attributes: { name: 'mytag' }
102
+ }
103
+ ]
104
+ hash = result(include: :tags)
105
+ assert_equal(expected, hash[:included])
106
+ end
107
+
108
+ def test_nested_relationship
109
+ expected = {
110
+ data: [
111
+ {
112
+ id: '1337',
113
+ type: 'tags'
114
+ }
115
+ ],
116
+ links: {
117
+ self: '//example.com/link_author/relationships/tags'
118
+ }
119
+ }
120
+
121
+ expected_no_data = {
122
+ links: {
123
+ self: '//example.com/link_author/relationships/tags'
124
+ }
125
+ }
126
+
127
+ assert_relationship(:tags, expected, include: [:tags, { posts: :tags }])
128
+
129
+ @author.define_singleton_method(:read_attribute_for_serialization) do |attr|
130
+ fail 'should not be called' if attr == :tags
131
+ super(attr)
132
+ end
133
+
134
+ assert_relationship(:tags, expected_no_data, include: { posts: :tags })
135
+ end
136
+
137
+ def test_include_params_with_no_block
138
+ @author.define_singleton_method(:read_attribute_for_serialization) do |attr|
139
+ fail 'should not be called' if attr == :locations
140
+ super(attr)
141
+ end
142
+
143
+ expected = { meta: {} }
144
+
145
+ assert_relationship(:locations, expected)
146
+ end
147
+
148
+ def test_block_relationship
149
+ expected = {
150
+ data: [
151
+ { 'foo' => 'bar' }
152
+ ]
153
+ }
154
+
155
+ assert_relationship(:comments, expected, include: [:comments])
156
+ end
157
+
158
+ def test_node_not_included_when_no_link
159
+ expected = { meta: {} }
160
+ assert_relationship(:unlinked_tags, expected, key_transform: :unaltered)
161
+ end
162
+
163
+ private
164
+
165
+ def assert_relationship(relationship_name, expected, opts = {})
166
+ actual = relationship_data(relationship_name, opts)
167
+ assert_equal(expected, actual)
168
+ end
169
+
170
+ def result(opts)
171
+ opts = { adapter: :json_api }.merge(opts)
172
+ serializable(@author, opts).serializable_hash
173
+ end
174
+
175
+ def relationship_data(relationship_name, opts = {})
176
+ hash = result(opts)
177
+ hash[:data][:relationships][relationship_name]
178
+ end
179
+ end
180
+ end
181
+ end
182
+ end
183
+ end
@@ -22,13 +22,11 @@ module ActiveModelSerializers
22
22
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
23
23
 
24
24
  assert_equal({
25
- reviews: { data: [
26
- { type: 'comments', id: '1' },
27
- { type: 'comments', id: '2' }
28
- ] },
29
- writer: { data: { type: 'authors', id: '1' } },
30
- site: { data: { type: 'blogs', id: '1' } }
31
- }, adapter.serializable_hash[:data][:relationships])
25
+ reviews: { data: [{ type: 'comments', id: '1' },
26
+ { type: 'comments', id: '2' }] },
27
+ writer: { data: { type: 'authors', id: '1' } },
28
+ site: { data: { type: 'blogs', id: '1' } }
29
+ }, adapter.serializable_hash[:data][:relationships])
32
30
  end
33
31
  end
34
32
  end
@@ -1,10 +1,9 @@
1
1
  require 'test_helper'
2
2
 
3
- NestedPost = Class.new(Model)
3
+ class NestedPost < ::Model; associations :nested_posts end
4
4
  class NestedPostSerializer < ActiveModel::Serializer
5
5
  has_many :nested_posts
6
6
  end
7
-
8
7
  module ActiveModelSerializers
9
8
  module Adapter
10
9
  class JsonApi
@@ -17,7 +16,7 @@ module ActiveModelSerializers
17
16
  @first_post = Post.new(id: 10, title: 'Hello!!', body: 'Hello, world!!')
18
17
  @second_post = Post.new(id: 20, title: 'New Post', body: 'Body')
19
18
  @third_post = Post.new(id: 30, title: 'Yet Another Post', body: 'Body')
20
- @blog = Blog.new({ name: 'AMS Blog' })
19
+ @blog = Blog.new(name: 'AMS Blog')
21
20
  @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
22
21
  @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
23
22
  @first_post.blog = @blog
@@ -224,6 +223,25 @@ module ActiveModelSerializers
224
223
  assert_equal expected, relationships
225
224
  end
226
225
 
226
+ def test_underscore_model_namespace_with_namespace_separator_for_linked_resource_type
227
+ spammy_post = Post.new(id: 123)
228
+ spammy_post.related = [Spam::UnrelatedLink.new(id: 456)]
229
+ serializer = SpammyPostSerializer.new(spammy_post)
230
+ adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
231
+ relationships = with_namespace_separator '--' do
232
+ adapter.serializable_hash[:data][:relationships]
233
+ end
234
+ expected = {
235
+ related: {
236
+ data: [{
237
+ type: 'spam--unrelated-links',
238
+ id: '456'
239
+ }]
240
+ }
241
+ }
242
+ assert_equal expected, relationships
243
+ end
244
+
227
245
  def test_multiple_references_to_same_resource
228
246
  serializer = ActiveModel::Serializer::CollectionSerializer.new([@first_comment, @second_comment])
229
247
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(
@@ -283,8 +301,8 @@ module ActiveModelSerializers
283
301
  end
284
302
 
285
303
  class NoDuplicatesTest < ActiveSupport::TestCase
286
- Post = Class.new(::Model)
287
- Author = Class.new(::Model)
304
+ class Post < ::Model; associations :author end
305
+ class Author < ::Model; associations :posts, :roles, :bio end
288
306
 
289
307
  class PostSerializer < ActiveModel::Serializer
290
308
  type 'posts'
@@ -303,8 +321,8 @@ module ActiveModelSerializers
303
321
  @author.posts << @post1
304
322
  @author.posts << @post2
305
323
 
306
- @nestedpost1 = ::NestedPost.new(id: 1, nested_posts: [])
307
- @nestedpost2 = ::NestedPost.new(id: 2, nested_posts: [])
324
+ @nestedpost1 = NestedPost.new(id: 1, nested_posts: [])
325
+ @nestedpost2 = NestedPost.new(id: 2, nested_posts: [])
308
326
  @nestedpost1.nested_posts << @nestedpost1
309
327
  @nestedpost1.nested_posts << @nestedpost2
310
328
  @nestedpost2.nested_posts << @nestedpost1
@@ -341,9 +359,10 @@ module ActiveModelSerializers
341
359
 
342
360
  def test_no_duplicates_collection
343
361
  hash = ActiveModelSerializers::SerializableResource.new(
344
- [@post1, @post2], adapter: :json_api,
345
- include: '*.*')
346
- .serializable_hash
362
+ [@post1, @post2],
363
+ adapter: :json_api,
364
+ include: '*.*'
365
+ ).serializable_hash
347
366
  expected = [
348
367
  {
349
368
  type: 'authors', id: '1',
@@ -364,7 +383,8 @@ module ActiveModelSerializers
364
383
  hash = ActiveModelSerializers::SerializableResource.new(
365
384
  @nestedpost1,
366
385
  adapter: :json_api,
367
- include: '*').serializable_hash
386
+ include: '*'
387
+ ).serializable_hash
368
388
  expected = [
369
389
  type: 'nested-posts', id: '2',
370
390
  relationships: {
@@ -383,7 +403,8 @@ module ActiveModelSerializers
383
403
  hash = ActiveModelSerializers::SerializableResource.new(
384
404
  [@nestedpost1, @nestedpost2],
385
405
  adapter: :json_api,
386
- include: '*').serializable_hash
406
+ include: '*'
407
+ ).serializable_hash
387
408
  assert_nil(hash[:included])
388
409
  end
389
410
  end
@@ -4,7 +4,7 @@ module ActiveModelSerializers
4
4
  module Adapter
5
5
  class JsonApi
6
6
  class LinksTest < ActiveSupport::TestCase
7
- LinkAuthor = Class.new(::Model)
7
+ class LinkAuthor < ::Model; associations :posts end
8
8
  class LinkAuthorSerializer < ActiveModel::Serializer
9
9
  link :self do
10
10
  href "http://example.com/link_author/#{object.id}"
@@ -17,6 +17,7 @@ module ActiveModelSerializers
17
17
  link :yet_another do
18
18
  "http://example.com/resource/#{object.id}"
19
19
  end
20
+ link(:nil) { nil }
20
21
  end
21
22
 
22
23
  def setup
@@ -40,7 +41,8 @@ module ActiveModelSerializers
40
41
  stuff: 'value'
41
42
  }
42
43
  }
43
- }).serializable_hash
44
+ }
45
+ ).serializable_hash
44
46
  expected = {
45
47
  self: {
46
48
  href: 'http://example.com/posts',
@@ -13,11 +13,11 @@ module ActiveModelSerializers
13
13
  def setup
14
14
  ActionController::Base.cache_store.clear
15
15
  @array = [
16
- Profile.new({ id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
17
- Profile.new({ id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
18
- Profile.new({ id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3' }),
19
- Profile.new({ id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4' }),
20
- Profile.new({ id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5' })
16
+ Profile.new(id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
17
+ Profile.new(id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2'),
18
+ Profile.new(id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3'),
19
+ Profile.new(id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4'),
20
+ Profile.new(id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5')
21
21
  ]
22
22
  end
23
23
 
@@ -43,7 +43,8 @@ module ActiveModelSerializers
43
43
  end
44
44
 
45
45
  def data
46
- { data: [
46
+ {
47
+ data: [
47
48
  { id: '1', type: 'profiles', attributes: { name: 'Name 1', description: 'Description 1' } },
48
49
  { id: '2', type: 'profiles', attributes: { name: 'Name 2', description: 'Description 2' } },
49
50
  { id: '3', type: 'profiles', attributes: { name: 'Name 3', description: 'Description 3' } },
@@ -75,7 +76,7 @@ module ActiveModelSerializers
75
76
  }
76
77
  end
77
78
 
78
- def expected_response_without_pagination_links
79
+ def expected_response_when_unpaginatable
79
80
  data
80
81
  end
81
82
 
@@ -86,6 +87,12 @@ module ActiveModelSerializers
86
87
  end
87
88
  end
88
89
 
90
+ def expected_response_without_pagination_links
91
+ {}.tap do |hash|
92
+ hash[:data] = data.values.flatten[2..3]
93
+ end
94
+ end
95
+
89
96
  def expected_response_with_pagination_links_and_additional_params
90
97
  new_links = links[:links].each_with_object({}) { |(key, value), hash| hash[key] = "#{value}&test=test" }
91
98
  {}.tap do |hash|
@@ -121,7 +128,7 @@ module ActiveModelSerializers
121
128
  end
122
129
 
123
130
  def test_pagination_links_with_additional_params
124
- adapter = load_adapter(using_will_paginate, mock_request({ test: 'test' }))
131
+ adapter = load_adapter(using_will_paginate, mock_request(test: 'test'))
125
132
 
126
133
  assert_equal expected_response_with_pagination_links_and_additional_params,
127
134
  adapter.serializable_hash
@@ -158,7 +165,27 @@ module ActiveModelSerializers
158
165
  def test_not_showing_pagination_links
159
166
  adapter = load_adapter(@array, mock_request)
160
167
 
168
+ assert_equal expected_response_when_unpaginatable, adapter.serializable_hash
169
+ end
170
+
171
+ def test_raises_descriptive_error_when_serialization_context_unset
172
+ render_options = { adapter: :json_api }
173
+ adapter = serializable(using_kaminari, render_options)
174
+ exception = assert_raises do
175
+ adapter.as_json
176
+ end
177
+ exception_class = ActiveModelSerializers::Adapter::JsonApi::PaginationLinks::MissingSerializationContextError
178
+ assert_equal exception_class, exception.class
179
+ assert_match(/CollectionSerializer#paginated\?/, exception.message)
180
+ end
181
+
182
+ def test_pagination_links_not_present_when_disabled
183
+ ActiveModel::Serializer.config.jsonapi_pagination_links_enabled = false
184
+ adapter = load_adapter(using_kaminari, mock_request)
185
+
161
186
  assert_equal expected_response_without_pagination_links, adapter.serializable_hash
187
+ ensure
188
+ ActiveModel::Serializer.config.jsonapi_pagination_links_enabled = true
162
189
  end
163
190
  end
164
191
  end