active_model_serializers 0.8.3 → 0.10.15
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 +726 -6
- data/{MIT-LICENSE.txt → MIT-LICENSE} +3 -2
- data/README.md +194 -545
- data/lib/action_controller/serialization.rb +53 -38
- data/lib/active_model/serializable_resource.rb +13 -0
- data/lib/active_model/serializer/adapter/attributes.rb +17 -0
- data/lib/active_model/serializer/adapter/base.rb +20 -0
- data/lib/active_model/serializer/adapter/json.rb +17 -0
- data/lib/active_model/serializer/adapter/json_api.rb +17 -0
- data/lib/active_model/serializer/adapter/null.rb +17 -0
- data/lib/active_model/serializer/adapter.rb +26 -0
- data/lib/active_model/serializer/array_serializer.rb +14 -0
- data/lib/active_model/serializer/association.rb +73 -0
- data/lib/active_model/serializer/attribute.rb +27 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +13 -0
- data/lib/active_model/serializer/collection_serializer.rb +99 -0
- data/lib/active_model/serializer/concerns/caching.rb +305 -0
- data/lib/active_model/serializer/error_serializer.rb +16 -0
- data/lib/active_model/serializer/errors_serializer.rb +34 -0
- data/lib/active_model/serializer/field.rb +92 -0
- data/lib/active_model/serializer/fieldset.rb +33 -0
- data/lib/active_model/serializer/has_many_reflection.rb +12 -0
- data/lib/active_model/serializer/has_one_reflection.rb +9 -0
- 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 +152 -0
- data/lib/active_model/serializer/null.rb +19 -0
- data/lib/active_model/serializer/reflection.rb +212 -0
- data/lib/active_model/serializer/version.rb +7 -0
- data/lib/active_model/serializer.rb +354 -442
- data/lib/active_model_serializers/adapter/attributes.rb +36 -0
- data/lib/active_model_serializers/adapter/base.rb +85 -0
- data/lib/active_model_serializers/adapter/json.rb +23 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +215 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +98 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +51 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +85 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +39 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +94 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +106 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +68 -0
- data/lib/active_model_serializers/adapter/json_api.rb +535 -0
- data/lib/active_model_serializers/adapter/null.rb +11 -0
- data/lib/active_model_serializers/adapter.rb +100 -0
- data/lib/active_model_serializers/callbacks.rb +57 -0
- data/lib/active_model_serializers/deprecate.rb +56 -0
- data/lib/active_model_serializers/deserialization.rb +17 -0
- data/lib/active_model_serializers/json_pointer.rb +16 -0
- data/lib/active_model_serializers/logging.rb +124 -0
- data/lib/active_model_serializers/lookup_chain.rb +82 -0
- data/lib/active_model_serializers/model.rb +132 -0
- data/lib/active_model_serializers/railtie.rb +62 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +80 -0
- data/lib/active_model_serializers/serializable_resource.rb +84 -0
- data/lib/active_model_serializers/serialization_context.rb +41 -0
- data/lib/active_model_serializers/test/schema.rb +140 -0
- data/lib/active_model_serializers/test/serializer.rb +127 -0
- data/lib/active_model_serializers/test.rb +9 -0
- data/lib/active_model_serializers.rb +49 -81
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +12 -0
- data/lib/generators/rails/serializer_generator.rb +38 -0
- data/lib/generators/rails/templates/serializer.rb.erb +8 -0
- data/lib/grape/active_model_serializers.rb +18 -0
- data/lib/grape/formatters/active_model_serializers.rb +34 -0
- data/lib/grape/helpers/active_model_serializers.rb +19 -0
- data/lib/tasks/rubocop.rake +60 -0
- metadata +240 -51
- data/.gitignore +0 -18
- data/.travis.yml +0 -28
- data/DESIGN.textile +0 -586
- data/Gemfile +0 -4
- data/Gemfile.edge +0 -9
- data/Rakefile +0 -18
- data/active_model_serializers.gemspec +0 -24
- data/bench/perf.rb +0 -43
- data/cruft.md +0 -19
- data/lib/active_model/array_serializer.rb +0 -104
- data/lib/active_model/serializer/associations.rb +0 -233
- data/lib/active_model/serializers/version.rb +0 -5
- data/lib/active_record/serializer_override.rb +0 -16
- data/lib/generators/resource_override.rb +0 -13
- data/lib/generators/serializer/USAGE +0 -9
- data/lib/generators/serializer/serializer_generator.rb +0 -42
- data/lib/generators/serializer/templates/serializer.rb +0 -19
- data/test/array_serializer_test.rb +0 -75
- data/test/association_test.rb +0 -592
- data/test/caching_test.rb +0 -96
- data/test/generators_test.rb +0 -85
- data/test/no_serialization_scope_test.rb +0 -34
- data/test/serialization_scope_name_test.rb +0 -67
- data/test/serialization_test.rb +0 -392
- data/test/serializer_support_test.rb +0 -51
- data/test/serializer_test.rb +0 -1465
- data/test/test_fakes.rb +0 -217
- data/test/test_helper.rb +0 -32
@@ -1,60 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/core_ext/class/attribute'
|
4
|
+
require 'active_model_serializers/serialization_context'
|
5
|
+
|
1
6
|
module ActionController
|
2
|
-
# Action Controller Serialization
|
3
|
-
#
|
4
|
-
# Overrides render :json to check if the given object implements +active_model_serializer+
|
5
|
-
# as a method. If so, use the returned serializer instead of calling +to_json+ on the object.
|
6
|
-
#
|
7
|
-
# This module also provides a serialization_scope method that allows you to configure the
|
8
|
-
# +serialization_scope+ of the serializer. Most apps will likely set the +serialization_scope+
|
9
|
-
# to the current user:
|
10
|
-
#
|
11
|
-
# class ApplicationController < ActionController::Base
|
12
|
-
# serialization_scope :current_user
|
13
|
-
# end
|
14
|
-
#
|
15
|
-
# If you need more complex scope rules, you can simply override the serialization_scope:
|
16
|
-
#
|
17
|
-
# class ApplicationController < ActionController::Base
|
18
|
-
# private
|
19
|
-
#
|
20
|
-
# def serialization_scope
|
21
|
-
# current_user
|
22
|
-
# end
|
23
|
-
# end
|
24
|
-
#
|
25
7
|
module Serialization
|
26
8
|
extend ActiveSupport::Concern
|
27
9
|
|
28
10
|
include ActionController::Renderers
|
29
11
|
|
12
|
+
module ClassMethods
|
13
|
+
def serialization_scope(scope)
|
14
|
+
self._serialization_scope = scope
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
30
18
|
included do
|
31
19
|
class_attribute :_serialization_scope
|
32
20
|
self._serialization_scope = :current_user
|
21
|
+
|
22
|
+
attr_writer :namespace_for_serializer
|
33
23
|
end
|
34
24
|
|
35
|
-
def
|
36
|
-
|
37
|
-
respond_to?(_serialization_scope, true)
|
25
|
+
def namespace_for_serializer
|
26
|
+
@namespace_for_serializer ||= namespace_for_class(self.class) unless namespace_for_class(self.class) == Object
|
38
27
|
end
|
39
28
|
|
40
|
-
def
|
29
|
+
def namespace_for_class(klass)
|
30
|
+
if Module.method_defined?(:module_parent)
|
31
|
+
klass.module_parent
|
32
|
+
else
|
33
|
+
klass.parent
|
34
|
+
end
|
41
35
|
end
|
42
36
|
|
43
|
-
|
44
|
-
|
45
|
-
json = ActiveModel::Serializer.build_json(self, resource, options)
|
37
|
+
def serialization_scope
|
38
|
+
return unless _serialization_scope && respond_to?(_serialization_scope, true)
|
46
39
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
40
|
+
send(_serialization_scope)
|
41
|
+
end
|
42
|
+
|
43
|
+
def get_serializer(resource, options = {})
|
44
|
+
unless use_adapter?
|
45
|
+
warn 'ActionController::Serialization#use_adapter? has been removed. '\
|
46
|
+
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new"
|
47
|
+
options[:adapter] = false
|
52
48
|
end
|
49
|
+
|
50
|
+
options.fetch(:namespace) { options[:namespace] = namespace_for_serializer }
|
51
|
+
|
52
|
+
serializable_resource = ActiveModelSerializers::SerializableResource.new(resource, options)
|
53
|
+
serializable_resource.serialization_scope ||= options.fetch(:scope) { serialization_scope }
|
54
|
+
serializable_resource.serialization_scope_name = options.fetch(:scope_name) { _serialization_scope }
|
55
|
+
# For compatibility with the JSON renderer: `json.to_json(options) if json.is_a?(String)`.
|
56
|
+
# Otherwise, since `serializable_resource` is not a string, the renderer would call
|
57
|
+
# `to_json` on a String and given odd results, such as `"".to_json #=> '""'`
|
58
|
+
serializable_resource.adapter.is_a?(String) ? serializable_resource.adapter : serializable_resource
|
53
59
|
end
|
54
60
|
|
55
|
-
|
56
|
-
|
57
|
-
|
61
|
+
# Deprecated
|
62
|
+
def use_adapter?
|
63
|
+
true
|
64
|
+
end
|
65
|
+
|
66
|
+
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
|
67
|
+
define_method renderer_method do |resource, options|
|
68
|
+
options.fetch(:serialization_context) do
|
69
|
+
options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request, options)
|
70
|
+
end
|
71
|
+
serializable_resource = get_serializer(resource, options)
|
72
|
+
super(serializable_resource, options)
|
58
73
|
end
|
59
74
|
end
|
60
75
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
module ActiveModel
|
6
|
+
class SerializableResource
|
7
|
+
class << self
|
8
|
+
extend ActiveModelSerializers::Deprecate
|
9
|
+
|
10
|
+
delegate_and_deprecate :new, ActiveModelSerializers::SerializableResource
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class Attributes < DelegateClass(ActiveModelSerializers::Adapter::Attributes)
|
7
|
+
def initialize(serializer, options = {})
|
8
|
+
super(ActiveModelSerializers::Adapter::Attributes.new(serializer, options))
|
9
|
+
end
|
10
|
+
class << self
|
11
|
+
extend ActiveModelSerializers::Deprecate
|
12
|
+
deprecate :new, 'ActiveModelSerializers::Adapter::Json.'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class Base < DelegateClass(ActiveModelSerializers::Adapter::Base)
|
7
|
+
class << self
|
8
|
+
extend ActiveModelSerializers::Deprecate
|
9
|
+
deprecate :inherited, 'ActiveModelSerializers::Adapter::Base.'
|
10
|
+
end
|
11
|
+
|
12
|
+
# :nocov:
|
13
|
+
def initialize(serializer, options = {})
|
14
|
+
super(ActiveModelSerializers::Adapter::Base.new(serializer, options))
|
15
|
+
end
|
16
|
+
# :nocov:
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class Json < DelegateClass(ActiveModelSerializers::Adapter::Json)
|
7
|
+
def initialize(serializer, options = {})
|
8
|
+
super(ActiveModelSerializers::Adapter::Json.new(serializer, options))
|
9
|
+
end
|
10
|
+
class << self
|
11
|
+
extend ActiveModelSerializers::Deprecate
|
12
|
+
deprecate :new, 'ActiveModelSerializers::Adapter::Json.new'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class JsonApi < DelegateClass(ActiveModelSerializers::Adapter::JsonApi)
|
7
|
+
def initialize(serializer, options = {})
|
8
|
+
super(ActiveModelSerializers::Adapter::JsonApi.new(serializer, options))
|
9
|
+
end
|
10
|
+
class << self
|
11
|
+
extend ActiveModelSerializers::Deprecate
|
12
|
+
deprecate :new, 'ActiveModelSerializers::Adapter::JsonApi.new'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class Null < DelegateClass(ActiveModelSerializers::Adapter::Null)
|
7
|
+
def initialize(serializer, options = {})
|
8
|
+
super(ActiveModelSerializers::Adapter::Null.new(serializer, options))
|
9
|
+
end
|
10
|
+
class << self
|
11
|
+
extend ActiveModelSerializers::Deprecate
|
12
|
+
deprecate :new, 'ActiveModelSerializers::Adapter::Null.new'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model_serializers/adapter'
|
4
|
+
require 'active_model_serializers/deprecate'
|
5
|
+
|
6
|
+
module ActiveModel
|
7
|
+
class Serializer
|
8
|
+
# @deprecated Use ActiveModelSerializers::Adapter instead
|
9
|
+
module Adapter
|
10
|
+
class << self
|
11
|
+
extend ActiveModelSerializers::Deprecate
|
12
|
+
|
13
|
+
DEPRECATED_METHODS = [:create, :adapter_class, :adapter_map, :adapters, :register, :lookup].freeze
|
14
|
+
DEPRECATED_METHODS.each do |method|
|
15
|
+
delegate_and_deprecate method, ActiveModelSerializers::Adapter
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'active_model/serializer/adapter/base'
|
23
|
+
require 'active_model/serializer/adapter/null'
|
24
|
+
require 'active_model/serializer/adapter/attributes'
|
25
|
+
require 'active_model/serializer/adapter/json'
|
26
|
+
require 'active_model/serializer/adapter/json_api'
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model/serializer/collection_serializer'
|
4
|
+
|
5
|
+
module ActiveModel
|
6
|
+
class Serializer
|
7
|
+
class ArraySerializer < CollectionSerializer
|
8
|
+
class << self
|
9
|
+
extend ActiveModelSerializers::Deprecate
|
10
|
+
deprecate :new, 'ActiveModel::Serializer::CollectionSerializer.'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model/serializer/lazy_association'
|
4
|
+
|
5
|
+
module ActiveModel
|
6
|
+
class Serializer
|
7
|
+
# This class holds all information about serializer's association.
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
Association = Struct.new(:reflection, :association_options) do
|
11
|
+
attr_reader :lazy_association
|
12
|
+
delegate :object, :include_data?, :virtual_value, :collection?, to: :lazy_association
|
13
|
+
|
14
|
+
def initialize(*)
|
15
|
+
super
|
16
|
+
@lazy_association = LazyAssociation.new(reflection, association_options)
|
17
|
+
end
|
18
|
+
|
19
|
+
# @return [Symbol]
|
20
|
+
delegate :name, to: :reflection
|
21
|
+
|
22
|
+
# @return [Symbol]
|
23
|
+
def key
|
24
|
+
reflection_options.fetch(:key, name)
|
25
|
+
end
|
26
|
+
|
27
|
+
# @return [True,False]
|
28
|
+
def key?
|
29
|
+
reflection_options.key?(:key)
|
30
|
+
end
|
31
|
+
|
32
|
+
# @return [Hash]
|
33
|
+
def links
|
34
|
+
reflection_options.fetch(:links) || {}
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [Hash, nil]
|
38
|
+
# This gets mutated, so cannot use the cached reflection_options
|
39
|
+
def meta
|
40
|
+
reflection.options[:meta]
|
41
|
+
end
|
42
|
+
|
43
|
+
def belongs_to?
|
44
|
+
reflection.foreign_key_on == :self
|
45
|
+
end
|
46
|
+
|
47
|
+
def polymorphic?
|
48
|
+
true == reflection_options[:polymorphic]
|
49
|
+
end
|
50
|
+
|
51
|
+
# @api private
|
52
|
+
def serializable_hash(adapter_options, adapter_instance)
|
53
|
+
association_serializer = lazy_association.serializer
|
54
|
+
return virtual_value if virtual_value
|
55
|
+
association_object = association_serializer && association_serializer.object
|
56
|
+
return unless association_object
|
57
|
+
|
58
|
+
serialization = association_serializer.serializable_hash(adapter_options, {}, adapter_instance)
|
59
|
+
|
60
|
+
if polymorphic? && serialization
|
61
|
+
polymorphic_type = association_object.class.name.underscore
|
62
|
+
serialization = { type: polymorphic_type, polymorphic_type.to_sym => serialization }
|
63
|
+
end
|
64
|
+
|
65
|
+
serialization
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
delegate :reflection_options, to: :lazy_association
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model/serializer/field'
|
4
|
+
|
5
|
+
module ActiveModel
|
6
|
+
class Serializer
|
7
|
+
# Holds all the meta-data about an attribute as it was specified in the
|
8
|
+
# ActiveModel::Serializer class.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# class PostSerializer < ActiveModel::Serializer
|
12
|
+
# attribute :content
|
13
|
+
# attribute :name, key: :title
|
14
|
+
# attribute :email, key: :author_email, if: :user_logged_in?
|
15
|
+
# attribute :preview do
|
16
|
+
# truncate(object.content)
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# def user_logged_in?
|
20
|
+
# current_user.logged_in?
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
class Attribute < Field
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class CollectionSerializer
|
6
|
+
include Enumerable
|
7
|
+
delegate :each, to: :@serializers
|
8
|
+
|
9
|
+
attr_reader :object, :root
|
10
|
+
|
11
|
+
def initialize(resources, options = {})
|
12
|
+
@object = resources
|
13
|
+
@options = options
|
14
|
+
@root = options[:root]
|
15
|
+
@serializers = serializers_from_resources
|
16
|
+
end
|
17
|
+
|
18
|
+
def success?
|
19
|
+
true
|
20
|
+
end
|
21
|
+
|
22
|
+
# @api private
|
23
|
+
def serializable_hash(adapter_options, options, adapter_instance)
|
24
|
+
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
|
25
|
+
options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(self, adapter_instance, options[:include_directive])
|
26
|
+
serializers.map do |serializer|
|
27
|
+
serializer.serializable_hash(adapter_options, options, adapter_instance)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# TODO: unify naming of root, json_key, and _type. Right now, a serializer's
|
32
|
+
# json_key comes from the root option or the object's model name, by default.
|
33
|
+
# But, if a dev defines a custom `json_key` method with an explicit value,
|
34
|
+
# we have no simple way to know that it is safe to call that instance method.
|
35
|
+
# (which is really a class property at this point, anyhow).
|
36
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
37
|
+
# Disabling cop since it's good to highlight the complexity of this method by
|
38
|
+
# including all the logic right here.
|
39
|
+
def json_key
|
40
|
+
return root if root
|
41
|
+
# 1. get from options[:serializer] for empty resource collection
|
42
|
+
key = object.empty? &&
|
43
|
+
(explicit_serializer_class = options[:serializer]) &&
|
44
|
+
explicit_serializer_class._type
|
45
|
+
# 2. get from first serializer instance in collection
|
46
|
+
key ||= (serializer = serializers.first) && serializer.json_key
|
47
|
+
# 3. get from collection name, if a named collection
|
48
|
+
key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil
|
49
|
+
# 4. key may be nil for empty collection and no serializer option
|
50
|
+
key &&= key.pluralize
|
51
|
+
if raise_cannot_infer_root_key_error?
|
52
|
+
# 5. fail if the key cannot be determined
|
53
|
+
key || fail(CannotInferRootKeyError, 'Cannot infer root key from collection type. Please specify the root or each_serializer option, or render a JSON String')
|
54
|
+
end
|
55
|
+
key
|
56
|
+
end
|
57
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
58
|
+
|
59
|
+
def paginated?
|
60
|
+
ActiveModelSerializers.config.jsonapi_pagination_links_enabled &&
|
61
|
+
object.respond_to?(:current_page) &&
|
62
|
+
object.respond_to?(:total_pages) &&
|
63
|
+
object.respond_to?(:size)
|
64
|
+
end
|
65
|
+
|
66
|
+
class CannotInferRootKeyError < StandardError; end
|
67
|
+
|
68
|
+
protected
|
69
|
+
|
70
|
+
attr_reader :serializers, :options
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def raise_cannot_infer_root_key_error?
|
75
|
+
ActiveModelSerializers.config.raise_cannot_infer_root_key_error
|
76
|
+
end
|
77
|
+
|
78
|
+
def serializers_from_resources
|
79
|
+
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
|
80
|
+
object.map do |resource|
|
81
|
+
serializer_from_resource(resource, serializer_context_class, options)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def serializer_from_resource(resource, serializer_context_class, options)
|
86
|
+
serializer_class = options.fetch(:serializer) do
|
87
|
+
serializer_context_class.serializer_for(resource, namespace: options[:namespace])
|
88
|
+
end
|
89
|
+
|
90
|
+
if serializer_class.nil?
|
91
|
+
ActiveModelSerializers.logger.debug "No serializer found for resource: #{resource.inspect}"
|
92
|
+
throw :no_serializer
|
93
|
+
else
|
94
|
+
serializer_class.new(resource, options.except(:serializer))
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|