active_model_serializers 0.10.0 → 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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +239 -2
- data/README.md +171 -34
- data/lib/action_controller/serialization.rb +23 -3
- data/lib/active_model/serializable_resource.rb +2 -0
- data/lib/active_model/serializer/adapter/attributes.rb +2 -0
- data/lib/active_model/serializer/adapter/base.rb +4 -0
- data/lib/active_model/serializer/adapter/json.rb +2 -0
- data/lib/active_model/serializer/adapter/json_api.rb +2 -0
- data/lib/active_model/serializer/adapter/null.rb +2 -0
- data/lib/active_model/serializer/adapter.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +10 -5
- data/lib/active_model/serializer/association.rb +64 -10
- data/lib/active_model/serializer/attribute.rb +2 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +6 -3
- data/lib/active_model/serializer/collection_serializer.rb +48 -13
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +89 -117
- data/lib/active_model/serializer/error_serializer.rb +13 -7
- data/lib/active_model/serializer/errors_serializer.rb +27 -20
- data/lib/active_model/serializer/field.rb +2 -0
- data/lib/active_model/serializer/fieldset.rb +3 -1
- data/lib/active_model/serializer/has_many_reflection.rb +5 -3
- data/lib/active_model/serializer/has_one_reflection.rb +3 -4
- data/lib/active_model/serializer/lazy_association.rb +99 -0
- data/lib/active_model/serializer/link.rb +23 -0
- data/lib/active_model/serializer/lint.rb +136 -130
- data/lib/active_model/serializer/null.rb +2 -0
- data/lib/active_model/serializer/reflection.rb +130 -65
- data/lib/active_model/serializer/version.rb +3 -1
- data/lib/active_model/serializer.rb +321 -86
- data/lib/active_model_serializers/adapter/attributes.rb +17 -57
- data/lib/active_model_serializers/adapter/base.rb +41 -39
- data/lib/active_model_serializers/adapter/json.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
- data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +3 -1
- data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +52 -20
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +77 -23
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +41 -10
- data/lib/active_model_serializers/adapter/json_api.rb +84 -65
- data/lib/active_model_serializers/adapter/null.rb +2 -0
- data/lib/active_model_serializers/adapter.rb +9 -1
- data/lib/active_model_serializers/callbacks.rb +2 -0
- data/lib/active_model_serializers/deprecate.rb +3 -2
- data/lib/active_model_serializers/deserialization.rb +4 -0
- data/lib/active_model_serializers/json_pointer.rb +2 -0
- data/lib/active_model_serializers/logging.rb +2 -0
- data/lib/active_model_serializers/lookup_chain.rb +82 -0
- data/lib/active_model_serializers/model/caching.rb +26 -0
- data/lib/active_model_serializers/model.rb +111 -28
- data/lib/active_model_serializers/railtie.rb +7 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +46 -31
- data/lib/active_model_serializers/serializable_resource.rb +10 -7
- data/lib/active_model_serializers/serialization_context.rb +12 -3
- data/lib/active_model_serializers/test/schema.rb +4 -2
- data/lib/active_model_serializers/test/serializer.rb +2 -0
- data/lib/active_model_serializers/test.rb +2 -0
- data/lib/active_model_serializers.rb +35 -10
- data/lib/generators/rails/resource_override.rb +3 -1
- data/lib/generators/rails/serializer_generator.rb +6 -4
- data/lib/grape/active_model_serializers.rb +9 -5
- data/lib/grape/formatters/active_model_serializers.rb +21 -2
- data/lib/grape/helpers/active_model_serializers.rb +3 -0
- data/lib/tasks/rubocop.rake +55 -0
- metadata +104 -296
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -104
- data/.rubocop_todo.yml +0 -167
- data/.simplecov +0 -110
- data/.travis.yml +0 -43
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -53
- data/Rakefile +0 -103
- data/active_model_serializers.gemspec +0 -66
- data/appveyor.yml +0 -24
- data/bin/bench +0 -171
- data/bin/bench_regression +0 -316
- data/bin/serve_benchmark +0 -39
- data/docs/ARCHITECTURE.md +0 -126
- data/docs/README.md +0 -40
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -245
- data/docs/general/caching.md +0 -52
- data/docs/general/configuration_options.md +0 -100
- data/docs/general/deserialization.md +0 -100
- data/docs/general/getting_started.md +0 -133
- data/docs/general/instrumentation.md +0 -40
- data/docs/general/key_transforms.md +0 -40
- data/docs/general/logging.md +0 -14
- data/docs/general/rendering.md +0 -255
- data/docs/general/serializers.md +0 -372
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +0 -139
- data/docs/howto/add_root_key.md +0 -51
- data/docs/howto/outside_controller_use.md +0 -58
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -32
- data/docs/howto/test.md +0 -152
- data/docs/integrations/ember-and-json-api.md +0 -112
- data/docs/integrations/grape.md +0 -19
- data/docs/jsonapi/errors.md +0 -56
- data/docs/jsonapi/schema/schema.json +0 -366
- data/docs/jsonapi/schema.md +0 -151
- data/docs/rfcs/0000-namespace.md +0 -106
- data/docs/rfcs/template.md +0 -15
- data/lib/active_model/serializer/associations.rb +0 -100
- data/lib/active_model/serializer/attributes.rb +0 -82
- data/lib/active_model/serializer/collection_reflection.rb +0 -7
- data/lib/active_model/serializer/configuration.rb +0 -35
- data/lib/active_model/serializer/include_tree.rb +0 -111
- data/lib/active_model/serializer/links.rb +0 -35
- data/lib/active_model/serializer/meta.rb +0 -29
- data/lib/active_model/serializer/singular_reflection.rb +0 -7
- data/lib/active_model/serializer/type.rb +0 -25
- data/lib/active_model_serializers/key_transform.rb +0 -70
- data/test/action_controller/adapter_selector_test.rb +0 -53
- data/test/action_controller/explicit_serializer_test.rb +0 -134
- data/test/action_controller/json/include_test.rb +0 -167
- data/test/action_controller/json_api/deserialization_test.rb +0 -112
- data/test/action_controller/json_api/errors_test.rb +0 -41
- data/test/action_controller/json_api/linked_test.rb +0 -197
- data/test/action_controller/json_api/pagination_test.rb +0 -116
- data/test/action_controller/json_api/transform_test.rb +0 -181
- data/test/action_controller/serialization_scope_name_test.rb +0 -229
- data/test/action_controller/serialization_test.rb +0 -469
- data/test/active_model_serializers/adapter_for_test.rb +0 -208
- data/test/active_model_serializers/json_pointer_test.rb +0 -20
- data/test/active_model_serializers/key_transform_test.rb +0 -263
- data/test/active_model_serializers/logging_test.rb +0 -77
- data/test/active_model_serializers/model_test.rb +0 -9
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -63
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -58
- data/test/active_model_serializers/test/schema_test.rb +0 -130
- data/test/active_model_serializers/test/serializer_test.rb +0 -62
- data/test/active_record_test.rb +0 -9
- data/test/adapter/deprecation_test.rb +0 -100
- data/test/adapter/json/belongs_to_test.rb +0 -45
- data/test/adapter/json/collection_test.rb +0 -90
- data/test/adapter/json/has_many_test.rb +0 -45
- data/test/adapter/json/transform_test.rb +0 -93
- data/test/adapter/json_api/belongs_to_test.rb +0 -155
- data/test/adapter/json_api/collection_test.rb +0 -95
- data/test/adapter/json_api/errors_test.rb +0 -78
- data/test/adapter/json_api/fields_test.rb +0 -87
- data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
- data/test/adapter/json_api/has_many_test.rb +0 -144
- data/test/adapter/json_api/has_one_test.rb +0 -80
- data/test/adapter/json_api/json_api_test.rb +0 -35
- data/test/adapter/json_api/linked_test.rb +0 -392
- data/test/adapter/json_api/links_test.rb +0 -93
- data/test/adapter/json_api/pagination_links_test.rb +0 -166
- data/test/adapter/json_api/parse_test.rb +0 -137
- data/test/adapter/json_api/relationship_test.rb +0 -161
- data/test/adapter/json_api/relationships_test.rb +0 -199
- data/test/adapter/json_api/resource_identifier_test.rb +0 -85
- data/test/adapter/json_api/resource_meta_test.rb +0 -100
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
- data/test/adapter/json_api/transform_test.rb +0 -502
- data/test/adapter/json_api/type_test.rb +0 -61
- data/test/adapter/json_test.rb +0 -45
- data/test/adapter/null_test.rb +0 -23
- data/test/adapter/polymorphic_test.rb +0 -171
- data/test/adapter_test.rb +0 -67
- data/test/array_serializer_test.rb +0 -22
- data/test/benchmark/app.rb +0 -65
- data/test/benchmark/benchmarking_support.rb +0 -67
- data/test/benchmark/bm_caching.rb +0 -119
- data/test/benchmark/bm_transform.rb +0 -34
- data/test/benchmark/config.ru +0 -3
- data/test/benchmark/controllers.rb +0 -84
- data/test/benchmark/fixtures.rb +0 -219
- data/test/cache_test.rb +0 -485
- data/test/collection_serializer_test.rb +0 -110
- data/test/fixtures/active_record.rb +0 -78
- data/test/fixtures/poro.rb +0 -282
- data/test/generators/scaffold_controller_generator_test.rb +0 -24
- data/test/generators/serializer_generator_test.rb +0 -57
- data/test/grape_test.rb +0 -82
- data/test/include_tree/from_include_args_test.rb +0 -26
- data/test/include_tree/from_string_test.rb +0 -94
- data/test/include_tree/include_args_to_hash_test.rb +0 -64
- data/test/lint_test.rb +0 -49
- data/test/logger_test.rb +0 -18
- data/test/poro_test.rb +0 -9
- data/test/serializable_resource_test.rb +0 -83
- data/test/serializers/association_macros_test.rb +0 -36
- data/test/serializers/associations_test.rb +0 -295
- data/test/serializers/attribute_test.rb +0 -151
- data/test/serializers/attributes_test.rb +0 -52
- data/test/serializers/caching_configuration_test_isolated.rb +0 -170
- data/test/serializers/configuration_test.rb +0 -32
- data/test/serializers/fieldset_test.rb +0 -14
- data/test/serializers/meta_test.rb +0 -196
- data/test/serializers/options_test.rb +0 -21
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
- data/test/serializers/root_test.rb +0 -21
- data/test/serializers/serialization_test.rb +0 -55
- data/test/serializers/serializer_for_test.rb +0 -134
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/isolated_unit.rb +0 -79
- data/test/support/rails5_shims.rb +0 -47
- data/test/support/rails_app.rb +0 -45
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
- data/test/support/schemas/custom/show.json +0 -7
- data/test/support/schemas/hyper_schema.json +0 -93
- data/test/support/schemas/render_using_json_api.json +0 -43
- data/test/support/schemas/simple_json_pointers.json +0 -10
- data/test/support/serialization_testing.rb +0 -53
- 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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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, @
|
|
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,
|
|
255
|
-
serializer.associations(
|
|
256
|
-
|
|
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,
|
|
269
|
+
def process_relationship(serializer, include_slice)
|
|
261
270
|
if serializer.respond_to?(:each)
|
|
262
|
-
serializer.each { |s| process_relationship(s,
|
|
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,
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
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
|
-
|
|
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
|
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
|
##
|
|
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
|
|
@@ -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,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
module ActiveModelSerializers
|
|
3
|
+
class Model
|
|
4
|
+
module Caching
|
|
5
|
+
extend ActiveSupport::Concern
|
|
6
|
+
|
|
7
|
+
included do
|
|
8
|
+
attr_writer :updated_at
|
|
9
|
+
attributes :id
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Defaults to the downcased model name and updated_at
|
|
13
|
+
def cache_key
|
|
14
|
+
ActiveSupport::Cache.expand_cache_key([
|
|
15
|
+
self.class.model_name.name.downcase,
|
|
16
|
+
"#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}"
|
|
17
|
+
].compact)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Defaults to the time the serializer file was modified.
|
|
21
|
+
def updated_at
|
|
22
|
+
defined?(@updated_at) ? @updated_at : File.mtime(__FILE__)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -1,49 +1,132 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
#
|
|
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
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
#
|
|
18
|
-
def
|
|
19
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
#
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
#
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
46
|
-
|
|
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', :
|
|
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)
|