active_model_serializers 0.10.6 → 0.10.13

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 (215) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +114 -1
  3. data/README.md +21 -24
  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/adapter/attributes.rb +2 -0
  7. data/lib/active_model/serializer/adapter/base.rb +2 -0
  8. data/lib/active_model/serializer/adapter/json.rb +2 -0
  9. data/lib/active_model/serializer/adapter/json_api.rb +2 -0
  10. data/lib/active_model/serializer/adapter/null.rb +2 -0
  11. data/lib/active_model/serializer/adapter.rb +2 -0
  12. data/lib/active_model/serializer/array_serializer.rb +2 -0
  13. data/lib/active_model/serializer/association.rb +2 -0
  14. data/lib/active_model/serializer/attribute.rb +2 -0
  15. data/lib/active_model/serializer/belongs_to_reflection.rb +2 -0
  16. data/lib/active_model/serializer/collection_serializer.rb +17 -5
  17. data/lib/active_model/serializer/concerns/caching.rb +7 -2
  18. data/lib/active_model/serializer/error_serializer.rb +2 -0
  19. data/lib/active_model/serializer/errors_serializer.rb +2 -0
  20. data/lib/active_model/serializer/field.rb +2 -0
  21. data/lib/active_model/serializer/fieldset.rb +3 -1
  22. data/lib/active_model/serializer/has_many_reflection.rb +2 -0
  23. data/lib/active_model/serializer/has_one_reflection.rb +2 -0
  24. data/lib/active_model/serializer/lazy_association.rb +6 -2
  25. data/lib/active_model/serializer/link.rb +23 -0
  26. data/lib/active_model/serializer/lint.rb +2 -0
  27. data/lib/active_model/serializer/null.rb +2 -0
  28. data/lib/active_model/serializer/reflection.rb +10 -5
  29. data/lib/active_model/serializer/version.rb +3 -1
  30. data/lib/active_model/serializer.rb +37 -19
  31. data/lib/active_model_serializers/adapter/attributes.rb +23 -0
  32. data/lib/active_model_serializers/adapter/base.rb +2 -0
  33. data/lib/active_model_serializers/adapter/json.rb +2 -0
  34. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +3 -1
  35. data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
  36. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
  37. data/lib/active_model_serializers/adapter/json_api/link.rb +2 -0
  38. data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
  39. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +45 -20
  40. data/lib/active_model_serializers/adapter/json_api/relationship.rb +18 -4
  41. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +33 -25
  42. data/lib/active_model_serializers/adapter/json_api.rb +14 -9
  43. data/lib/active_model_serializers/adapter/null.rb +2 -0
  44. data/lib/active_model_serializers/adapter.rb +3 -1
  45. data/lib/active_model_serializers/callbacks.rb +2 -0
  46. data/lib/active_model_serializers/deprecate.rb +2 -0
  47. data/lib/active_model_serializers/deserialization.rb +2 -0
  48. data/lib/active_model_serializers/json_pointer.rb +2 -0
  49. data/lib/active_model_serializers/logging.rb +2 -0
  50. data/lib/active_model_serializers/lookup_chain.rb +2 -0
  51. data/lib/active_model_serializers/model/caching.rb +26 -0
  52. data/lib/active_model_serializers/model.rb +2 -0
  53. data/lib/active_model_serializers/railtie.rb +4 -0
  54. data/lib/active_model_serializers/register_jsonapi_renderer.rb +2 -0
  55. data/lib/active_model_serializers/serializable_resource.rb +4 -2
  56. data/lib/active_model_serializers/serialization_context.rb +2 -0
  57. data/lib/active_model_serializers/test/schema.rb +2 -0
  58. data/lib/active_model_serializers/test/serializer.rb +2 -0
  59. data/lib/active_model_serializers/test.rb +2 -0
  60. data/lib/active_model_serializers.rb +21 -11
  61. data/lib/generators/rails/resource_override.rb +2 -0
  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 +2 -0
  67. metadata +43 -298
  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 -105
  72. data/.simplecov +0 -110
  73. data/.travis.yml +0 -55
  74. data/CODE_OF_CONDUCT.md +0 -74
  75. data/CONTRIBUTING.md +0 -105
  76. data/Gemfile +0 -56
  77. data/Rakefile +0 -74
  78. data/active_model_serializers.gemspec +0 -63
  79. data/appveyor.yml +0 -30
  80. data/bin/bench +0 -171
  81. data/bin/bench_regression +0 -316
  82. data/bin/rubocop +0 -38
  83. data/bin/serve_benchmark +0 -39
  84. data/docs/README.md +0 -41
  85. data/docs/STYLE.md +0 -58
  86. data/docs/general/adapters.md +0 -263
  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 -21
  95. data/docs/general/rendering.md +0 -293
  96. data/docs/general/serializers.md +0 -480
  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 -140
  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 -66
  103. data/docs/howto/passing_arbitrary_options.md +0 -27
  104. data/docs/howto/serialize_poro.md +0 -73
  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 -147
  108. data/docs/integrations/grape.md +0 -19
  109. data/docs/jsonapi/errors.md +0 -56
  110. data/docs/jsonapi/schema/schema.json +0 -366
  111. data/docs/jsonapi/schema.md +0 -151
  112. data/docs/rfcs/0000-namespace.md +0 -106
  113. data/docs/rfcs/template.md +0 -15
  114. data/test/action_controller/adapter_selector_test.rb +0 -62
  115. data/test/action_controller/explicit_serializer_test.rb +0 -135
  116. data/test/action_controller/json/include_test.rb +0 -246
  117. data/test/action_controller/json_api/deserialization_test.rb +0 -112
  118. data/test/action_controller/json_api/errors_test.rb +0 -40
  119. data/test/action_controller/json_api/fields_test.rb +0 -66
  120. data/test/action_controller/json_api/linked_test.rb +0 -202
  121. data/test/action_controller/json_api/pagination_test.rb +0 -116
  122. data/test/action_controller/json_api/transform_test.rb +0 -189
  123. data/test/action_controller/lookup_proc_test.rb +0 -49
  124. data/test/action_controller/namespace_lookup_test.rb +0 -232
  125. data/test/action_controller/serialization_scope_name_test.rb +0 -235
  126. data/test/action_controller/serialization_test.rb +0 -472
  127. data/test/active_model_serializers/adapter_for_test.rb +0 -208
  128. data/test/active_model_serializers/json_pointer_test.rb +0 -22
  129. data/test/active_model_serializers/logging_test.rb +0 -77
  130. data/test/active_model_serializers/model_test.rb +0 -142
  131. data/test/active_model_serializers/railtie_test_isolated.rb +0 -68
  132. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -161
  133. data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -71
  134. data/test/active_model_serializers/test/schema_test.rb +0 -131
  135. data/test/active_model_serializers/test/serializer_test.rb +0 -62
  136. data/test/active_record_test.rb +0 -9
  137. data/test/adapter/attributes_test.rb +0 -40
  138. data/test/adapter/deprecation_test.rb +0 -100
  139. data/test/adapter/json/belongs_to_test.rb +0 -45
  140. data/test/adapter/json/collection_test.rb +0 -104
  141. data/test/adapter/json/has_many_test.rb +0 -53
  142. data/test/adapter/json/transform_test.rb +0 -93
  143. data/test/adapter/json_api/belongs_to_test.rb +0 -155
  144. data/test/adapter/json_api/collection_test.rb +0 -96
  145. data/test/adapter/json_api/errors_test.rb +0 -76
  146. data/test/adapter/json_api/fields_test.rb +0 -96
  147. data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
  148. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
  149. data/test/adapter/json_api/has_many_test.rb +0 -173
  150. data/test/adapter/json_api/has_one_test.rb +0 -80
  151. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -183
  152. data/test/adapter/json_api/json_api_test.rb +0 -33
  153. data/test/adapter/json_api/linked_test.rb +0 -413
  154. data/test/adapter/json_api/links_test.rb +0 -95
  155. data/test/adapter/json_api/pagination_links_test.rb +0 -193
  156. data/test/adapter/json_api/parse_test.rb +0 -137
  157. data/test/adapter/json_api/relationship_test.rb +0 -397
  158. data/test/adapter/json_api/resource_identifier_test.rb +0 -110
  159. data/test/adapter/json_api/resource_meta_test.rb +0 -100
  160. data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
  161. data/test/adapter/json_api/transform_test.rb +0 -512
  162. data/test/adapter/json_api/type_test.rb +0 -61
  163. data/test/adapter/json_test.rb +0 -46
  164. data/test/adapter/null_test.rb +0 -22
  165. data/test/adapter/polymorphic_test.rb +0 -171
  166. data/test/adapter_test.rb +0 -67
  167. data/test/array_serializer_test.rb +0 -22
  168. data/test/benchmark/app.rb +0 -65
  169. data/test/benchmark/benchmarking_support.rb +0 -67
  170. data/test/benchmark/bm_active_record.rb +0 -81
  171. data/test/benchmark/bm_adapter.rb +0 -38
  172. data/test/benchmark/bm_caching.rb +0 -119
  173. data/test/benchmark/bm_lookup_chain.rb +0 -83
  174. data/test/benchmark/bm_transform.rb +0 -45
  175. data/test/benchmark/config.ru +0 -3
  176. data/test/benchmark/controllers.rb +0 -83
  177. data/test/benchmark/fixtures.rb +0 -219
  178. data/test/cache_test.rb +0 -651
  179. data/test/collection_serializer_test.rb +0 -123
  180. data/test/fixtures/active_record.rb +0 -113
  181. data/test/fixtures/poro.rb +0 -225
  182. data/test/generators/scaffold_controller_generator_test.rb +0 -24
  183. data/test/generators/serializer_generator_test.rb +0 -75
  184. data/test/grape_test.rb +0 -196
  185. data/test/lint_test.rb +0 -49
  186. data/test/logger_test.rb +0 -20
  187. data/test/poro_test.rb +0 -9
  188. data/test/serializable_resource_test.rb +0 -79
  189. data/test/serializers/association_macros_test.rb +0 -37
  190. data/test/serializers/associations_test.rb +0 -424
  191. data/test/serializers/attribute_test.rb +0 -153
  192. data/test/serializers/attributes_test.rb +0 -52
  193. data/test/serializers/caching_configuration_test_isolated.rb +0 -170
  194. data/test/serializers/configuration_test.rb +0 -32
  195. data/test/serializers/fieldset_test.rb +0 -14
  196. data/test/serializers/meta_test.rb +0 -202
  197. data/test/serializers/options_test.rb +0 -32
  198. data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
  199. data/test/serializers/reflection_test.rb +0 -427
  200. data/test/serializers/root_test.rb +0 -21
  201. data/test/serializers/serialization_test.rb +0 -55
  202. data/test/serializers/serializer_for_test.rb +0 -136
  203. data/test/serializers/serializer_for_with_namespace_test.rb +0 -88
  204. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  205. data/test/support/isolated_unit.rb +0 -82
  206. data/test/support/rails5_shims.rb +0 -53
  207. data/test/support/rails_app.rb +0 -38
  208. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  209. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
  210. data/test/support/schemas/custom/show.json +0 -7
  211. data/test/support/schemas/hyper_schema.json +0 -93
  212. data/test/support/schemas/render_using_json_api.json +0 -43
  213. data/test/support/schemas/simple_json_pointers.json +0 -10
  214. data/test/support/serialization_testing.rb +0 -71
  215. data/test/test_helper.rb +0 -70
@@ -1,424 +0,0 @@
1
- require 'test_helper'
2
- module ActiveModel
3
- class Serializer
4
- class AssociationsTest < ActiveSupport::TestCase
5
- class ModelWithoutSerializer < ::Model
6
- attributes :id, :name
7
- end
8
-
9
- def setup
10
- @author = Author.new(name: 'Steve K.')
11
- @author.bio = nil
12
- @author.roles = []
13
- @blog = Blog.new(name: 'AMS Blog')
14
- @post = Post.new(title: 'New Post', body: 'Body')
15
- @tag = ModelWithoutSerializer.new(id: 'tagid', name: '#hashtagged')
16
- @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
17
- @post.comments = [@comment]
18
- @post.tags = [@tag]
19
- @post.blog = @blog
20
- @comment.post = @post
21
- @comment.author = nil
22
- @post.author = @author
23
- @author.posts = [@post]
24
-
25
- @post_serializer = PostSerializer.new(@post, custom_options: true)
26
- @author_serializer = AuthorSerializer.new(@author)
27
- @comment_serializer = CommentSerializer.new(@comment)
28
- end
29
-
30
- def test_has_many_and_has_one
31
- @author_serializer.associations.each do |association|
32
- key = association.key
33
- serializer = association.lazy_association.serializer
34
-
35
- case key
36
- when :posts
37
- assert_equal true, association.include_data?
38
- assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
39
- when :bio
40
- assert_equal true, association.include_data?
41
- assert_nil serializer
42
- when :roles
43
- assert_equal true, association.include_data?
44
- assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
45
- else
46
- flunk "Unknown association: #{key}"
47
- end
48
- end
49
- end
50
-
51
- def test_has_many_with_no_serializer
52
- post_serializer_class = Class.new(ActiveModel::Serializer) do
53
- attributes :id
54
- has_many :tags
55
- end
56
- post_serializer_class.new(@post).associations.each do |association|
57
- key = association.key
58
- serializer = association.lazy_association.serializer
59
-
60
- assert_equal :tags, key
61
- assert_nil serializer
62
- assert_equal [{ id: 'tagid', name: '#hashtagged' }].to_json, association.virtual_value.to_json
63
- end
64
- end
65
-
66
- def test_serializer_options_are_passed_into_associations_serializers
67
- association = @post_serializer
68
- .associations
69
- .detect { |assoc| assoc.key == :comments }
70
-
71
- comment_serializer = association.lazy_association.serializer.first
72
- class << comment_serializer
73
- def custom_options
74
- instance_options
75
- end
76
- end
77
- assert comment_serializer.custom_options.fetch(:custom_options)
78
- end
79
-
80
- def test_belongs_to
81
- @comment_serializer.associations.each do |association|
82
- key = association.key
83
- serializer = association.lazy_association.serializer
84
-
85
- case key
86
- when :post
87
- assert_kind_of(PostSerializer, serializer)
88
- when :author
89
- assert_nil serializer
90
- else
91
- flunk "Unknown association: #{key}"
92
- end
93
-
94
- assert_equal true, association.include_data?
95
- end
96
- end
97
-
98
- def test_belongs_to_with_custom_method
99
- assert(
100
- @post_serializer.associations.any? do |association|
101
- association.key == :blog
102
- end
103
- )
104
- end
105
-
106
- def test_associations_inheritance
107
- inherited_klass = Class.new(PostSerializer)
108
-
109
- assert_equal(PostSerializer._reflections, inherited_klass._reflections)
110
- end
111
-
112
- def test_associations_inheritance_with_new_association
113
- inherited_klass = Class.new(PostSerializer) do
114
- has_many :top_comments, serializer: CommentSerializer
115
- end
116
-
117
- assert(
118
- PostSerializer._reflections.values.all? do |reflection|
119
- inherited_klass._reflections.values.include?(reflection)
120
- end
121
- )
122
-
123
- assert(
124
- inherited_klass._reflections.values.any? do |reflection|
125
- reflection.name == :top_comments
126
- end
127
- )
128
- end
129
-
130
- def test_associations_custom_keys
131
- serializer = PostWithCustomKeysSerializer.new(@post)
132
-
133
- expected_association_keys = serializer.associations.map(&:key)
134
-
135
- assert expected_association_keys.include? :reviews
136
- assert expected_association_keys.include? :writer
137
- assert expected_association_keys.include? :site
138
- end
139
-
140
- class BelongsToBlogModel < ::Model
141
- attributes :id, :title
142
- associations :blog
143
- end
144
- class BelongsToBlogModelSerializer < ActiveModel::Serializer
145
- type :posts
146
- belongs_to :blog
147
- end
148
-
149
- def test_belongs_to_doesnt_load_record
150
- attributes = { id: 1, title: 'Belongs to Blog', blog: Blog.new(id: 5) }
151
- post = BelongsToBlogModel.new(attributes)
152
- class << post
153
- def blog
154
- fail 'should use blog_id'
155
- end
156
-
157
- def blog_id
158
- 5
159
- end
160
- end
161
-
162
- actual = serializable(post, adapter: :json_api, serializer: BelongsToBlogModelSerializer).as_json
163
- expected = { data: { id: '1', type: 'posts', relationships: { blog: { data: { id: '5', type: 'blogs' } } } } }
164
-
165
- assert_equal expected, actual
166
- end
167
-
168
- class InlineAssociationTestPostSerializer < ActiveModel::Serializer
169
- has_many :comments
170
- has_many :comments, key: :last_comments do
171
- object.comments.last(1)
172
- end
173
- end
174
-
175
- def test_virtual_attribute_block
176
- comment1 = ::ARModels::Comment.create!(contents: 'first comment')
177
- comment2 = ::ARModels::Comment.create!(contents: 'last comment')
178
- post = ::ARModels::Post.create!(
179
- title: 'inline association test',
180
- body: 'etc',
181
- comments: [comment1, comment2]
182
- )
183
- actual = serializable(post, adapter: :attributes, serializer: InlineAssociationTestPostSerializer).as_json
184
- expected = {
185
- comments: [
186
- { id: 1, contents: 'first comment' },
187
- { id: 2, contents: 'last comment' }
188
- ],
189
- last_comments: [
190
- { id: 2, contents: 'last comment' }
191
- ]
192
- }
193
-
194
- assert_equal expected, actual
195
- ensure
196
- ::ARModels::Post.delete_all
197
- ::ARModels::Comment.delete_all
198
- end
199
-
200
- class NamespacedResourcesTest < ActiveSupport::TestCase
201
- class ResourceNamespace
202
- class Post < ::Model
203
- associations :comments, :author, :description
204
- end
205
- class Comment < ::Model; end
206
- class Author < ::Model; end
207
- class Description < ::Model; end
208
- class PostSerializer < ActiveModel::Serializer
209
- has_many :comments
210
- belongs_to :author
211
- has_one :description
212
- end
213
- class CommentSerializer < ActiveModel::Serializer; end
214
- class AuthorSerializer < ActiveModel::Serializer; end
215
- class DescriptionSerializer < ActiveModel::Serializer; end
216
- end
217
-
218
- def setup
219
- @comment = ResourceNamespace::Comment.new
220
- @author = ResourceNamespace::Author.new
221
- @description = ResourceNamespace::Description.new
222
- @post = ResourceNamespace::Post.new(comments: [@comment],
223
- author: @author,
224
- description: @description)
225
- @post_serializer = ResourceNamespace::PostSerializer.new(@post)
226
- end
227
-
228
- def test_associations_namespaced_resources
229
- @post_serializer.associations.each do |association|
230
- case association.key
231
- when :comments
232
- assert_instance_of(ResourceNamespace::CommentSerializer, association.lazy_association.serializer.first)
233
- when :author
234
- assert_instance_of(ResourceNamespace::AuthorSerializer, association.lazy_association.serializer)
235
- when :description
236
- assert_instance_of(ResourceNamespace::DescriptionSerializer, association.lazy_association.serializer)
237
- else
238
- flunk "Unknown association: #{key}"
239
- end
240
- end
241
- end
242
- end
243
-
244
- class NestedSerializersTest < ActiveSupport::TestCase
245
- class Post < ::Model
246
- associations :comments, :author, :description
247
- end
248
- class Comment < ::Model; end
249
- class Author < ::Model; end
250
- class Description < ::Model; end
251
- class PostSerializer < ActiveModel::Serializer
252
- has_many :comments
253
- class CommentSerializer < ActiveModel::Serializer; end
254
- belongs_to :author
255
- class AuthorSerializer < ActiveModel::Serializer; end
256
- has_one :description
257
- class DescriptionSerializer < ActiveModel::Serializer; end
258
- end
259
-
260
- def setup
261
- @comment = Comment.new
262
- @author = Author.new
263
- @description = Description.new
264
- @post = Post.new(comments: [@comment],
265
- author: @author,
266
- description: @description)
267
- @post_serializer = PostSerializer.new(@post)
268
- end
269
-
270
- def test_associations_namespaced_resources
271
- @post_serializer.associations.each do |association|
272
- case association.key
273
- when :comments
274
- assert_instance_of(PostSerializer::CommentSerializer, association.lazy_association.serializer.first)
275
- when :author
276
- assert_instance_of(PostSerializer::AuthorSerializer, association.lazy_association.serializer)
277
- when :description
278
- assert_instance_of(PostSerializer::DescriptionSerializer, association.lazy_association.serializer)
279
- else
280
- flunk "Unknown association: #{key}"
281
- end
282
- end
283
- end
284
-
285
- # rubocop:disable Metrics/AbcSize
286
- def test_conditional_associations
287
- model = Class.new(::Model) do
288
- attributes :true, :false
289
- associations :something
290
- end.new(true: true, false: false)
291
-
292
- scenarios = [
293
- { options: { if: :true }, included: true },
294
- { options: { if: :false }, included: false },
295
- { options: { unless: :false }, included: true },
296
- { options: { unless: :true }, included: false },
297
- { options: { if: 'object.true' }, included: true },
298
- { options: { if: 'object.false' }, included: false },
299
- { options: { unless: 'object.false' }, included: true },
300
- { options: { unless: 'object.true' }, included: false },
301
- { options: { if: -> { object.true } }, included: true },
302
- { options: { if: -> { object.false } }, included: false },
303
- { options: { unless: -> { object.false } }, included: true },
304
- { options: { unless: -> { object.true } }, included: false },
305
- { options: { if: -> (s) { s.object.true } }, included: true },
306
- { options: { if: -> (s) { s.object.false } }, included: false },
307
- { options: { unless: -> (s) { s.object.false } }, included: true },
308
- { options: { unless: -> (s) { s.object.true } }, included: false }
309
- ]
310
-
311
- scenarios.each do |s|
312
- serializer = Class.new(ActiveModel::Serializer) do
313
- belongs_to :something, s[:options]
314
-
315
- def true
316
- true
317
- end
318
-
319
- def false
320
- false
321
- end
322
- end
323
-
324
- hash = serializable(model, serializer: serializer).serializable_hash
325
- assert_equal(s[:included], hash.key?(:something), "Error with #{s[:options]}")
326
- end
327
- end
328
-
329
- def test_illegal_conditional_associations
330
- exception = assert_raises(TypeError) do
331
- Class.new(ActiveModel::Serializer) do
332
- belongs_to :x, if: nil
333
- end
334
- end
335
-
336
- assert_match(/:if should be a Symbol, String or Proc/, exception.message)
337
- end
338
- end
339
-
340
- class InheritedSerializerTest < ActiveSupport::TestCase
341
- class PostSerializer < ActiveModel::Serializer
342
- belongs_to :author
343
- has_many :comments
344
- belongs_to :blog
345
- end
346
-
347
- class InheritedPostSerializer < PostSerializer
348
- belongs_to :author, polymorphic: true
349
- has_many :comments, key: :reviews
350
- end
351
-
352
- class AuthorSerializer < ActiveModel::Serializer
353
- has_many :posts
354
- has_many :roles
355
- has_one :bio
356
- end
357
-
358
- class InheritedAuthorSerializer < AuthorSerializer
359
- has_many :roles, polymorphic: true
360
- has_one :bio, polymorphic: true
361
- end
362
-
363
- def setup
364
- @author = Author.new(name: 'Steve K.')
365
- @post = Post.new(title: 'New Post', body: 'Body')
366
- @post_serializer = PostSerializer.new(@post)
367
- @author_serializer = AuthorSerializer.new(@author)
368
- @inherited_post_serializer = InheritedPostSerializer.new(@post)
369
- @inherited_author_serializer = InheritedAuthorSerializer.new(@author)
370
- @author_associations = @author_serializer.associations.to_a.sort_by(&:name)
371
- @inherited_author_associations = @inherited_author_serializer.associations.to_a.sort_by(&:name)
372
- @post_associations = @post_serializer.associations.to_a
373
- @inherited_post_associations = @inherited_post_serializer.associations.to_a
374
- end
375
-
376
- test 'an author serializer must have [posts,roles,bio] associations' do
377
- expected = [:posts, :roles, :bio].sort
378
- result = @author_serializer.associations.map(&:name).sort
379
- assert_equal(result, expected)
380
- end
381
-
382
- test 'a post serializer must have [author,comments,blog] associations' do
383
- expected = [:author, :comments, :blog].sort
384
- result = @post_serializer.associations.map(&:name).sort
385
- assert_equal(result, expected)
386
- end
387
-
388
- test 'a serializer inheriting from another serializer can redefine has_many and has_one associations' do
389
- expected = [:roles, :bio].sort
390
- result = (@inherited_author_associations.map(&:reflection) - @author_associations.map(&:reflection)).map(&:name)
391
- assert_equal(result, expected)
392
- assert_equal [true, false, true], @inherited_author_associations.map(&:polymorphic?)
393
- assert_equal [false, false, false], @author_associations.map(&:polymorphic?)
394
- end
395
-
396
- test 'a serializer inheriting from another serializer can redefine belongs_to associations' do
397
- assert_equal [:author, :comments, :blog], @post_associations.map(&:name)
398
- assert_equal [:author, :comments, :blog, :comments], @inherited_post_associations.map(&:name)
399
-
400
- refute @post_associations.detect { |assoc| assoc.name == :author }.polymorphic?
401
- assert @inherited_post_associations.detect { |assoc| assoc.name == :author }.polymorphic?
402
-
403
- refute @post_associations.detect { |assoc| assoc.name == :comments }.key?
404
- original_comment_assoc, new_comments_assoc = @inherited_post_associations.select { |assoc| assoc.name == :comments }
405
- refute original_comment_assoc.key?
406
- assert_equal :reviews, new_comments_assoc.key
407
-
408
- original_blog = @post_associations.detect { |assoc| assoc.name == :blog }
409
- inherited_blog = @inherited_post_associations.detect { |assoc| assoc.name == :blog }
410
- original_parent_serializer = original_blog.lazy_association.association_options.delete(:parent_serializer)
411
- inherited_parent_serializer = inherited_blog.lazy_association.association_options.delete(:parent_serializer)
412
- assert_equal PostSerializer, original_parent_serializer.class
413
- assert_equal InheritedPostSerializer, inherited_parent_serializer.class
414
- end
415
-
416
- test 'a serializer inheriting from another serializer can have an additional association with the same name but with different key' do
417
- expected = [:author, :comments, :blog, :reviews].sort
418
- result = @inherited_post_serializer.associations.map(&:key).sort
419
- assert_equal(result, expected)
420
- end
421
- end
422
- end
423
- end
424
- end
@@ -1,153 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class Serializer
5
- class AttributeTest < ActiveSupport::TestCase
6
- def setup
7
- @blog = Blog.new(id: 1, name: 'AMS Hints', type: 'stuff')
8
- @blog_serializer = AlternateBlogSerializer.new(@blog)
9
- end
10
-
11
- def test_attributes_definition
12
- assert_equal([:id, :title],
13
- @blog_serializer.class._attributes)
14
- end
15
-
16
- def test_json_serializable_hash
17
- adapter = ActiveModelSerializers::Adapter::Json.new(@blog_serializer)
18
- assert_equal({ blog: { id: 1, title: 'AMS Hints' } }, adapter.serializable_hash)
19
- end
20
-
21
- def test_attribute_inheritance_with_key
22
- inherited_klass = Class.new(AlternateBlogSerializer)
23
- blog_serializer = inherited_klass.new(@blog)
24
- adapter = ActiveModelSerializers::Adapter::Attributes.new(blog_serializer)
25
- assert_equal({ id: 1, title: 'AMS Hints' }, adapter.serializable_hash)
26
- end
27
-
28
- def test_multiple_calls_with_the_same_attribute
29
- serializer_class = Class.new(ActiveModel::Serializer) do
30
- attribute :title
31
- attribute :title
32
- end
33
-
34
- assert_equal([:title], serializer_class._attributes)
35
- end
36
-
37
- def test_id_attribute_override
38
- serializer = Class.new(ActiveModel::Serializer) do
39
- attribute :name, key: :id
40
- end
41
-
42
- adapter = ActiveModelSerializers::Adapter::Json.new(serializer.new(@blog))
43
- assert_equal({ blog: { id: 'AMS Hints' } }, adapter.serializable_hash)
44
- end
45
-
46
- def test_object_attribute_override
47
- serializer = Class.new(ActiveModel::Serializer) do
48
- attribute :name, key: :object
49
- end
50
-
51
- adapter = ActiveModelSerializers::Adapter::Json.new(serializer.new(@blog))
52
- assert_equal({ blog: { object: 'AMS Hints' } }, adapter.serializable_hash)
53
- end
54
-
55
- def test_type_attribute
56
- attribute_serializer = Class.new(ActiveModel::Serializer) do
57
- attribute :id, key: :type
58
- end
59
- attributes_serializer = Class.new(ActiveModel::Serializer) do
60
- attributes :type
61
- end
62
-
63
- adapter = ActiveModelSerializers::Adapter::Json.new(attribute_serializer.new(@blog))
64
- assert_equal({ blog: { type: 1 } }, adapter.serializable_hash)
65
-
66
- adapter = ActiveModelSerializers::Adapter::Json.new(attributes_serializer.new(@blog))
67
- assert_equal({ blog: { type: 'stuff' } }, adapter.serializable_hash)
68
- end
69
-
70
- def test_id_attribute_override_before
71
- serializer = Class.new(ActiveModel::Serializer) do
72
- def id
73
- 'custom'
74
- end
75
-
76
- attribute :id
77
- end
78
-
79
- hash = ActiveModelSerializers::SerializableResource.new(@blog, adapter: :json, serializer: serializer).serializable_hash
80
-
81
- assert_equal('custom', hash[:blog][:id])
82
- end
83
-
84
- class PostWithVirtualAttribute < ::Model; attributes :first_name, :last_name end
85
- class PostWithVirtualAttributeSerializer < ActiveModel::Serializer
86
- attribute :name do
87
- "#{object.first_name} #{object.last_name}"
88
- end
89
- end
90
-
91
- def test_virtual_attribute_block
92
- post = PostWithVirtualAttribute.new(first_name: 'Lucas', last_name: 'Hosseini')
93
- hash = serializable(post).serializable_hash
94
- expected = { name: 'Lucas Hosseini' }
95
-
96
- assert_equal(expected, hash)
97
- end
98
-
99
- # rubocop:disable Metrics/AbcSize
100
- def test_conditional_associations
101
- model = Class.new(::Model) do
102
- attributes :true, :false, :attribute
103
- end.new(true: true, false: false)
104
-
105
- scenarios = [
106
- { options: { if: :true }, included: true },
107
- { options: { if: :false }, included: false },
108
- { options: { unless: :false }, included: true },
109
- { options: { unless: :true }, included: false },
110
- { options: { if: 'object.true' }, included: true },
111
- { options: { if: 'object.false' }, included: false },
112
- { options: { unless: 'object.false' }, included: true },
113
- { options: { unless: 'object.true' }, included: false },
114
- { options: { if: -> { object.true } }, included: true },
115
- { options: { if: -> { object.false } }, included: false },
116
- { options: { unless: -> { object.false } }, included: true },
117
- { options: { unless: -> { object.true } }, included: false },
118
- { options: { if: -> (s) { s.object.true } }, included: true },
119
- { options: { if: -> (s) { s.object.false } }, included: false },
120
- { options: { unless: -> (s) { s.object.false } }, included: true },
121
- { options: { unless: -> (s) { s.object.true } }, included: false }
122
- ]
123
-
124
- scenarios.each do |s|
125
- serializer = Class.new(ActiveModel::Serializer) do
126
- attribute :attribute, s[:options]
127
-
128
- def true
129
- true
130
- end
131
-
132
- def false
133
- false
134
- end
135
- end
136
-
137
- hash = serializable(model, serializer: serializer).serializable_hash
138
- assert_equal(s[:included], hash.key?(:attribute), "Error with #{s[:options]}")
139
- end
140
- end
141
-
142
- def test_illegal_conditional_attributes
143
- exception = assert_raises(TypeError) do
144
- Class.new(ActiveModel::Serializer) do
145
- attribute :x, if: nil
146
- end
147
- end
148
-
149
- assert_match(/:if should be a Symbol, String or Proc/, exception.message)
150
- end
151
- end
152
- end
153
- end
@@ -1,52 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class Serializer
5
- class AttributesTest < ActiveSupport::TestCase
6
- def setup
7
- @profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
8
- @profile_serializer = ProfileSerializer.new(@profile)
9
- @comment = Comment.new(id: 1, body: 'ZOMG!!', date: '2015')
10
- @serializer_klass = Class.new(CommentSerializer)
11
- @serializer_klass_with_new_attributes = Class.new(CommentSerializer) do
12
- attributes :date, :likes
13
- end
14
- end
15
-
16
- def test_attributes_definition
17
- assert_equal([:name, :description],
18
- @profile_serializer.class._attributes)
19
- end
20
-
21
- def test_attributes_inheritance_definition
22
- assert_equal([:id, :body], @serializer_klass._attributes)
23
- end
24
-
25
- def test_attributes_inheritance
26
- serializer = @serializer_klass.new(@comment)
27
- assert_equal({ id: 1, body: 'ZOMG!!' },
28
- serializer.attributes)
29
- end
30
-
31
- def test_attribute_inheritance_with_new_attribute_definition
32
- assert_equal([:id, :body, :date, :likes], @serializer_klass_with_new_attributes._attributes)
33
- assert_equal([:id, :body], CommentSerializer._attributes)
34
- end
35
-
36
- def test_attribute_inheritance_with_new_attribute
37
- serializer = @serializer_klass_with_new_attributes.new(@comment)
38
- assert_equal({ id: 1, body: 'ZOMG!!', date: '2015', likes: nil },
39
- serializer.attributes)
40
- end
41
-
42
- def test_multiple_calls_with_the_same_attribute
43
- serializer_class = Class.new(ActiveModel::Serializer) do
44
- attributes :id, :title
45
- attributes :id, :title, :title, :body
46
- end
47
-
48
- assert_equal([:id, :title, :body], serializer_class._attributes)
49
- end
50
- end
51
- end
52
- end