active_model_serializers 0.10.5 → 0.10.6
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 +4 -4
- data/.rubocop.yml +4 -1
- data/CHANGELOG.md +16 -2
- data/README.md +2 -2
- data/Rakefile +1 -30
- data/active_model_serializers.gemspec +1 -1
- data/bin/rubocop +38 -0
- data/docs/general/adapters.md +25 -9
- data/docs/general/getting_started.md +1 -1
- data/docs/general/rendering.md +18 -4
- data/docs/general/serializers.md +21 -2
- data/docs/howto/add_pagination_links.md +1 -1
- data/docs/integrations/ember-and-json-api.md +3 -0
- data/lib/active_model/serializer.rb +252 -74
- data/lib/active_model/serializer/association.rb +51 -14
- data/lib/active_model/serializer/belongs_to_reflection.rb +5 -1
- data/lib/active_model/serializer/concerns/caching.rb +29 -21
- data/lib/active_model/serializer/has_many_reflection.rb +4 -1
- data/lib/active_model/serializer/has_one_reflection.rb +1 -1
- data/lib/active_model/serializer/lazy_association.rb +95 -0
- data/lib/active_model/serializer/reflection.rb +119 -75
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api.rb +30 -17
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +38 -9
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +9 -0
- data/lib/active_model_serializers/model.rb +5 -4
- data/lib/tasks/rubocop.rake +53 -0
- data/test/action_controller/adapter_selector_test.rb +2 -2
- data/test/serializers/associations_test.rb +64 -31
- data/test/serializers/reflection_test.rb +427 -0
- metadata +9 -12
- data/lib/active_model/serializer/collection_reflection.rb +0 -7
- data/lib/active_model/serializer/concerns/associations.rb +0 -102
- data/lib/active_model/serializer/concerns/attributes.rb +0 -82
- data/lib/active_model/serializer/concerns/configuration.rb +0 -59
- data/lib/active_model/serializer/concerns/links.rb +0 -35
- data/lib/active_model/serializer/concerns/meta.rb +0 -29
- data/lib/active_model/serializer/concerns/type.rb +0 -25
- data/lib/active_model/serializer/singular_reflection.rb +0 -7
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
module ActiveModel
|
|
2
|
-
class Serializer
|
|
3
|
-
# Defines an association in the object should be rendered.
|
|
4
|
-
#
|
|
5
|
-
# The serializer object should implement the association name
|
|
6
|
-
# as a method which should return an array when invoked. If a method
|
|
7
|
-
# with the association name does not exist, the association name is
|
|
8
|
-
# dispatched to the serialized object.
|
|
9
|
-
#
|
|
10
|
-
module Associations
|
|
11
|
-
extend ActiveSupport::Concern
|
|
12
|
-
|
|
13
|
-
included do
|
|
14
|
-
with_options instance_writer: false, instance_reader: true do |serializer|
|
|
15
|
-
serializer.class_attribute :_reflections
|
|
16
|
-
self._reflections ||= {}
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
extend ActiveSupport::Autoload
|
|
20
|
-
autoload :Association
|
|
21
|
-
autoload :Reflection
|
|
22
|
-
autoload :SingularReflection
|
|
23
|
-
autoload :CollectionReflection
|
|
24
|
-
autoload :BelongsToReflection
|
|
25
|
-
autoload :HasOneReflection
|
|
26
|
-
autoload :HasManyReflection
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
module ClassMethods
|
|
30
|
-
def inherited(base)
|
|
31
|
-
super
|
|
32
|
-
base._reflections = _reflections.dup
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
# @param [Symbol] name of the association
|
|
36
|
-
# @param [Hash<Symbol => any>] options for the reflection
|
|
37
|
-
# @return [void]
|
|
38
|
-
#
|
|
39
|
-
# @example
|
|
40
|
-
# has_many :comments, serializer: CommentSummarySerializer
|
|
41
|
-
#
|
|
42
|
-
def has_many(name, options = {}, &block) # rubocop:disable Style/PredicateName
|
|
43
|
-
associate(HasManyReflection.new(name, options, block))
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
# @param [Symbol] name of the association
|
|
47
|
-
# @param [Hash<Symbol => any>] options for the reflection
|
|
48
|
-
# @return [void]
|
|
49
|
-
#
|
|
50
|
-
# @example
|
|
51
|
-
# belongs_to :author, serializer: AuthorSerializer
|
|
52
|
-
#
|
|
53
|
-
def belongs_to(name, options = {}, &block)
|
|
54
|
-
associate(BelongsToReflection.new(name, options, block))
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
# @param [Symbol] name of the association
|
|
58
|
-
# @param [Hash<Symbol => any>] options for the reflection
|
|
59
|
-
# @return [void]
|
|
60
|
-
#
|
|
61
|
-
# @example
|
|
62
|
-
# has_one :author, serializer: AuthorSerializer
|
|
63
|
-
#
|
|
64
|
-
def has_one(name, options = {}, &block) # rubocop:disable Style/PredicateName
|
|
65
|
-
associate(HasOneReflection.new(name, options, block))
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
private
|
|
69
|
-
|
|
70
|
-
# Add reflection and define {name} accessor.
|
|
71
|
-
# @param [ActiveModel::Serializer::Reflection] reflection
|
|
72
|
-
# @return [void]
|
|
73
|
-
#
|
|
74
|
-
# @api private
|
|
75
|
-
#
|
|
76
|
-
def associate(reflection)
|
|
77
|
-
key = reflection.options[:key] || reflection.name
|
|
78
|
-
self._reflections[key] = reflection
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
# @param [JSONAPI::IncludeDirective] include_directive (defaults to the
|
|
83
|
-
# +default_include_directive+ config value when not provided)
|
|
84
|
-
# @return [Enumerator<Association>]
|
|
85
|
-
#
|
|
86
|
-
def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
|
|
87
|
-
include_slice ||= include_directive
|
|
88
|
-
return unless object
|
|
89
|
-
|
|
90
|
-
Enumerator.new do |y|
|
|
91
|
-
self.class._reflections.values.each do |reflection|
|
|
92
|
-
next if reflection.excluded?(self)
|
|
93
|
-
key = reflection.options.fetch(:key, reflection.name)
|
|
94
|
-
next unless include_directive.key?(key)
|
|
95
|
-
|
|
96
|
-
y.yield reflection.build_association(self, instance_options, include_slice)
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
end
|
|
101
|
-
end
|
|
102
|
-
end
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
module ActiveModel
|
|
2
|
-
class Serializer
|
|
3
|
-
module Attributes
|
|
4
|
-
extend ActiveSupport::Concern
|
|
5
|
-
|
|
6
|
-
included do
|
|
7
|
-
with_options instance_writer: false, instance_reader: false do |serializer|
|
|
8
|
-
serializer.class_attribute :_attributes_data # @api private
|
|
9
|
-
self._attributes_data ||= {}
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
extend ActiveSupport::Autoload
|
|
13
|
-
autoload :Attribute
|
|
14
|
-
|
|
15
|
-
# Return the +attributes+ of +object+ as presented
|
|
16
|
-
# by the serializer.
|
|
17
|
-
def attributes(requested_attrs = nil, reload = false)
|
|
18
|
-
@attributes = nil if reload
|
|
19
|
-
@attributes ||= self.class._attributes_data.each_with_object({}) do |(key, attr), hash|
|
|
20
|
-
next if attr.excluded?(self)
|
|
21
|
-
next unless requested_attrs.nil? || requested_attrs.include?(key)
|
|
22
|
-
hash[key] = attr.value(self)
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
module ClassMethods
|
|
28
|
-
def inherited(base)
|
|
29
|
-
super
|
|
30
|
-
base._attributes_data = _attributes_data.dup
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
# @example
|
|
34
|
-
# class AdminAuthorSerializer < ActiveModel::Serializer
|
|
35
|
-
# attributes :id, :name, :recent_edits
|
|
36
|
-
def attributes(*attrs)
|
|
37
|
-
attrs = attrs.first if attrs.first.class == Array
|
|
38
|
-
|
|
39
|
-
attrs.each do |attr|
|
|
40
|
-
attribute(attr)
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
# @example
|
|
45
|
-
# class AdminAuthorSerializer < ActiveModel::Serializer
|
|
46
|
-
# attributes :id, :recent_edits
|
|
47
|
-
# attribute :name, key: :title
|
|
48
|
-
#
|
|
49
|
-
# attribute :full_name do
|
|
50
|
-
# "#{object.first_name} #{object.last_name}"
|
|
51
|
-
# end
|
|
52
|
-
#
|
|
53
|
-
# def recent_edits
|
|
54
|
-
# object.edits.last(5)
|
|
55
|
-
# end
|
|
56
|
-
def attribute(attr, options = {}, &block)
|
|
57
|
-
key = options.fetch(:key, attr)
|
|
58
|
-
_attributes_data[key] = Attribute.new(attr, options, block)
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
# @api private
|
|
62
|
-
# keys of attributes
|
|
63
|
-
# @see Serializer::attribute
|
|
64
|
-
def _attributes
|
|
65
|
-
_attributes_data.keys
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
# @api private
|
|
69
|
-
# maps attribute value to explicit key name
|
|
70
|
-
# @see Serializer::attribute
|
|
71
|
-
# @see FragmentCache#fragment_serializer
|
|
72
|
-
def _attributes_keys
|
|
73
|
-
_attributes_data
|
|
74
|
-
.each_with_object({}) do |(key, attr), hash|
|
|
75
|
-
next if key == attr.name
|
|
76
|
-
hash[attr.name] = { key: key }
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
end
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
module ActiveModel
|
|
2
|
-
class Serializer
|
|
3
|
-
module Configuration
|
|
4
|
-
include ActiveSupport::Configurable
|
|
5
|
-
extend ActiveSupport::Concern
|
|
6
|
-
|
|
7
|
-
# Configuration options may also be set in
|
|
8
|
-
# Serializers and Adapters
|
|
9
|
-
included do |base|
|
|
10
|
-
config = base.config
|
|
11
|
-
config.collection_serializer = ActiveModel::Serializer::CollectionSerializer
|
|
12
|
-
config.serializer_lookup_enabled = true
|
|
13
|
-
|
|
14
|
-
def config.array_serializer=(collection_serializer)
|
|
15
|
-
self.collection_serializer = collection_serializer
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def config.array_serializer
|
|
19
|
-
collection_serializer
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
config.default_includes = '*'
|
|
23
|
-
config.adapter = :attributes
|
|
24
|
-
config.key_transform = nil
|
|
25
|
-
config.jsonapi_pagination_links_enabled = true
|
|
26
|
-
config.jsonapi_resource_type = :plural
|
|
27
|
-
config.jsonapi_namespace_separator = '-'.freeze
|
|
28
|
-
config.jsonapi_version = '1.0'
|
|
29
|
-
config.jsonapi_toplevel_meta = {}
|
|
30
|
-
# Make JSON API top-level jsonapi member opt-in
|
|
31
|
-
# ref: http://jsonapi.org/format/#document-top-level
|
|
32
|
-
config.jsonapi_include_toplevel_object = false
|
|
33
|
-
config.include_data_default = true
|
|
34
|
-
|
|
35
|
-
# For configuring how serializers are found.
|
|
36
|
-
# This should be an array of procs.
|
|
37
|
-
#
|
|
38
|
-
# The priority of the output is that the first item
|
|
39
|
-
# in the evaluated result array will take precedence
|
|
40
|
-
# over other possible serializer paths.
|
|
41
|
-
#
|
|
42
|
-
# i.e.: First match wins.
|
|
43
|
-
#
|
|
44
|
-
# @example output
|
|
45
|
-
# => [
|
|
46
|
-
# "CustomNamespace::ResourceSerializer",
|
|
47
|
-
# "ParentSerializer::ResourceSerializer",
|
|
48
|
-
# "ResourceNamespace::ResourceSerializer" ,
|
|
49
|
-
# "ResourceSerializer"]
|
|
50
|
-
#
|
|
51
|
-
# If CustomNamespace::ResourceSerializer exists, it will be used
|
|
52
|
-
# for serialization
|
|
53
|
-
config.serializer_lookup_chain = ActiveModelSerializers::LookupChain::DEFAULT.dup
|
|
54
|
-
|
|
55
|
-
config.schema_path = 'test/support/schemas'
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
end
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
module ActiveModel
|
|
2
|
-
class Serializer
|
|
3
|
-
module Links
|
|
4
|
-
extend ActiveSupport::Concern
|
|
5
|
-
|
|
6
|
-
included do
|
|
7
|
-
with_options instance_writer: false, instance_reader: true do |serializer|
|
|
8
|
-
serializer.class_attribute :_links # @api private
|
|
9
|
-
self._links ||= {}
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
extend ActiveSupport::Autoload
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
module ClassMethods
|
|
16
|
-
def inherited(base)
|
|
17
|
-
super
|
|
18
|
-
base._links = _links.dup
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
# Define a link on a serializer.
|
|
22
|
-
# @example
|
|
23
|
-
# link(:self) { resource_url(object) }
|
|
24
|
-
# @example
|
|
25
|
-
# link(:self) { "http://example.com/resource/#{object.id}" }
|
|
26
|
-
# @example
|
|
27
|
-
# link :resource, "http://example.com/resource"
|
|
28
|
-
#
|
|
29
|
-
def link(name, value = nil, &block)
|
|
30
|
-
_links[name] = block || value
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
end
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
module ActiveModel
|
|
2
|
-
class Serializer
|
|
3
|
-
module Meta
|
|
4
|
-
extend ActiveSupport::Concern
|
|
5
|
-
|
|
6
|
-
included do
|
|
7
|
-
with_options instance_writer: false, instance_reader: true do |serializer|
|
|
8
|
-
serializer.class_attribute :_meta # @api private
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
extend ActiveSupport::Autoload
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
module ClassMethods
|
|
15
|
-
# Set the JSON API meta attribute of a serializer.
|
|
16
|
-
# @example
|
|
17
|
-
# class AdminAuthorSerializer < ActiveModel::Serializer
|
|
18
|
-
# meta { stuff: 'value' }
|
|
19
|
-
# @example
|
|
20
|
-
# meta do
|
|
21
|
-
# { comment_count: object.comments.count }
|
|
22
|
-
# end
|
|
23
|
-
def meta(value = nil, &block)
|
|
24
|
-
self._meta = block || value
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
module ActiveModel
|
|
2
|
-
class Serializer
|
|
3
|
-
module Type
|
|
4
|
-
extend ActiveSupport::Concern
|
|
5
|
-
|
|
6
|
-
included do
|
|
7
|
-
with_options instance_writer: false, instance_reader: true do |serializer|
|
|
8
|
-
serializer.class_attribute :_type # @api private
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
extend ActiveSupport::Autoload
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
module ClassMethods
|
|
15
|
-
# Set the JSON API type of a serializer.
|
|
16
|
-
# @example
|
|
17
|
-
# class AdminAuthorSerializer < ActiveModel::Serializer
|
|
18
|
-
# type 'authors'
|
|
19
|
-
def type(type)
|
|
20
|
-
self._type = type && type.to_s
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|