active_model_serializers 0.10.6 → 0.10.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +114 -1
- data/README.md +21 -24
- data/lib/action_controller/serialization.rb +11 -1
- 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 +2 -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 +2 -0
- data/lib/active_model/serializer/association.rb +2 -0
- data/lib/active_model/serializer/attribute.rb +2 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +2 -0
- data/lib/active_model/serializer/collection_serializer.rb +17 -5
- data/lib/active_model/serializer/concerns/caching.rb +7 -2
- data/lib/active_model/serializer/error_serializer.rb +2 -0
- data/lib/active_model/serializer/errors_serializer.rb +2 -0
- 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 +2 -0
- data/lib/active_model/serializer/has_one_reflection.rb +2 -0
- data/lib/active_model/serializer/lazy_association.rb +6 -2
- data/lib/active_model/serializer/link.rb +23 -0
- data/lib/active_model/serializer/lint.rb +2 -0
- data/lib/active_model/serializer/null.rb +2 -0
- data/lib/active_model/serializer/reflection.rb +10 -5
- data/lib/active_model/serializer/version.rb +3 -1
- data/lib/active_model/serializer.rb +37 -19
- data/lib/active_model_serializers/adapter/attributes.rb +23 -0
- data/lib/active_model_serializers/adapter/base.rb +2 -0
- data/lib/active_model_serializers/adapter/json.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +3 -1
- 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 +2 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +45 -20
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +18 -4
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +33 -25
- data/lib/active_model_serializers/adapter/json_api.rb +14 -9
- data/lib/active_model_serializers/adapter/null.rb +2 -0
- data/lib/active_model_serializers/adapter.rb +3 -1
- data/lib/active_model_serializers/callbacks.rb +2 -0
- data/lib/active_model_serializers/deprecate.rb +2 -0
- data/lib/active_model_serializers/deserialization.rb +2 -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 +2 -0
- data/lib/active_model_serializers/model/caching.rb +26 -0
- data/lib/active_model_serializers/model.rb +2 -0
- data/lib/active_model_serializers/railtie.rb +4 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +2 -0
- data/lib/active_model_serializers/serializable_resource.rb +4 -2
- data/lib/active_model_serializers/serialization_context.rb +2 -0
- data/lib/active_model_serializers/test/schema.rb +2 -0
- 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 +21 -11
- data/lib/generators/rails/resource_override.rb +2 -0
- data/lib/generators/rails/serializer_generator.rb +2 -0
- data/lib/grape/active_model_serializers.rb +2 -0
- data/lib/grape/formatters/active_model_serializers.rb +2 -0
- data/lib/grape/helpers/active_model_serializers.rb +2 -0
- data/lib/tasks/rubocop.rake +2 -0
- metadata +43 -298
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -105
- data/.simplecov +0 -110
- data/.travis.yml +0 -55
- data/CODE_OF_CONDUCT.md +0 -74
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -56
- data/Rakefile +0 -74
- data/active_model_serializers.gemspec +0 -63
- data/appveyor.yml +0 -30
- data/bin/bench +0 -171
- data/bin/bench_regression +0 -316
- data/bin/rubocop +0 -38
- data/bin/serve_benchmark +0 -39
- data/docs/README.md +0 -41
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -263
- data/docs/general/caching.md +0 -58
- data/docs/general/configuration_options.md +0 -169
- data/docs/general/deserialization.md +0 -100
- data/docs/general/fields.md +0 -31
- 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 -21
- data/docs/general/rendering.md +0 -293
- data/docs/general/serializers.md +0 -480
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +0 -138
- data/docs/howto/add_relationship_links.md +0 -140
- data/docs/howto/add_root_key.md +0 -55
- data/docs/howto/grape_integration.md +0 -42
- data/docs/howto/outside_controller_use.md +0 -66
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -73
- data/docs/howto/test.md +0 -154
- data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
- data/docs/integrations/ember-and-json-api.md +0 -147
- 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/test/action_controller/adapter_selector_test.rb +0 -62
- data/test/action_controller/explicit_serializer_test.rb +0 -135
- data/test/action_controller/json/include_test.rb +0 -246
- data/test/action_controller/json_api/deserialization_test.rb +0 -112
- data/test/action_controller/json_api/errors_test.rb +0 -40
- data/test/action_controller/json_api/fields_test.rb +0 -66
- data/test/action_controller/json_api/linked_test.rb +0 -202
- data/test/action_controller/json_api/pagination_test.rb +0 -116
- data/test/action_controller/json_api/transform_test.rb +0 -189
- data/test/action_controller/lookup_proc_test.rb +0 -49
- data/test/action_controller/namespace_lookup_test.rb +0 -232
- data/test/action_controller/serialization_scope_name_test.rb +0 -235
- data/test/action_controller/serialization_test.rb +0 -472
- data/test/active_model_serializers/adapter_for_test.rb +0 -208
- data/test/active_model_serializers/json_pointer_test.rb +0 -22
- data/test/active_model_serializers/logging_test.rb +0 -77
- data/test/active_model_serializers/model_test.rb +0 -142
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -68
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -161
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -71
- data/test/active_model_serializers/test/schema_test.rb +0 -131
- data/test/active_model_serializers/test/serializer_test.rb +0 -62
- data/test/active_record_test.rb +0 -9
- data/test/adapter/attributes_test.rb +0 -40
- 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 -104
- data/test/adapter/json/has_many_test.rb +0 -53
- 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 -96
- data/test/adapter/json_api/errors_test.rb +0 -76
- data/test/adapter/json_api/fields_test.rb +0 -96
- 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 -173
- data/test/adapter/json_api/has_one_test.rb +0 -80
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -183
- data/test/adapter/json_api/json_api_test.rb +0 -33
- data/test/adapter/json_api/linked_test.rb +0 -413
- data/test/adapter/json_api/links_test.rb +0 -95
- data/test/adapter/json_api/pagination_links_test.rb +0 -193
- data/test/adapter/json_api/parse_test.rb +0 -137
- data/test/adapter/json_api/relationship_test.rb +0 -397
- data/test/adapter/json_api/resource_identifier_test.rb +0 -110
- 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 -512
- data/test/adapter/json_api/type_test.rb +0 -61
- data/test/adapter/json_test.rb +0 -46
- data/test/adapter/null_test.rb +0 -22
- 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_active_record.rb +0 -81
- data/test/benchmark/bm_adapter.rb +0 -38
- data/test/benchmark/bm_caching.rb +0 -119
- data/test/benchmark/bm_lookup_chain.rb +0 -83
- data/test/benchmark/bm_transform.rb +0 -45
- data/test/benchmark/config.ru +0 -3
- data/test/benchmark/controllers.rb +0 -83
- data/test/benchmark/fixtures.rb +0 -219
- data/test/cache_test.rb +0 -651
- data/test/collection_serializer_test.rb +0 -123
- data/test/fixtures/active_record.rb +0 -113
- data/test/fixtures/poro.rb +0 -225
- data/test/generators/scaffold_controller_generator_test.rb +0 -24
- data/test/generators/serializer_generator_test.rb +0 -75
- data/test/grape_test.rb +0 -196
- data/test/lint_test.rb +0 -49
- data/test/logger_test.rb +0 -20
- data/test/poro_test.rb +0 -9
- data/test/serializable_resource_test.rb +0 -79
- data/test/serializers/association_macros_test.rb +0 -37
- data/test/serializers/associations_test.rb +0 -424
- data/test/serializers/attribute_test.rb +0 -153
- 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 -202
- data/test/serializers/options_test.rb +0 -32
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
- data/test/serializers/reflection_test.rb +0 -427
- data/test/serializers/root_test.rb +0 -21
- data/test/serializers/serialization_test.rb +0 -55
- data/test/serializers/serializer_for_test.rb +0 -136
- data/test/serializers/serializer_for_with_namespace_test.rb +0 -88
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/isolated_unit.rb +0 -82
- data/test/support/rails5_shims.rb +0 -53
- data/test/support/rails_app.rb +0 -38
- 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 -71
- data/test/test_helper.rb +0 -70
@@ -1,4 +1,5 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'jsonapi/include_directive'
|
3
4
|
require 'active_model/serializer/collection_serializer'
|
4
5
|
require 'active_model/serializer/array_serializer'
|
@@ -18,16 +19,17 @@ module ActiveModel
|
|
18
19
|
# @see #serializable_hash for more details on these valid keys.
|
19
20
|
SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include, :root].freeze
|
20
21
|
extend ActiveSupport::Autoload
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
22
|
+
eager_autoload do
|
23
|
+
autoload :Adapter
|
24
|
+
autoload :Null
|
25
|
+
autoload :Attribute
|
26
|
+
autoload :Link
|
27
|
+
autoload :Association
|
28
|
+
autoload :Reflection
|
29
|
+
autoload :BelongsToReflection
|
30
|
+
autoload :HasOneReflection
|
31
|
+
autoload :HasManyReflection
|
32
|
+
end
|
31
33
|
include ActiveSupport::Configurable
|
32
34
|
include Caching
|
33
35
|
|
@@ -70,7 +72,7 @@ module ActiveModel
|
|
70
72
|
# Used to cache serializer name => serializer class
|
71
73
|
# when looked up by Serializer.get_serializer_for.
|
72
74
|
def self.serializers_cache
|
73
|
-
@serializers_cache ||=
|
75
|
+
@serializers_cache ||= Concurrent::Map.new
|
74
76
|
end
|
75
77
|
|
76
78
|
# @api private
|
@@ -91,7 +93,7 @@ module ActiveModel
|
|
91
93
|
if serializer_class
|
92
94
|
serializer_class
|
93
95
|
elsif klass.superclass
|
94
|
-
get_serializer_for(klass.superclass)
|
96
|
+
get_serializer_for(klass.superclass, namespace)
|
95
97
|
else
|
96
98
|
nil # No serializer found
|
97
99
|
end
|
@@ -142,7 +144,10 @@ module ActiveModel
|
|
142
144
|
# Make JSON API top-level jsonapi member opt-in
|
143
145
|
# ref: http://jsonapi.org/format/#document-top-level
|
144
146
|
config.jsonapi_include_toplevel_object = false
|
147
|
+
config.jsonapi_use_foreign_key_on_belongs_to_relationship = false
|
145
148
|
config.include_data_default = true
|
149
|
+
# Raise ActiveModel::Serializer::CollectionSerializer::CannotInferRootKeyError when cannot infer root key from collection type
|
150
|
+
config.raise_cannot_infer_root_key_error = true
|
146
151
|
|
147
152
|
# For configuring how serializers are found.
|
148
153
|
# This should be an array of procs.
|
@@ -274,9 +279,14 @@ module ActiveModel
|
|
274
279
|
# link(:self) { "http://example.com/resource/#{object.id}" }
|
275
280
|
# @example
|
276
281
|
# link :resource, "http://example.com/resource"
|
282
|
+
# @example
|
283
|
+
# link(:callback, if: :internal?), { "http://example.com/callback" }
|
277
284
|
#
|
278
|
-
def self.link(name,
|
279
|
-
|
285
|
+
def self.link(name, *args, &block)
|
286
|
+
options = args.extract_options!
|
287
|
+
# For compatibility with the use case of passing link directly as string argument
|
288
|
+
# without block, we are creating a wrapping block
|
289
|
+
_links[name] = Link.new(name, options, block || ->(_serializer) { args.first })
|
280
290
|
end
|
281
291
|
|
282
292
|
# Set the JSON API meta attribute of a serializer.
|
@@ -337,10 +347,10 @@ module ActiveModel
|
|
337
347
|
# @return [Enumerator<Association>]
|
338
348
|
def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
|
339
349
|
include_slice ||= include_directive
|
340
|
-
return Enumerator.new unless object
|
350
|
+
return Enumerator.new {} unless object
|
341
351
|
|
342
352
|
Enumerator.new do |y|
|
343
|
-
self.class._reflections.each do |key, reflection|
|
353
|
+
(self.instance_reflections ||= self.class._reflections.deep_dup).each do |key, reflection|
|
344
354
|
next if reflection.excluded?(self)
|
345
355
|
next unless include_directive.key?(key)
|
346
356
|
|
@@ -356,6 +366,9 @@ module ActiveModel
|
|
356
366
|
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
|
357
367
|
adapter_options ||= {}
|
358
368
|
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
|
369
|
+
if (fieldset = adapter_options[:fieldset])
|
370
|
+
options[:fields] = fieldset.fields_for(json_key)
|
371
|
+
end
|
359
372
|
resource = attributes_hash(adapter_options, options, adapter_instance)
|
360
373
|
relationships = associations_hash(adapter_options, options, adapter_instance)
|
361
374
|
resource.merge(relationships)
|
@@ -370,7 +383,12 @@ module ActiveModel
|
|
370
383
|
|
371
384
|
# Used by adapter as resource root.
|
372
385
|
def json_key
|
373
|
-
root || _type ||
|
386
|
+
root || _type ||
|
387
|
+
begin
|
388
|
+
object.class.model_name.to_s.underscore
|
389
|
+
rescue ArgumentError
|
390
|
+
'anonymous_object'
|
391
|
+
end
|
374
392
|
end
|
375
393
|
|
376
394
|
def read_attribute_for_serialization(attr)
|
@@ -404,6 +422,6 @@ module ActiveModel
|
|
404
422
|
|
405
423
|
protected
|
406
424
|
|
407
|
-
attr_accessor :instance_options
|
425
|
+
attr_accessor :instance_options, :instance_reflections
|
408
426
|
end
|
409
427
|
end
|
@@ -1,6 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveModelSerializers
|
2
4
|
module Adapter
|
3
5
|
class Attributes < Base
|
6
|
+
def initialize(*)
|
7
|
+
super
|
8
|
+
instance_options[:fieldset] ||= ActiveModel::Serializer::Fieldset.new(fields_to_fieldset(instance_options.delete(:fields)))
|
9
|
+
end
|
10
|
+
|
4
11
|
def serializable_hash(options = nil)
|
5
12
|
options = serialization_options(options)
|
6
13
|
options[:fields] ||= instance_options[:fields]
|
@@ -8,6 +15,22 @@ module ActiveModelSerializers
|
|
8
15
|
|
9
16
|
self.class.transform_key_casing!(serialized_hash, instance_options)
|
10
17
|
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def fields_to_fieldset(fields)
|
22
|
+
return fields if fields.nil?
|
23
|
+
resource_fields = []
|
24
|
+
relationship_fields = {}
|
25
|
+
fields.each do |field|
|
26
|
+
case field
|
27
|
+
when Symbol, String then resource_fields << field
|
28
|
+
when Hash then relationship_fields.merge!(field)
|
29
|
+
else fail ArgumentError, "Unknown conversion of fields to fieldset: '#{field.inspect}' in '#{fields.inspect}'"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
relationship_fields.merge!(serializer.json_key.to_sym => resource_fields)
|
33
|
+
end
|
11
34
|
end
|
12
35
|
end
|
13
36
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveModelSerializers
|
2
4
|
module Adapter
|
3
5
|
class JsonApi
|
@@ -189,7 +191,7 @@ module ActiveModelSerializers
|
|
189
191
|
|
190
192
|
polymorphic = (options[:polymorphic] || []).include?(assoc_name.to_sym)
|
191
193
|
if polymorphic
|
192
|
-
hash["#{prefix_key}_type".to_sym] = assoc_data.present? ? assoc_data['type'] : nil
|
194
|
+
hash["#{prefix_key}_type".to_sym] = assoc_data.present? ? assoc_data['type'].classify : nil
|
193
195
|
end
|
194
196
|
|
195
197
|
hash
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveModelSerializers
|
2
4
|
module Adapter
|
3
5
|
class JsonApi < Base
|
@@ -15,17 +17,18 @@ module ActiveModelSerializers
|
|
15
17
|
JsonApi::PaginationLinks requires a ActiveModelSerializers::SerializationContext.
|
16
18
|
Please pass a ':serialization_context' option or
|
17
19
|
override CollectionSerializer#paginated? to return 'false'.
|
18
|
-
|
20
|
+
EOF
|
19
21
|
end
|
20
22
|
end
|
21
23
|
|
22
24
|
def as_json
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
{
|
26
|
+
self: location_url,
|
27
|
+
first: first_page_url,
|
28
|
+
prev: prev_page_url,
|
29
|
+
next: next_page_url,
|
30
|
+
last: last_page_url
|
31
|
+
}
|
29
32
|
end
|
30
33
|
|
31
34
|
protected
|
@@ -34,25 +37,43 @@ module ActiveModelSerializers
|
|
34
37
|
|
35
38
|
private
|
36
39
|
|
37
|
-
def
|
38
|
-
|
40
|
+
def location_url
|
41
|
+
url_for_page(collection.current_page)
|
42
|
+
end
|
39
43
|
|
40
|
-
|
41
|
-
|
44
|
+
def first_page_url
|
45
|
+
url_for_page(1)
|
46
|
+
end
|
42
47
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
48
|
+
def last_page_url
|
49
|
+
if collection.total_pages == 0
|
50
|
+
url_for_page(FIRST_PAGE)
|
51
|
+
else
|
52
|
+
url_for_page(collection.total_pages)
|
53
|
+
end
|
54
|
+
end
|
47
55
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
56
|
+
def prev_page_url
|
57
|
+
return nil if collection.current_page == FIRST_PAGE
|
58
|
+
if collection.current_page > collection.total_pages
|
59
|
+
return url_for_page(collection.total_pages)
|
52
60
|
end
|
61
|
+
url_for_page(collection.current_page - FIRST_PAGE)
|
53
62
|
end
|
54
63
|
|
55
|
-
def
|
64
|
+
def next_page_url
|
65
|
+
return nil if collection.total_pages == 0 ||
|
66
|
+
collection.current_page >= collection.total_pages
|
67
|
+
url_for_page(collection.next_page)
|
68
|
+
end
|
69
|
+
|
70
|
+
def url_for_page(number)
|
71
|
+
params = query_parameters.dup
|
72
|
+
params[:page] = { size: per_page, number: number }
|
73
|
+
"#{url(adapter_options)}?#{params.to_query}"
|
74
|
+
end
|
75
|
+
|
76
|
+
def url(options = {})
|
56
77
|
@url ||= options.fetch(:links, {}).fetch(:self, nil) || request_url
|
57
78
|
end
|
58
79
|
|
@@ -63,6 +84,10 @@ module ActiveModelSerializers
|
|
63
84
|
def query_parameters
|
64
85
|
@query_parameters ||= context.query_parameters
|
65
86
|
end
|
87
|
+
|
88
|
+
def per_page
|
89
|
+
@per_page ||= collection.try(:per_page) || collection.try(:limit_value) || collection.size
|
90
|
+
end
|
66
91
|
end
|
67
92
|
end
|
68
93
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveModelSerializers
|
2
4
|
module Adapter
|
3
5
|
class JsonApi
|
@@ -43,10 +45,16 @@ module ActiveModelSerializers
|
|
43
45
|
end
|
44
46
|
|
45
47
|
def data_for_one(association)
|
46
|
-
if association
|
47
|
-
|
48
|
-
|
49
|
-
|
48
|
+
if belongs_to_id_on_self?(association)
|
49
|
+
id = parent_serializer.read_attribute_for_serialization(association.reflection.foreign_key)
|
50
|
+
type =
|
51
|
+
if association.polymorphic?
|
52
|
+
# We can't infer resource type for polymorphic relationships from the serializer.
|
53
|
+
# We can ONLY know a polymorphic resource type by inspecting each resource.
|
54
|
+
association.lazy_association.serializer.json_key
|
55
|
+
else
|
56
|
+
association.reflection.type.to_s
|
57
|
+
end
|
50
58
|
ResourceIdentifier.for_type_with_id(type, id, serializable_resource_options)
|
51
59
|
else
|
52
60
|
# TODO(BF): Process relationship without evaluating lazy_association
|
@@ -86,6 +94,12 @@ module ActiveModelSerializers
|
|
86
94
|
meta = association.meta
|
87
95
|
meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
|
88
96
|
end
|
97
|
+
|
98
|
+
def belongs_to_id_on_self?(association)
|
99
|
+
parent_serializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship &&
|
100
|
+
association.belongs_to? &&
|
101
|
+
parent_serializer.object.respond_to?(association.reflection.foreign_key)
|
102
|
+
end
|
89
103
|
end
|
90
104
|
end
|
91
105
|
end
|
@@ -1,33 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveModelSerializers
|
2
4
|
module Adapter
|
3
5
|
class JsonApi
|
4
6
|
class ResourceIdentifier
|
5
|
-
def self.type_for(
|
6
|
-
|
7
|
-
raw_type = serializer_type
|
8
|
-
else
|
9
|
-
inflection =
|
10
|
-
if ActiveModelSerializers.config.jsonapi_resource_type == :singular
|
11
|
-
:singularize
|
12
|
-
else
|
13
|
-
:pluralize
|
14
|
-
end
|
15
|
-
|
16
|
-
raw_type = class_name.underscore
|
17
|
-
raw_type = ActiveSupport::Inflector.public_send(inflection, raw_type)
|
18
|
-
raw_type
|
19
|
-
.gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
|
20
|
-
raw_type
|
21
|
-
end
|
7
|
+
def self.type_for(serializer, serializer_type = nil, transform_options = {})
|
8
|
+
raw_type = serializer_type ? serializer_type : raw_type_from_serializer_object(serializer.object)
|
22
9
|
JsonApi.send(:transform_key_casing!, raw_type, transform_options)
|
23
10
|
end
|
24
11
|
|
25
12
|
def self.for_type_with_id(type, id, options)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
13
|
+
type = inflect_type(type)
|
14
|
+
type = type_for(:no_class_needed, type, options)
|
15
|
+
if id.blank?
|
16
|
+
nil
|
17
|
+
else
|
18
|
+
{ id: id.to_s, type: type }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.raw_type_from_serializer_object(object)
|
23
|
+
class_name = object.class.name # should use model_name
|
24
|
+
raw_type = class_name.underscore
|
25
|
+
raw_type = inflect_type(raw_type)
|
26
|
+
raw_type
|
27
|
+
.gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
|
28
|
+
raw_type
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.inflect_type(type)
|
32
|
+
singularize = ActiveModelSerializers.config.jsonapi_resource_type == :singular
|
33
|
+
inflection = singularize ? :singularize : :pluralize
|
34
|
+
ActiveSupport::Inflector.public_send(inflection, type)
|
31
35
|
end
|
32
36
|
|
33
37
|
# {http://jsonapi.org/format/#document-resource-identifier-objects Resource Identifier Objects}
|
@@ -37,8 +41,11 @@ module ActiveModelSerializers
|
|
37
41
|
end
|
38
42
|
|
39
43
|
def as_json
|
40
|
-
|
41
|
-
|
44
|
+
if id.blank?
|
45
|
+
{ type: type }
|
46
|
+
else
|
47
|
+
{ id: id.to_s, type: type }
|
48
|
+
end
|
42
49
|
end
|
43
50
|
|
44
51
|
protected
|
@@ -48,7 +55,8 @@ module ActiveModelSerializers
|
|
48
55
|
private
|
49
56
|
|
50
57
|
def type_for(serializer, transform_options)
|
51
|
-
|
58
|
+
serializer_type = serializer._type
|
59
|
+
self.class.type_for(serializer, serializer_type, transform_options)
|
52
60
|
end
|
53
61
|
|
54
62
|
def id_for(serializer)
|
@@ -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,14 +24,16 @@ module ActiveModelSerializers
|
|
22
24
|
module Adapter
|
23
25
|
class JsonApi < Base
|
24
26
|
extend ActiveSupport::Autoload
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
36
|
+
end
|
33
37
|
|
34
38
|
def self.default_key_transform
|
35
39
|
:dash
|
@@ -480,7 +484,8 @@ module ActiveModelSerializers
|
|
480
484
|
# }.reject! {|_,v| v.nil? }
|
481
485
|
def links_for(serializer)
|
482
486
|
serializer._links.each_with_object({}) do |(name, value), hash|
|
483
|
-
|
487
|
+
next if value.excluded?(serializer)
|
488
|
+
result = Link.new(serializer, value.block).as_json
|
484
489
|
hash[name] = result if result
|
485
490
|
end
|
486
491
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveModelSerializers
|
2
4
|
module Adapter
|
3
5
|
UnknownAdapterError = Class.new(ArgumentError)
|
@@ -35,7 +37,7 @@ module ActiveModelSerializers
|
|
35
37
|
|
36
38
|
# @return [Array<Symbol>] list of adapter names
|
37
39
|
def adapters
|
38
|
-
adapter_map.keys.sort
|
40
|
+
adapter_map.keys.sort!
|
39
41
|
end
|
40
42
|
|
41
43
|
# Adds an adapter 'klass' with 'name' to the 'adapter_map'
|
@@ -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,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
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'set'
|
2
4
|
|
3
5
|
module ActiveModelSerializers
|
@@ -14,8 +16,8 @@ module ActiveModelSerializers
|
|
14
16
|
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
|
15
17
|
def initialize(resource, options = {})
|
16
18
|
@resource = resource
|
17
|
-
@adapter_opts,
|
18
|
-
|
19
|
+
@adapter_opts = options.select { |k, _| ADAPTER_OPTION_KEYS.include? k }
|
20
|
+
@serializer_opts = options.reject { |k, _| ADAPTER_OPTION_KEYS.include? k }
|
19
21
|
end
|
20
22
|
|
21
23
|
def serialization_scope=(scope)
|