active_model_serializers 0.10.0 → 0.10.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (215) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +227 -2
  3. data/README.md +171 -33
  4. data/lib/action_controller/serialization.rb +23 -3
  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 +4 -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 +10 -5
  13. data/lib/active_model/serializer/association.rb +64 -10
  14. data/lib/active_model/serializer/attribute.rb +2 -0
  15. data/lib/active_model/serializer/belongs_to_reflection.rb +6 -3
  16. data/lib/active_model/serializer/collection_serializer.rb +39 -13
  17. data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +89 -117
  18. data/lib/active_model/serializer/error_serializer.rb +13 -7
  19. data/lib/active_model/serializer/errors_serializer.rb +27 -20
  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 +5 -3
  23. data/lib/active_model/serializer/has_one_reflection.rb +3 -4
  24. data/lib/active_model/serializer/lazy_association.rb +99 -0
  25. data/lib/active_model/serializer/link.rb +23 -0
  26. data/lib/active_model/serializer/lint.rb +136 -130
  27. data/lib/active_model/serializer/null.rb +2 -0
  28. data/lib/active_model/serializer/reflection.rb +130 -65
  29. data/lib/active_model/serializer/version.rb +3 -1
  30. data/lib/active_model/serializer.rb +319 -86
  31. data/lib/active_model_serializers/adapter/attributes.rb +17 -57
  32. data/lib/active_model_serializers/adapter/base.rb +41 -39
  33. data/lib/active_model_serializers/adapter/json.rb +2 -0
  34. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
  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 +3 -1
  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 +49 -21
  40. data/lib/active_model_serializers/adapter/json_api/relationship.rb +77 -23
  41. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +41 -10
  42. data/lib/active_model_serializers/adapter/json_api.rb +84 -65
  43. data/lib/active_model_serializers/adapter/null.rb +2 -0
  44. data/lib/active_model_serializers/adapter.rb +9 -1
  45. data/lib/active_model_serializers/callbacks.rb +2 -0
  46. data/lib/active_model_serializers/deprecate.rb +3 -2
  47. data/lib/active_model_serializers/deserialization.rb +4 -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 +82 -0
  51. data/lib/active_model_serializers/model/caching.rb +25 -0
  52. data/lib/active_model_serializers/model.rb +111 -28
  53. data/lib/active_model_serializers/railtie.rb +7 -1
  54. data/lib/active_model_serializers/register_jsonapi_renderer.rb +46 -31
  55. data/lib/active_model_serializers/serializable_resource.rb +10 -7
  56. data/lib/active_model_serializers/serialization_context.rb +12 -3
  57. data/lib/active_model_serializers/test/schema.rb +4 -2
  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 +35 -10
  61. data/lib/generators/rails/resource_override.rb +3 -1
  62. data/lib/generators/rails/serializer_generator.rb +6 -4
  63. data/lib/grape/active_model_serializers.rb +9 -5
  64. data/lib/grape/formatters/active_model_serializers.rb +21 -2
  65. data/lib/grape/helpers/active_model_serializers.rb +3 -0
  66. data/lib/tasks/rubocop.rake +55 -0
  67. metadata +110 -276
  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 -104
  72. data/.rubocop_todo.yml +0 -167
  73. data/.simplecov +0 -110
  74. data/.travis.yml +0 -43
  75. data/CONTRIBUTING.md +0 -105
  76. data/Gemfile +0 -53
  77. data/Rakefile +0 -103
  78. data/active_model_serializers.gemspec +0 -66
  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 -126
  84. data/docs/README.md +0 -40
  85. data/docs/STYLE.md +0 -58
  86. data/docs/general/adapters.md +0 -245
  87. data/docs/general/caching.md +0 -52
  88. data/docs/general/configuration_options.md +0 -100
  89. data/docs/general/deserialization.md +0 -100
  90. data/docs/general/getting_started.md +0 -133
  91. data/docs/general/instrumentation.md +0 -40
  92. data/docs/general/key_transforms.md +0 -40
  93. data/docs/general/logging.md +0 -14
  94. data/docs/general/rendering.md +0 -255
  95. data/docs/general/serializers.md +0 -372
  96. data/docs/how-open-source-maintained.jpg +0 -0
  97. data/docs/howto/add_pagination_links.md +0 -139
  98. data/docs/howto/add_root_key.md +0 -51
  99. data/docs/howto/outside_controller_use.md +0 -58
  100. data/docs/howto/passing_arbitrary_options.md +0 -27
  101. data/docs/howto/serialize_poro.md +0 -32
  102. data/docs/howto/test.md +0 -152
  103. data/docs/integrations/ember-and-json-api.md +0 -112
  104. data/docs/integrations/grape.md +0 -19
  105. data/docs/jsonapi/errors.md +0 -56
  106. data/docs/jsonapi/schema/schema.json +0 -366
  107. data/docs/jsonapi/schema.md +0 -151
  108. data/docs/rfcs/0000-namespace.md +0 -106
  109. data/docs/rfcs/template.md +0 -15
  110. data/lib/active_model/serializer/associations.rb +0 -100
  111. data/lib/active_model/serializer/attributes.rb +0 -82
  112. data/lib/active_model/serializer/collection_reflection.rb +0 -7
  113. data/lib/active_model/serializer/configuration.rb +0 -35
  114. data/lib/active_model/serializer/include_tree.rb +0 -111
  115. data/lib/active_model/serializer/links.rb +0 -35
  116. data/lib/active_model/serializer/meta.rb +0 -29
  117. data/lib/active_model/serializer/singular_reflection.rb +0 -7
  118. data/lib/active_model/serializer/type.rb +0 -25
  119. data/lib/active_model_serializers/key_transform.rb +0 -70
  120. data/test/action_controller/adapter_selector_test.rb +0 -53
  121. data/test/action_controller/explicit_serializer_test.rb +0 -134
  122. data/test/action_controller/json/include_test.rb +0 -167
  123. data/test/action_controller/json_api/deserialization_test.rb +0 -112
  124. data/test/action_controller/json_api/errors_test.rb +0 -41
  125. data/test/action_controller/json_api/linked_test.rb +0 -197
  126. data/test/action_controller/json_api/pagination_test.rb +0 -116
  127. data/test/action_controller/json_api/transform_test.rb +0 -181
  128. data/test/action_controller/serialization_scope_name_test.rb +0 -229
  129. data/test/action_controller/serialization_test.rb +0 -469
  130. data/test/active_model_serializers/adapter_for_test.rb +0 -208
  131. data/test/active_model_serializers/json_pointer_test.rb +0 -20
  132. data/test/active_model_serializers/key_transform_test.rb +0 -263
  133. data/test/active_model_serializers/logging_test.rb +0 -77
  134. data/test/active_model_serializers/model_test.rb +0 -9
  135. data/test/active_model_serializers/railtie_test_isolated.rb +0 -63
  136. data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -58
  137. data/test/active_model_serializers/test/schema_test.rb +0 -130
  138. data/test/active_model_serializers/test/serializer_test.rb +0 -62
  139. data/test/active_record_test.rb +0 -9
  140. data/test/adapter/deprecation_test.rb +0 -100
  141. data/test/adapter/json/belongs_to_test.rb +0 -45
  142. data/test/adapter/json/collection_test.rb +0 -90
  143. data/test/adapter/json/has_many_test.rb +0 -45
  144. data/test/adapter/json/transform_test.rb +0 -93
  145. data/test/adapter/json_api/belongs_to_test.rb +0 -155
  146. data/test/adapter/json_api/collection_test.rb +0 -95
  147. data/test/adapter/json_api/errors_test.rb +0 -78
  148. data/test/adapter/json_api/fields_test.rb +0 -87
  149. data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
  150. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
  151. data/test/adapter/json_api/has_many_test.rb +0 -144
  152. data/test/adapter/json_api/has_one_test.rb +0 -80
  153. data/test/adapter/json_api/json_api_test.rb +0 -35
  154. data/test/adapter/json_api/linked_test.rb +0 -392
  155. data/test/adapter/json_api/links_test.rb +0 -93
  156. data/test/adapter/json_api/pagination_links_test.rb +0 -166
  157. data/test/adapter/json_api/parse_test.rb +0 -137
  158. data/test/adapter/json_api/relationship_test.rb +0 -161
  159. data/test/adapter/json_api/relationships_test.rb +0 -199
  160. data/test/adapter/json_api/resource_identifier_test.rb +0 -85
  161. data/test/adapter/json_api/resource_meta_test.rb +0 -100
  162. data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
  163. data/test/adapter/json_api/transform_test.rb +0 -502
  164. data/test/adapter/json_api/type_test.rb +0 -61
  165. data/test/adapter/json_test.rb +0 -45
  166. data/test/adapter/null_test.rb +0 -23
  167. data/test/adapter/polymorphic_test.rb +0 -171
  168. data/test/adapter_test.rb +0 -67
  169. data/test/array_serializer_test.rb +0 -22
  170. data/test/benchmark/app.rb +0 -65
  171. data/test/benchmark/benchmarking_support.rb +0 -67
  172. data/test/benchmark/bm_caching.rb +0 -119
  173. data/test/benchmark/bm_transform.rb +0 -34
  174. data/test/benchmark/config.ru +0 -3
  175. data/test/benchmark/controllers.rb +0 -84
  176. data/test/benchmark/fixtures.rb +0 -219
  177. data/test/cache_test.rb +0 -485
  178. data/test/collection_serializer_test.rb +0 -110
  179. data/test/fixtures/active_record.rb +0 -78
  180. data/test/fixtures/poro.rb +0 -282
  181. data/test/generators/scaffold_controller_generator_test.rb +0 -24
  182. data/test/generators/serializer_generator_test.rb +0 -57
  183. data/test/grape_test.rb +0 -82
  184. data/test/include_tree/from_include_args_test.rb +0 -26
  185. data/test/include_tree/from_string_test.rb +0 -94
  186. data/test/include_tree/include_args_to_hash_test.rb +0 -64
  187. data/test/lint_test.rb +0 -49
  188. data/test/logger_test.rb +0 -18
  189. data/test/poro_test.rb +0 -9
  190. data/test/serializable_resource_test.rb +0 -83
  191. data/test/serializers/association_macros_test.rb +0 -36
  192. data/test/serializers/associations_test.rb +0 -295
  193. data/test/serializers/attribute_test.rb +0 -151
  194. data/test/serializers/attributes_test.rb +0 -52
  195. data/test/serializers/caching_configuration_test_isolated.rb +0 -170
  196. data/test/serializers/configuration_test.rb +0 -32
  197. data/test/serializers/fieldset_test.rb +0 -14
  198. data/test/serializers/meta_test.rb +0 -196
  199. data/test/serializers/options_test.rb +0 -21
  200. data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
  201. data/test/serializers/root_test.rb +0 -21
  202. data/test/serializers/serialization_test.rb +0 -55
  203. data/test/serializers/serializer_for_test.rb +0 -134
  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 -79
  206. data/test/support/rails5_shims.rb +0 -47
  207. data/test/support/rails_app.rb +0 -45
  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 -53
  215. data/test/test_helper.rb +0 -57
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # {http://jsonapi.org/format/ JSON API specification}
2
4
  # rubocop:disable Style/AsciiComments
3
5
  # TODO: implement!
@@ -22,25 +24,38 @@ module ActiveModelSerializers
22
24
  module Adapter
23
25
  class JsonApi < Base
24
26
  extend ActiveSupport::Autoload
25
- autoload :Jsonapi
26
- autoload :ResourceIdentifier
27
- autoload :Relationship
28
- autoload :Link
29
- autoload :PaginationLinks
30
- autoload :Meta
31
- autoload :Error
32
- autoload :Deserialization
33
-
34
- def initialize(serializer, options = {})
35
- super
36
- @include_tree = ActiveModel::Serializer::IncludeTree.from_include_args(options[:include])
37
- @fieldset = options[:fieldset] || ActiveModel::Serializer::Fieldset.new(options.delete(:fields))
27
+ eager_autoload do
28
+ autoload :Jsonapi
29
+ autoload :ResourceIdentifier
30
+ autoload :Link
31
+ autoload :PaginationLinks
32
+ autoload :Meta
33
+ autoload :Error
34
+ autoload :Deserialization
35
+ autoload :Relationship
38
36
  end
39
37
 
40
38
  def self.default_key_transform
41
39
  :dash
42
40
  end
43
41
 
42
+ def self.fragment_cache(cached_hash, non_cached_hash, root = true)
43
+ core_cached = cached_hash.first
44
+ core_non_cached = non_cached_hash.first
45
+ no_root_cache = cached_hash.delete_if { |key, _value| key == core_cached[0] }
46
+ no_root_non_cache = non_cached_hash.delete_if { |key, _value| key == core_non_cached[0] }
47
+ cached_resource = (core_cached[1]) ? core_cached[1].deep_merge(core_non_cached[1]) : core_non_cached[1]
48
+ hash = root ? { root => cached_resource } : cached_resource
49
+
50
+ hash.deep_merge no_root_non_cache.deep_merge no_root_cache
51
+ end
52
+
53
+ def initialize(serializer, options = {})
54
+ super
55
+ @include_directive = JSONAPI::IncludeDirective.new(options[:include], allow_wildcard: true)
56
+ @fieldset = options[:fieldset] || ActiveModel::Serializer::Fieldset.new(options.delete(:fields))
57
+ end
58
+
44
59
  # {http://jsonapi.org/format/#crud Requests are transactional, i.e. success or failure}
45
60
  # {http://jsonapi.org/format/#document-top-level data and errors MUST NOT coexist in the same document.}
46
61
  def serializable_hash(*)
@@ -52,6 +67,11 @@ module ActiveModelSerializers
52
67
  self.class.transform_key_casing!(document, instance_options)
53
68
  end
54
69
 
70
+ def fragment_cache(cached_hash, non_cached_hash)
71
+ root = !instance_options.include?(:include)
72
+ self.class.fragment_cache(cached_hash, non_cached_hash, root)
73
+ end
74
+
55
75
  # {http://jsonapi.org/format/#document-top-level Primary data}
56
76
  # definition:
57
77
  # ☐ toplevel_data (required)
@@ -174,18 +194,6 @@ module ActiveModelSerializers
174
194
  hash
175
195
  end
176
196
 
177
- def fragment_cache(cached_hash, non_cached_hash)
178
- root = false if instance_options.include?(:include)
179
- core_cached = cached_hash.first
180
- core_non_cached = non_cached_hash.first
181
- no_root_cache = cached_hash.delete_if { |key, _value| key == core_cached[0] }
182
- no_root_non_cache = non_cached_hash.delete_if { |key, _value| key == core_non_cached[0] }
183
- cached_resource = (core_cached[1]) ? core_cached[1].deep_merge(core_non_cached[1]) : core_non_cached[1]
184
- hash = root ? { root => cached_resource } : cached_resource
185
-
186
- hash.deep_merge no_root_non_cache.deep_merge no_root_cache
187
- end
188
-
189
197
  protected
190
198
 
191
199
  attr_reader :fieldset
@@ -231,17 +239,17 @@ module ActiveModelSerializers
231
239
  @primary = []
232
240
  @included = []
233
241
  @resource_identifiers = Set.new
234
- serializers.each { |serializer| process_resource(serializer, true) }
235
- serializers.each { |serializer| process_relationships(serializer, @include_tree) }
242
+ serializers.each { |serializer| process_resource(serializer, true, @include_directive) }
243
+ serializers.each { |serializer| process_relationships(serializer, @include_directive) }
236
244
 
237
245
  [@primary, @included]
238
246
  end
239
247
 
240
- def process_resource(serializer, primary)
248
+ def process_resource(serializer, primary, include_slice = {})
241
249
  resource_identifier = ResourceIdentifier.new(serializer, instance_options).as_json
242
250
  return false unless @resource_identifiers.add?(resource_identifier)
243
251
 
244
- resource_object = resource_object_for(serializer)
252
+ resource_object = resource_object_for(serializer, include_slice)
245
253
  if primary
246
254
  @primary << resource_object
247
255
  else
@@ -251,21 +259,22 @@ module ActiveModelSerializers
251
259
  true
252
260
  end
253
261
 
254
- def process_relationships(serializer, include_tree)
255
- serializer.associations(include_tree).each do |association|
256
- process_relationship(association.serializer, include_tree[association.key])
262
+ def process_relationships(serializer, include_slice)
263
+ serializer.associations(include_slice).each do |association|
264
+ # TODO(BF): Process relationship without evaluating lazy_association
265
+ process_relationship(association.lazy_association.serializer, include_slice[association.key])
257
266
  end
258
267
  end
259
268
 
260
- def process_relationship(serializer, include_tree)
269
+ def process_relationship(serializer, include_slice)
261
270
  if serializer.respond_to?(:each)
262
- serializer.each { |s| process_relationship(s, include_tree) }
271
+ serializer.each { |s| process_relationship(s, include_slice) }
263
272
  return
264
273
  end
265
274
  return unless serializer && serializer.object
266
- return unless process_resource(serializer, false)
275
+ return unless process_resource(serializer, false, include_slice)
267
276
 
268
- process_relationships(serializer, include_tree)
277
+ process_relationships(serializer, include_slice)
269
278
  end
270
279
 
271
280
  # {http://jsonapi.org/format/#document-resource-object-attributes Document Resource Object Attributes}
@@ -289,21 +298,9 @@ module ActiveModelSerializers
289
298
  end
290
299
 
291
300
  # {http://jsonapi.org/format/#document-resource-objects Document Resource Objects}
292
- def resource_object_for(serializer)
293
- resource_object = cache_check(serializer) do
294
- resource_object = ResourceIdentifier.new(serializer, instance_options).as_json
295
-
296
- requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
297
- attributes = attributes_for(serializer, requested_fields)
298
- resource_object[:attributes] = attributes if attributes.any?
299
- resource_object
300
- end
301
+ def resource_object_for(serializer, include_slice = {})
302
+ resource_object = data_for(serializer, include_slice)
301
303
 
302
- requested_associations = fieldset.fields_for(resource_object[:type]) || '*'
303
- relationships = relationships_for(serializer, requested_associations)
304
- resource_object[:relationships] = relationships if relationships.any?
305
-
306
- links = links_for(serializer)
307
304
  # toplevel_links
308
305
  # definition:
309
306
  # allOf
@@ -317,7 +314,10 @@ module ActiveModelSerializers
317
314
  # prs:
318
315
  # https://github.com/rails-api/active_model_serializers/pull/1247
319
316
  # https://github.com/rails-api/active_model_serializers/pull/1018
320
- resource_object[:links] = links if links.any?
317
+ if (links = links_for(serializer)).any?
318
+ resource_object ||= {}
319
+ resource_object[:links] = links
320
+ end
321
321
 
322
322
  # toplevel_meta
323
323
  # alias meta
@@ -327,12 +327,33 @@ module ActiveModelSerializers
327
327
  # {
328
328
  # :'git-ref' => 'abc123'
329
329
  # }
330
- meta = meta_for(serializer)
331
- resource_object[:meta] = meta unless meta.blank?
330
+ if (meta = meta_for(serializer)).present?
331
+ resource_object ||= {}
332
+ resource_object[:meta] = meta
333
+ end
332
334
 
333
335
  resource_object
334
336
  end
335
337
 
338
+ def data_for(serializer, include_slice)
339
+ data = serializer.fetch(self) do
340
+ resource_object = ResourceIdentifier.new(serializer, instance_options).as_json
341
+ break nil if resource_object.nil?
342
+
343
+ requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
344
+ attributes = attributes_for(serializer, requested_fields)
345
+ resource_object[:attributes] = attributes if attributes.any?
346
+ resource_object
347
+ end
348
+ data.tap do |resource_object|
349
+ next if resource_object.nil?
350
+ # NOTE(BF): the attributes are cached above, separately from the relationships, below.
351
+ requested_associations = fieldset.fields_for(resource_object[:type]) || '*'
352
+ relationships = relationships_for(serializer, requested_associations, include_slice)
353
+ resource_object[:relationships] = relationships if relationships.any?
354
+ end
355
+ end
356
+
336
357
  # {http://jsonapi.org/format/#document-resource-object-relationships Document Resource Object Relationship}
337
358
  # relationships
338
359
  # definition:
@@ -428,17 +449,13 @@ module ActiveModelSerializers
428
449
  # id: 'required-id',
429
450
  # meta: meta
430
451
  # }.reject! {|_,v| v.nil? }
431
- def relationships_for(serializer, requested_associations)
432
- include_tree = ActiveModel::Serializer::IncludeTree.from_include_args(requested_associations)
433
- serializer.associations(include_tree).each_with_object({}) do |association, hash|
434
- hash[association.key] = Relationship.new(
435
- serializer,
436
- association.serializer,
437
- instance_options,
438
- options: association.options,
439
- links: association.links,
440
- meta: association.meta
441
- ).as_json
452
+ def relationships_for(serializer, requested_associations, include_slice)
453
+ include_directive = JSONAPI::IncludeDirective.new(
454
+ requested_associations,
455
+ allow_wildcard: true
456
+ )
457
+ serializer.associations(include_directive, include_slice).each_with_object({}) do |association, hash|
458
+ hash[association.key] = Relationship.new(serializer, instance_options, association).as_json
442
459
  end
443
460
  end
444
461
 
@@ -467,7 +484,9 @@ module ActiveModelSerializers
467
484
  # }.reject! {|_,v| v.nil? }
468
485
  def links_for(serializer)
469
486
  serializer._links.each_with_object({}) do |(name, value), hash|
470
- hash[name] = Link.new(serializer, value).as_json
487
+ next if value.excluded?(serializer)
488
+ result = Link.new(serializer, value.block).as_json
489
+ hash[name] = result if result
471
490
  end
472
491
  end
473
492
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  class Null < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Adapter
3
5
  UnknownAdapterError = Class.new(ArgumentError)
@@ -5,11 +7,13 @@ module ActiveModelSerializers
5
7
  private_constant :ADAPTER_MAP if defined?(private_constant)
6
8
 
7
9
  class << self # All methods are class functions
10
+ # :nocov:
8
11
  def new(*args)
9
12
  fail ArgumentError, 'Adapters inherit from Adapter::Base.' \
10
13
  "Adapter.new called with args: '#{args.inspect}', from" \
11
14
  "'caller[0]'."
12
15
  end
16
+ # :nocov:
13
17
 
14
18
  def configured_adapter
15
19
  lookup(ActiveModelSerializers.config.adapter)
@@ -33,7 +37,7 @@ module ActiveModelSerializers
33
37
 
34
38
  # @return [Array<Symbol>] list of adapter names
35
39
  def adapters
36
- adapter_map.keys.sort
40
+ adapter_map.keys.sort!
37
41
  end
38
42
 
39
43
  # Adds an adapter 'klass' with 'name' to the 'adapter_map'
@@ -51,6 +55,10 @@ module ActiveModelSerializers
51
55
  self
52
56
  end
53
57
 
58
+ def registered_name(adapter_class)
59
+ ADAPTER_MAP.key adapter_class
60
+ end
61
+
54
62
  # @param adapter [String, Symbol, Class] name to fetch adapter by
55
63
  # @return [ActiveModelSerializers::Adapter] subclass of Adapter
56
64
  # @raise [UnknownAdapterError]
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Adapted from
2
4
  # https://github.com/rails/rails/blob/7f18ea14c8/activejob/lib/active_job/callbacks.rb
3
5
  require 'active_support/callbacks'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ##
2
4
  # Provides a single method +deprecate+ to be used to declare when
3
5
  # something is going away.
@@ -36,8 +38,7 @@ module ActiveModelSerializers
36
38
  target = is_a?(Module) ? "#{self}." : "#{self.class}#"
37
39
  msg = ["NOTE: #{target}#{name} is deprecated",
38
40
  replacement == :none ? ' with no replacement' : "; use #{replacement} instead",
39
- "\n#{target}#{name} called from #{ActiveModelSerializers.location_of_caller.join(":")}"
40
- ]
41
+ "\n#{target}#{name} called from #{ActiveModelSerializers.location_of_caller.join(':')}"]
41
42
  warn "#{msg.join}."
42
43
  send old, *args, &block
43
44
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Deserialization
3
5
  module_function
@@ -6,8 +8,10 @@ module ActiveModelSerializers
6
8
  Adapter::JsonApi::Deserialization.parse(*args)
7
9
  end
8
10
 
11
+ # :nocov:
9
12
  def jsonapi_parse!(*args)
10
13
  Adapter::JsonApi::Deserialization.parse!(*args)
11
14
  end
15
+ # :nocov:
12
16
  end
13
17
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module JsonPointer
3
5
  module_function
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ##
2
4
  # ActiveModelSerializers::Logging
3
5
  #
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveModelSerializers
4
+ module LookupChain
5
+ # Standard appending of Serializer to the resource name.
6
+ #
7
+ # Example:
8
+ # Author => AuthorSerializer
9
+ BY_RESOURCE = lambda do |resource_class, _serializer_class, _namespace|
10
+ serializer_from(resource_class)
11
+ end
12
+
13
+ # Uses the namespace of the resource to find the serializer
14
+ #
15
+ # Example:
16
+ # British::Author => British::AuthorSerializer
17
+ BY_RESOURCE_NAMESPACE = lambda do |resource_class, _serializer_class, _namespace|
18
+ resource_namespace = namespace_for(resource_class)
19
+ serializer_name = serializer_from(resource_class)
20
+
21
+ "#{resource_namespace}::#{serializer_name}"
22
+ end
23
+
24
+ # Uses the controller namespace of the resource to find the serializer
25
+ #
26
+ # Example:
27
+ # Api::V3::AuthorsController => Api::V3::AuthorSerializer
28
+ BY_NAMESPACE = lambda do |resource_class, _serializer_class, namespace|
29
+ resource_name = resource_class_name(resource_class)
30
+ namespace ? "#{namespace}::#{resource_name}Serializer" : nil
31
+ end
32
+
33
+ # Allows for serializers to be defined in parent serializers
34
+ # - useful if a relationship only needs a different set of attributes
35
+ # than if it were rendered independently.
36
+ #
37
+ # Example:
38
+ # class BlogSerializer < ActiveModel::Serializer
39
+ # class AuthorSerialier < ActiveModel::Serializer
40
+ # ...
41
+ # end
42
+ #
43
+ # belongs_to :author
44
+ # ...
45
+ # end
46
+ #
47
+ # The belongs_to relationship would be rendered with
48
+ # BlogSerializer::AuthorSerialier
49
+ BY_PARENT_SERIALIZER = lambda do |resource_class, serializer_class, _namespace|
50
+ return if serializer_class == ActiveModel::Serializer
51
+
52
+ serializer_name = serializer_from(resource_class)
53
+ "#{serializer_class}::#{serializer_name}"
54
+ end
55
+
56
+ DEFAULT = [
57
+ BY_PARENT_SERIALIZER,
58
+ BY_NAMESPACE,
59
+ BY_RESOURCE_NAMESPACE,
60
+ BY_RESOURCE
61
+ ].freeze
62
+
63
+ module_function
64
+
65
+ def namespace_for(klass)
66
+ klass.name.deconstantize
67
+ end
68
+
69
+ def resource_class_name(klass)
70
+ klass.name.demodulize
71
+ end
72
+
73
+ def serializer_from_resource_name(name)
74
+ "#{name}Serializer"
75
+ end
76
+
77
+ def serializer_from(klass)
78
+ name = resource_class_name(klass)
79
+ serializer_from_resource_name(name)
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,25 @@
1
+ module ActiveModelSerializers
2
+ class Model
3
+ module Caching
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ attr_writer :updated_at
8
+ attributes :id
9
+ end
10
+
11
+ # Defaults to the downcased model name and updated_at
12
+ def cache_key
13
+ ActiveSupport::Cache.expand_cache_key([
14
+ self.class.model_name.name.downcase,
15
+ "#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}"
16
+ ].compact)
17
+ end
18
+
19
+ # Defaults to the time the serializer file was modified.
20
+ def updated_at
21
+ defined?(@updated_at) ? @updated_at : File.mtime(__FILE__)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,49 +1,132 @@
1
- # ActiveModelSerializers::Model is a convenient
2
- # serializable class to inherit from when making
3
- # serializable non-activerecord objects.
1
+ # frozen_string_literal: true
2
+
3
+ # ActiveModelSerializers::Model is a convenient superclass for making your models
4
+ # from Plain-Old Ruby Objects (PORO). It also serves as a reference implementation
5
+ # that satisfies ActiveModel::Serializer::Lint::Tests.
6
+ require 'active_support/core_ext/hash'
4
7
  module ActiveModelSerializers
5
8
  class Model
6
- include ActiveModel::Model
7
9
  include ActiveModel::Serializers::JSON
10
+ include ActiveModel::Model
8
11
 
9
- attr_reader :attributes, :errors
12
+ # Declare names of attributes to be included in +attributes+ hash.
13
+ # Is only available as a class-method since the ActiveModel::Serialization mixin in Rails
14
+ # uses an +attribute_names+ local variable, which may conflict if we were to add instance methods here.
15
+ #
16
+ # @overload attribute_names
17
+ # @return [Array<Symbol>]
18
+ class_attribute :attribute_names, instance_writer: false, instance_reader: false
19
+ # Initialize +attribute_names+ for all subclasses. The array is usually
20
+ # mutated in the +attributes+ method, but can be set directly, as well.
21
+ self.attribute_names = []
10
22
 
11
- def initialize(attributes = {})
12
- @attributes = attributes
13
- @errors = ActiveModel::Errors.new(self)
14
- super
23
+ # Easily declare instance attributes with setters and getters for each.
24
+ #
25
+ # To initialize an instance, all attributes must have setters.
26
+ # However, the hash returned by +attributes+ instance method will ALWAYS
27
+ # be the value of the initial attributes, regardless of what accessors are defined.
28
+ # The only way to change the change the attributes after initialization is
29
+ # to mutate the +attributes+ directly.
30
+ # Accessor methods do NOT mutate the attributes. (This is a bug).
31
+ #
32
+ # @note For now, the Model only supports the notion of 'attributes'.
33
+ # In the tests, there is a special Model that also supports 'associations'. This is
34
+ # important so that we can add accessors for values that should not appear in the
35
+ # attributes hash when modeling associations. It is not yet clear if it
36
+ # makes sense for a PORO to have associations outside of the tests.
37
+ #
38
+ # @overload attributes(names)
39
+ # @param names [Array<String, Symbol>]
40
+ # @param name [String, Symbol]
41
+ def self.attributes(*names)
42
+ self.attribute_names |= names.map(&:to_sym)
43
+ # Silence redefinition of methods warnings
44
+ ActiveModelSerializers.silence_warnings do
45
+ attr_accessor(*names)
46
+ end
15
47
  end
16
48
 
17
- # Defaults to the downcased model name.
18
- def id
19
- attributes.fetch(:id) { self.class.name.downcase }
49
+ # Opt-in to breaking change
50
+ def self.derive_attributes_from_names_and_fix_accessors
51
+ unless included_modules.include?(DeriveAttributesFromNamesAndFixAccessors)
52
+ prepend(DeriveAttributesFromNamesAndFixAccessors)
53
+ end
20
54
  end
21
55
 
22
- # Defaults to the downcased model name and updated_at
23
- def cache_key
24
- attributes.fetch(:cache_key) { "#{self.class.name.downcase}/#{id}-#{updated_at.strftime("%Y%m%d%H%M%S%9N")}" }
56
+ module DeriveAttributesFromNamesAndFixAccessors
57
+ def self.included(base)
58
+ # NOTE that +id+ will always be in +attributes+.
59
+ base.attributes :id
60
+ end
61
+
62
+ # Override the +attributes+ method so that the hash is derived from +attribute_names+.
63
+ #
64
+ # The fields in +attribute_names+ determines the returned hash.
65
+ # +attributes+ are returned frozen to prevent any expectations that mutation affects
66
+ # the actual values in the model.
67
+ def attributes
68
+ self.class.attribute_names.each_with_object({}) do |attribute_name, result|
69
+ result[attribute_name] = public_send(attribute_name).freeze
70
+ end.with_indifferent_access.freeze
71
+ end
25
72
  end
26
73
 
27
- # Defaults to the time the serializer file was modified.
28
- def updated_at
29
- attributes.fetch(:updated_at) { File.mtime(__FILE__) }
74
+ # Support for validation and other ActiveModel::Errors
75
+ # @return [ActiveModel::Errors]
76
+ attr_reader :errors
77
+
78
+ # (see #updated_at)
79
+ attr_writer :updated_at
80
+
81
+ # The only way to change the attributes of an instance is to directly mutate the attributes.
82
+ # @example
83
+ #
84
+ # model.attributes[:foo] = :bar
85
+ # @return [Hash]
86
+ attr_reader :attributes
87
+
88
+ # @param attributes [Hash]
89
+ def initialize(attributes = {})
90
+ attributes ||= {} # protect against nil
91
+ @attributes = attributes.symbolize_keys.with_indifferent_access
92
+ @errors = ActiveModel::Errors.new(self)
93
+ super
30
94
  end
31
95
 
32
- def read_attribute_for_serialization(key)
33
- if key == :id || key == 'id'
34
- attributes.fetch(key) { id }
35
- else
36
- attributes[key]
96
+ # Defaults to the downcased model name.
97
+ # This probably isn't a good default, since it's not a unique instance identifier,
98
+ # but that's what is currently implemented \_('-')_/.
99
+ #
100
+ # @note Though +id+ is defined, it will only show up
101
+ # in +attributes+ when it is passed in to the initializer or added to +attributes+,
102
+ # such as <tt>attributes[:id] = 5</tt>.
103
+ # @return [String, Numeric, Symbol]
104
+ def id
105
+ attributes.fetch(:id) do
106
+ defined?(@id) ? @id : self.class.model_name.name && self.class.model_name.name.downcase
37
107
  end
38
108
  end
39
109
 
40
- # The following methods are needed to be minimally implemented for ActiveModel::Errors
41
- def self.human_attribute_name(attr, _options = {})
42
- attr
110
+ # When not set, defaults to the time the file was modified.
111
+ #
112
+ # @note Though +updated_at+ and +updated_at=+ are defined, it will only show up
113
+ # in +attributes+ when it is passed in to the initializer or added to +attributes+,
114
+ # such as <tt>attributes[:updated_at] = Time.current</tt>.
115
+ # @return [String, Numeric, Time]
116
+ def updated_at
117
+ attributes.fetch(:updated_at) do
118
+ defined?(@updated_at) ? @updated_at : File.mtime(__FILE__)
119
+ end
43
120
  end
44
121
 
45
- def self.lookup_ancestors
46
- [self]
122
+ # To customize model behavior, this method must be redefined. However,
123
+ # there are other ways of setting the +cache_key+ a serializer uses.
124
+ # @return [String]
125
+ def cache_key
126
+ ActiveSupport::Cache.expand_cache_key([
127
+ self.class.model_name.name.downcase,
128
+ "#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}"
129
+ ].compact)
47
130
  end
48
131
  end
49
132
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails/railtie'
2
4
  require 'action_controller'
3
5
  require 'action_controller/railtie'
@@ -5,6 +7,8 @@ require 'action_controller/serialization'
5
7
 
6
8
  module ActiveModelSerializers
7
9
  class Railtie < Rails::Railtie
10
+ config.eager_load_namespaces << ActiveModelSerializers
11
+
8
12
  config.to_prepare do
9
13
  ActiveModel::Serializer.serializers_cache.clear
10
14
  end
@@ -23,7 +27,7 @@ module ActiveModelSerializers
23
27
  # This hook is run after the action_controller railtie has set the configuration
24
28
  # based on the *environment* configuration and before any config/initializers are run
25
29
  # and also before eager_loading (if enabled).
26
- initializer 'active_model_serializers.set_configs', :after => 'action_controller.set_configs' do
30
+ initializer 'active_model_serializers.set_configs', after: 'action_controller.set_configs' do
27
31
  ActiveModelSerializers.logger = Rails.configuration.action_controller.logger
28
32
  ActiveModelSerializers.config.perform_caching = Rails.configuration.action_controller.perform_caching
29
33
  # We want this hook to run after the config has been set, even if ActionController has already loaded.
@@ -32,11 +36,13 @@ module ActiveModelSerializers
32
36
  end
33
37
  end
34
38
 
39
+ # :nocov:
35
40
  generators do |app|
36
41
  Rails::Generators.configure!(app.config.generators)
37
42
  Rails::Generators.hidden_namespaces.uniq!
38
43
  require 'generators/rails/resource_override'
39
44
  end
45
+ # :nocov:
40
46
 
41
47
  if Rails.env.test?
42
48
  ActionController::TestCase.send(:include, ActiveModelSerializers::Test::Schema)