active_model_serializers 0.10.0.rc4 → 0.10.0.rc5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +19 -1
- data/.rubocop_todo.yml +30 -103
- data/.simplecov +0 -1
- data/.travis.yml +20 -8
- data/CHANGELOG.md +89 -5
- data/CONTRIBUTING.md +54 -179
- data/Gemfile +7 -2
- data/{LICENSE.txt → MIT-LICENSE} +0 -0
- data/README.md +27 -5
- data/Rakefile +44 -16
- data/active_model_serializers.gemspec +9 -1
- data/appveyor.yml +1 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/serve_benchmark +39 -0
- data/docs/ARCHITECTURE.md +13 -7
- data/docs/README.md +5 -1
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +99 -16
- data/docs/general/configuration_options.md +87 -14
- data/docs/general/deserialization.md +100 -0
- data/docs/general/getting_started.md +35 -0
- data/docs/general/instrumentation.md +1 -1
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/rendering.md +115 -13
- data/docs/general/serializers.md +138 -6
- data/docs/howto/add_pagination_links.md +36 -18
- data/docs/howto/outside_controller_use.md +4 -4
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema.md +29 -18
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +10 -19
- data/lib/active_model/serializable_resource.rb +4 -65
- data/lib/active_model/serializer.rb +73 -18
- data/lib/active_model/serializer/adapter.rb +15 -82
- data/lib/active_model/serializer/adapter/attributes.rb +5 -56
- data/lib/active_model/serializer/adapter/base.rb +5 -47
- data/lib/active_model/serializer/adapter/json.rb +6 -12
- data/lib/active_model/serializer/adapter/json_api.rb +5 -213
- data/lib/active_model/serializer/adapter/null.rb +7 -3
- data/lib/active_model/serializer/array_serializer.rb +3 -3
- data/lib/active_model/serializer/association.rb +4 -5
- data/lib/active_model/serializer/attributes.rb +1 -1
- data/lib/active_model/serializer/caching.rb +56 -5
- data/lib/active_model/serializer/collection_serializer.rb +30 -13
- data/lib/active_model/serializer/configuration.rb +7 -0
- data/lib/active_model/serializer/error_serializer.rb +10 -0
- data/lib/active_model/serializer/errors_serializer.rb +27 -0
- data/lib/active_model/serializer/links.rb +4 -2
- data/lib/active_model/serializer/lint.rb +14 -0
- data/lib/active_model/serializer/meta.rb +29 -0
- data/lib/active_model/serializer/null.rb +17 -0
- data/lib/active_model/serializer/reflection.rb +57 -1
- data/lib/active_model/serializer/type.rb +1 -1
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +17 -0
- data/lib/active_model_serializers/adapter.rb +92 -0
- data/lib/active_model_serializers/adapter/attributes.rb +94 -0
- data/lib/active_model_serializers/adapter/base.rb +90 -0
- data/lib/active_model_serializers/adapter/json.rb +11 -0
- data/lib/active_model_serializers/adapter/json_api.rb +513 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +57 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
- data/lib/active_model_serializers/adapter/null.rb +10 -0
- data/lib/active_model_serializers/cached_serializer.rb +87 -0
- data/lib/active_model_serializers/callbacks.rb +1 -1
- data/lib/active_model_serializers/deprecate.rb +55 -0
- data/lib/active_model_serializers/deserialization.rb +2 -2
- data/lib/active_model_serializers/fragment_cache.rb +118 -0
- data/lib/active_model_serializers/json_pointer.rb +14 -0
- data/lib/active_model_serializers/key_transform.rb +70 -0
- data/lib/active_model_serializers/logging.rb +4 -1
- data/lib/active_model_serializers/model.rb +11 -1
- data/lib/active_model_serializers/railtie.rb +9 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
- data/lib/active_model_serializers/serializable_resource.rb +81 -0
- data/lib/active_model_serializers/serialization_context.rb +24 -2
- data/lib/active_model_serializers/test/schema.rb +2 -2
- data/lib/grape/formatters/active_model_serializers.rb +1 -1
- data/test/action_controller/adapter_selector_test.rb +1 -1
- data/test/action_controller/json_api/deserialization_test.rb +56 -3
- data/test/action_controller/json_api/errors_test.rb +41 -0
- data/test/action_controller/json_api/linked_test.rb +10 -9
- data/test/action_controller/json_api/pagination_test.rb +2 -2
- data/test/action_controller/json_api/transform_test.rb +180 -0
- data/test/action_controller/serialization_scope_name_test.rb +201 -35
- data/test/action_controller/serialization_test.rb +39 -7
- data/test/active_model_serializers/adapter_for_test.rb +208 -0
- data/test/active_model_serializers/cached_serializer_test.rb +80 -0
- data/test/active_model_serializers/fragment_cache_test.rb +34 -0
- data/test/active_model_serializers/json_pointer_test.rb +20 -0
- data/test/active_model_serializers/key_transform_test.rb +263 -0
- data/test/active_model_serializers/logging_test.rb +8 -8
- data/test/active_model_serializers/railtie_test_isolated.rb +6 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
- data/test/adapter/deprecation_test.rb +100 -0
- data/test/adapter/json/belongs_to_test.rb +32 -34
- data/test/adapter/json/collection_test.rb +73 -75
- data/test/adapter/json/has_many_test.rb +36 -38
- data/test/adapter/json/transform_test.rb +93 -0
- data/test/adapter/json_api/belongs_to_test.rb +127 -129
- data/test/adapter/json_api/collection_test.rb +80 -82
- data/test/adapter/json_api/errors_test.rb +78 -0
- data/test/adapter/json_api/fields_test.rb +68 -70
- data/test/adapter/json_api/has_many_embed_ids_test.rb +32 -34
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +75 -77
- data/test/adapter/json_api/has_many_test.rb +121 -123
- data/test/adapter/json_api/has_one_test.rb +59 -61
- data/test/adapter/json_api/json_api_test.rb +28 -30
- data/test/adapter/json_api/linked_test.rb +319 -321
- data/test/adapter/json_api/links_test.rb +75 -50
- data/test/adapter/json_api/pagination_links_test.rb +115 -82
- data/test/adapter/json_api/parse_test.rb +114 -116
- data/test/adapter/json_api/relationship_test.rb +161 -0
- data/test/adapter/json_api/relationships_test.rb +199 -0
- data/test/adapter/json_api/resource_identifier_test.rb +85 -0
- data/test/adapter/json_api/resource_meta_test.rb +100 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +61 -63
- data/test/adapter/json_api/transform_test.rb +500 -0
- data/test/adapter/json_api/type_test.rb +61 -0
- data/test/adapter/json_test.rb +35 -37
- data/test/adapter/null_test.rb +13 -15
- data/test/adapter/polymorphic_test.rb +72 -0
- data/test/adapter_test.rb +27 -29
- data/test/array_serializer_test.rb +7 -8
- data/test/benchmark/app.rb +65 -0
- data/test/benchmark/benchmarking_support.rb +67 -0
- data/test/benchmark/bm_caching.rb +117 -0
- data/test/benchmark/bm_transform.rb +34 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +77 -0
- data/test/benchmark/fixtures.rb +167 -0
- data/test/cache_test.rb +388 -0
- data/test/collection_serializer_test.rb +10 -0
- data/test/fixtures/active_record.rb +12 -0
- data/test/fixtures/poro.rb +28 -3
- data/test/grape_test.rb +5 -5
- data/test/lint_test.rb +9 -0
- data/test/serializable_resource_test.rb +59 -3
- data/test/serializers/associations_test.rb +8 -8
- data/test/serializers/attribute_test.rb +7 -7
- data/test/serializers/caching_configuration_test_isolated.rb +170 -0
- data/test/serializers/meta_test.rb +74 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
- data/test/serializers/serialization_test.rb +55 -0
- data/test/support/isolated_unit.rb +3 -0
- data/test/support/rails5_shims.rb +26 -8
- data/test/support/rails_app.rb +38 -18
- data/test/support/serialization_testing.rb +5 -5
- data/test/test_helper.rb +6 -10
- metadata +132 -37
- data/docs/DESIGN.textile +7 -1
- data/lib/active_model/serializer/adapter/cached_serializer.rb +0 -45
- data/lib/active_model/serializer/adapter/fragment_cache.rb +0 -111
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +0 -13
- data/lib/active_model/serializer/adapter/json_api/deserialization.rb +0 -207
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +0 -21
- data/lib/active_model/serializer/adapter/json_api/link.rb +0 -44
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +0 -58
- data/test/active_model_serializers/serialization_context_test.rb +0 -18
- data/test/adapter/fragment_cache_test.rb +0 -38
- data/test/adapter/json_api/resource_type_config_test.rb +0 -71
- data/test/serializers/adapter_for_test.rb +0 -166
- data/test/serializers/cache_test.rb +0 -209
- data/test/support/simplecov.rb +0 -6
- data/test/support/stream_capture.rb +0 -50
- data/test/support/test_case.rb +0 -19
@@ -1,9 +1,13 @@
|
|
1
1
|
module ActiveModel
|
2
2
|
class Serializer
|
3
3
|
module Adapter
|
4
|
-
class Null <
|
5
|
-
def
|
6
|
-
|
4
|
+
class Null < DelegateClass(ActiveModelSerializers::Adapter::Null)
|
5
|
+
def initialize(serializer, options = {})
|
6
|
+
super(ActiveModelSerializers::Adapter::Null.new(serializer, options))
|
7
|
+
end
|
8
|
+
class << self
|
9
|
+
extend ActiveModelSerializers::Deprecate
|
10
|
+
deprecate :new, 'ActiveModelSerializers::Adapter::Null.new'
|
7
11
|
end
|
8
12
|
end
|
9
13
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'active_model/serializer/collection_serializer'
|
2
2
|
class ActiveModel::Serializer
|
3
3
|
class ArraySerializer < CollectionSerializer
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
class << self
|
5
|
+
extend ActiveModelSerializers::Deprecate
|
6
|
+
deprecate :new, 'ActiveModel::Serializer::CollectionSerializer.'
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
@@ -2,16 +2,15 @@ module ActiveModel
|
|
2
2
|
class Serializer
|
3
3
|
# This class hold all information about serializer's association.
|
4
4
|
#
|
5
|
-
# @
|
6
|
-
# @
|
7
|
-
# @
|
5
|
+
# @attr [Symbol] name
|
6
|
+
# @attr [ActiveModel::Serializer] serializer
|
7
|
+
# @attr [Hash{Symbol => Object}] options
|
8
8
|
#
|
9
9
|
# @example
|
10
10
|
# Association.new(:comments, CommentSummarySerializer)
|
11
11
|
#
|
12
|
-
Association = Struct.new(:name, :serializer, :options) do
|
12
|
+
Association = Struct.new(:name, :serializer, :options, :links, :meta) do
|
13
13
|
# @return [Symbol]
|
14
|
-
#
|
15
14
|
def key
|
16
15
|
options.fetch(:key, name)
|
17
16
|
end
|
@@ -68,7 +68,7 @@ module ActiveModel
|
|
68
68
|
# @api private
|
69
69
|
# maps attribute value to explict key name
|
70
70
|
# @see Serializer::attribute
|
71
|
-
# @see
|
71
|
+
# @see FragmentCache#fragment_serializer
|
72
72
|
def _attributes_keys
|
73
73
|
_attributes_data
|
74
74
|
.each_with_object({}) do |(key, attr), hash|
|
@@ -5,9 +5,9 @@ module ActiveModel
|
|
5
5
|
|
6
6
|
included do
|
7
7
|
with_options instance_writer: false, instance_reader: false do |serializer|
|
8
|
-
serializer.class_attribute :_cache # @api private : the cache
|
8
|
+
serializer.class_attribute :_cache # @api private : the cache store
|
9
9
|
serializer.class_attribute :_fragmented # @api private : @see ::fragmented
|
10
|
-
serializer.class_attribute :_cache_key # @api private : when present, is first item in cache_key
|
10
|
+
serializer.class_attribute :_cache_key # @api private : when present, is first item in cache_key. Ignored if the serializable object defines #cache_key.
|
11
11
|
serializer.class_attribute :_cache_only # @api private : when fragment caching, whitelists cached_attributes. Cannot combine with except
|
12
12
|
serializer.class_attribute :_cache_except # @api private : when fragment caching, blacklists cached_attributes. Cannot combine with only
|
13
13
|
serializer.class_attribute :_cache_options # @api private : used by CachedSerializer, passed to _cache.fetch
|
@@ -58,6 +58,10 @@ module ActiveModel
|
|
58
58
|
''.freeze
|
59
59
|
end
|
60
60
|
|
61
|
+
def _skip_digest?
|
62
|
+
_cache_options && _cache_options[:skip_digest]
|
63
|
+
end
|
64
|
+
|
61
65
|
# @api private
|
62
66
|
# Used by FragmentCache on the CachedSerializer
|
63
67
|
# to call attribute methods on the fragmented cached serializer.
|
@@ -70,7 +74,8 @@ module ActiveModel
|
|
70
74
|
# Sets +::_cache+ object to <tt>ActionController::Base.cache_store</tt>
|
71
75
|
# when Rails.configuration.action_controller.perform_caching
|
72
76
|
#
|
73
|
-
# @
|
77
|
+
# @param options [Hash] with valid keys:
|
78
|
+
# cache_store : @see ::_cache
|
74
79
|
# key : @see ::_cache_key
|
75
80
|
# only : @see ::_cache_only
|
76
81
|
# except : @see ::_cache_except
|
@@ -88,11 +93,57 @@ module ActiveModel
|
|
88
93
|
# @todo require less code comments. See
|
89
94
|
# https://github.com/rails-api/active_model_serializers/pull/1249#issuecomment-146567837
|
90
95
|
def cache(options = {})
|
91
|
-
self._cache =
|
96
|
+
self._cache =
|
97
|
+
options.delete(:cache_store) ||
|
98
|
+
ActiveModelSerializers.config.cache_store ||
|
99
|
+
ActiveSupport::Cache.lookup_store(:null_store)
|
92
100
|
self._cache_key = options.delete(:key)
|
93
101
|
self._cache_only = options.delete(:only)
|
94
102
|
self._cache_except = options.delete(:except)
|
95
|
-
self._cache_options =
|
103
|
+
self._cache_options = options.empty? ? nil : options
|
104
|
+
end
|
105
|
+
|
106
|
+
# Value is from ActiveModelSerializers.config.perform_caching. Is used to
|
107
|
+
# globally enable or disable all serializer caching, just like
|
108
|
+
# Rails.configuration.action_controller.perform_caching, which is its
|
109
|
+
# default value in a Rails application.
|
110
|
+
# @return [true, false]
|
111
|
+
# Memoizes value of config first time it is called with a non-nil value.
|
112
|
+
# rubocop:disable Style/ClassVars
|
113
|
+
def perform_caching
|
114
|
+
return @@perform_caching if defined?(@@perform_caching) && !@@perform_caching.nil?
|
115
|
+
@@perform_caching = ActiveModelSerializers.config.perform_caching
|
116
|
+
end
|
117
|
+
alias perform_caching? perform_caching
|
118
|
+
# rubocop:enable Style/ClassVars
|
119
|
+
|
120
|
+
# The canonical method for getting the cache store for the serializer.
|
121
|
+
#
|
122
|
+
# @return [nil] when _cache is not set (i.e. when `cache` has not been called)
|
123
|
+
# @return [._cache] when _cache is not the NullStore
|
124
|
+
# @return [ActiveModelSerializers.config.cache_store] when _cache is the NullStore.
|
125
|
+
# This is so we can use `cache` being called to mean the serializer should be cached
|
126
|
+
# even if ActiveModelSerializers.config.cache_store has not yet been set.
|
127
|
+
# That means that when _cache is the NullStore and ActiveModelSerializers.config.cache_store
|
128
|
+
# is configured, `cache_store` becomes `ActiveModelSerializers.config.cache_store`.
|
129
|
+
# @return [nil] when _cache is the NullStore and ActiveModelSerializers.config.cache_store is nil.
|
130
|
+
def cache_store
|
131
|
+
return nil if _cache.nil?
|
132
|
+
return _cache if _cache.class != ActiveSupport::Cache::NullStore
|
133
|
+
if ActiveModelSerializers.config.cache_store
|
134
|
+
self._cache = ActiveModelSerializers.config.cache_store
|
135
|
+
else
|
136
|
+
nil
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def cache_enabled?
|
141
|
+
perform_caching? && cache_store && !_cache_only && !_cache_except
|
142
|
+
end
|
143
|
+
|
144
|
+
def fragment_cache_enabled?
|
145
|
+
perform_caching? && cache_store &&
|
146
|
+
(_cache_only && !_cache_except || !_cache_only && _cache_except)
|
96
147
|
end
|
97
148
|
end
|
98
149
|
end
|
@@ -8,13 +8,14 @@ module ActiveModel
|
|
8
8
|
attr_reader :object, :root
|
9
9
|
|
10
10
|
def initialize(resources, options = {})
|
11
|
-
@
|
12
|
-
@
|
11
|
+
@object = resources
|
12
|
+
@options = options
|
13
|
+
@root = options[:root]
|
14
|
+
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
|
13
15
|
@serializers = resources.map do |resource|
|
14
|
-
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
|
15
16
|
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }
|
16
17
|
|
17
|
-
if serializer_class.nil?
|
18
|
+
if serializer_class.nil? # rubocop:disable Style/GuardClause
|
18
19
|
fail NoSerializerError, "No serializer found for resource: #{resource.inspect}"
|
19
20
|
else
|
20
21
|
serializer_class.new(resource, options.except(:serializer))
|
@@ -22,9 +23,32 @@ module ActiveModel
|
|
22
23
|
end
|
23
24
|
end
|
24
25
|
|
26
|
+
def success?
|
27
|
+
true
|
28
|
+
end
|
29
|
+
|
30
|
+
# TODO: unify naming of root, json_key, and _type. Right now, a serializer's
|
31
|
+
# json_key comes from the root option or the object's model name, by default.
|
32
|
+
# But, if a dev defines a custom `json_key` method with an explicit value,
|
33
|
+
# we have no simple way to know that it is safe to call that instance method.
|
34
|
+
# (which is really a class property at this point, anyhow).
|
35
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
36
|
+
# Disabling cop since it's good to highlight the complexity of this method by
|
37
|
+
# including all the logic right here.
|
25
38
|
def json_key
|
26
|
-
root
|
39
|
+
return root if root
|
40
|
+
# 1. get from options[:serializer] for empty resource collection
|
41
|
+
key = object.empty? &&
|
42
|
+
(explicit_serializer_class = options[:serializer]) &&
|
43
|
+
explicit_serializer_class._type
|
44
|
+
# 2. get from first serializer instance in collection
|
45
|
+
key ||= (serializer = serializers.first) && serializer.json_key
|
46
|
+
# 3. get from collection name, if a named collection
|
47
|
+
key ||= object.respond_to?(:name) ? object.name && object.name.underscore : nil
|
48
|
+
# 4. key may be nil for empty collection and no serializer option
|
49
|
+
key && key.pluralize
|
27
50
|
end
|
51
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
28
52
|
|
29
53
|
def paginated?
|
30
54
|
object.respond_to?(:current_page) &&
|
@@ -34,14 +58,7 @@ module ActiveModel
|
|
34
58
|
|
35
59
|
protected
|
36
60
|
|
37
|
-
attr_reader :serializers
|
38
|
-
|
39
|
-
private
|
40
|
-
|
41
|
-
def derived_root
|
42
|
-
key = serializers.first.try(:json_key) || object.try(:name).try(:underscore)
|
43
|
-
key.try(:pluralize)
|
44
|
-
end
|
61
|
+
attr_reader :serializers, :options
|
45
62
|
end
|
46
63
|
end
|
47
64
|
end
|
@@ -21,6 +21,13 @@ module ActiveModel
|
|
21
21
|
|
22
22
|
config.adapter = :attributes
|
23
23
|
config.jsonapi_resource_type = :plural
|
24
|
+
config.jsonapi_version = '1.0'
|
25
|
+
config.jsonapi_toplevel_meta = {}
|
26
|
+
# Make JSON API top-level jsonapi member opt-in
|
27
|
+
# ref: http://jsonapi.org/format/#document-top-level
|
28
|
+
config.jsonapi_include_toplevel_object = false
|
29
|
+
config.key_transform = nil
|
30
|
+
|
24
31
|
config.schema_path = 'test/support/schemas'
|
25
32
|
end
|
26
33
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'active_model/serializer/error_serializer'
|
2
|
+
class ActiveModel::Serializer::ErrorsSerializer
|
3
|
+
include Enumerable
|
4
|
+
delegate :each, to: :@serializers
|
5
|
+
attr_reader :object, :root
|
6
|
+
|
7
|
+
def initialize(resources, options = {})
|
8
|
+
@root = options[:root]
|
9
|
+
@object = resources
|
10
|
+
@serializers = resources.map do |resource|
|
11
|
+
serializer_class = options.fetch(:serializer) { ActiveModel::Serializer::ErrorSerializer }
|
12
|
+
serializer_class.new(resource, options.except(:serializer))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def success?
|
17
|
+
false
|
18
|
+
end
|
19
|
+
|
20
|
+
def json_key
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
attr_reader :serializers
|
27
|
+
end
|
@@ -20,9 +20,11 @@ module ActiveModel
|
|
20
20
|
|
21
21
|
# Define a link on a serializer.
|
22
22
|
# @example
|
23
|
-
# link
|
23
|
+
# link(:self) { resource_url(object) }
|
24
24
|
# @example
|
25
|
-
# link
|
25
|
+
# link(:self) { "http://example.com/resource/#{object.id}" }
|
26
|
+
# @example
|
27
|
+
# link :resource, "http://example.com/resource"
|
26
28
|
#
|
27
29
|
def link(name, value = nil, &block)
|
28
30
|
_links[name] = block || value
|
@@ -129,6 +129,20 @@ module ActiveModel::Serializer::Lint
|
|
129
129
|
assert_instance_of resource_class.model_name, ActiveModel::Name
|
130
130
|
end
|
131
131
|
|
132
|
+
def test_active_model_errors
|
133
|
+
assert_respond_to resource, :errors
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_active_model_errors_human_attribute_name
|
137
|
+
assert_respond_to resource.class, :human_attribute_name
|
138
|
+
assert_equal(-2, resource.class.method(:human_attribute_name).arity)
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_active_model_errors_lookup_ancestors
|
142
|
+
assert_respond_to resource.class, :lookup_ancestors
|
143
|
+
assert_equal 0, resource.class.method(:lookup_ancestors).arity
|
144
|
+
end
|
145
|
+
|
132
146
|
private
|
133
147
|
|
134
148
|
def resource
|
@@ -0,0 +1,29 @@
|
|
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
|
@@ -34,6 +34,57 @@ module ActiveModel
|
|
34
34
|
# So you can inspect reflections in your Adapters.
|
35
35
|
#
|
36
36
|
class Reflection < Field
|
37
|
+
def initialize(*)
|
38
|
+
super
|
39
|
+
@_links = {}
|
40
|
+
@_include_data = true
|
41
|
+
@_meta = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
def link(name, value = nil, &block)
|
45
|
+
@_links[name] = block || value
|
46
|
+
:nil
|
47
|
+
end
|
48
|
+
|
49
|
+
def meta(value = nil, &block)
|
50
|
+
@_meta = block || value
|
51
|
+
:nil
|
52
|
+
end
|
53
|
+
|
54
|
+
def include_data(value = true)
|
55
|
+
@_include_data = value
|
56
|
+
:nil
|
57
|
+
end
|
58
|
+
|
59
|
+
# @param serializer [ActiveModel::Serializer]
|
60
|
+
# @yield [ActiveModel::Serializer]
|
61
|
+
# @return [:nil, associated resource or resource collection]
|
62
|
+
# @example
|
63
|
+
# has_one :blog do |serializer|
|
64
|
+
# serializer.cached_blog
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# def cached_blog
|
68
|
+
# cache_store.fetch("cached_blog:#{object.updated_at}") do
|
69
|
+
# Blog.find(object.blog_id)
|
70
|
+
# end
|
71
|
+
# end
|
72
|
+
def value(serializer)
|
73
|
+
@object = serializer.object
|
74
|
+
@scope = serializer.scope
|
75
|
+
|
76
|
+
if block
|
77
|
+
block_value = instance_exec(serializer, &block)
|
78
|
+
if block_value == :nil
|
79
|
+
serializer.read_attribute_for_serialization(name)
|
80
|
+
else
|
81
|
+
block_value
|
82
|
+
end
|
83
|
+
else
|
84
|
+
serializer.read_attribute_for_serialization(name)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
37
88
|
# Build association. This method is used internally to
|
38
89
|
# build serializer's association by its reflection.
|
39
90
|
#
|
@@ -59,6 +110,7 @@ module ActiveModel
|
|
59
110
|
association_value = value(subject)
|
60
111
|
reflection_options = options.dup
|
61
112
|
serializer_class = subject.class.serializer_for(association_value, reflection_options)
|
113
|
+
reflection_options[:include_data] = @_include_data
|
62
114
|
|
63
115
|
if serializer_class
|
64
116
|
begin
|
@@ -73,9 +125,13 @@ module ActiveModel
|
|
73
125
|
reflection_options[:virtual_value] = association_value
|
74
126
|
end
|
75
127
|
|
76
|
-
Association.new(name, serializer, reflection_options)
|
128
|
+
Association.new(name, serializer, reflection_options, @_links, @_meta)
|
77
129
|
end
|
78
130
|
|
131
|
+
protected
|
132
|
+
|
133
|
+
attr_accessor :object, :scope
|
134
|
+
|
79
135
|
private
|
80
136
|
|
81
137
|
def serializer_options(subject, parent_serializer_options, reflection_options)
|