active_model_serializers 0.10.0 → 0.10.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -4
- data/.travis.yml +17 -5
- data/CHANGELOG.md +112 -2
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +5 -2
- data/README.md +166 -26
- data/Rakefile +3 -3
- data/active_model_serializers.gemspec +21 -24
- data/appveyor.yml +9 -3
- data/docs/README.md +2 -1
- data/docs/general/adapters.md +4 -2
- data/docs/general/caching.md +7 -1
- data/docs/general/configuration_options.md +70 -1
- data/docs/general/deserialization.md +1 -1
- data/docs/general/fields.md +31 -0
- data/docs/general/logging.md +7 -0
- data/docs/general/rendering.md +44 -20
- data/docs/general/serializers.md +100 -11
- data/docs/howto/add_pagination_links.md +15 -16
- data/docs/howto/add_relationship_links.md +140 -0
- data/docs/howto/add_root_key.md +4 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +12 -4
- data/docs/howto/passing_arbitrary_options.md +2 -2
- data/docs/howto/serialize_poro.md +46 -5
- data/docs/howto/test.md +2 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +64 -32
- data/docs/jsonapi/schema.md +1 -1
- data/lib/action_controller/serialization.rb +13 -3
- data/lib/active_model/serializer/adapter/base.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +8 -5
- data/lib/active_model/serializer/association.rb +19 -4
- data/lib/active_model/serializer/belongs_to_reflection.rb +0 -3
- data/lib/active_model/serializer/collection_serializer.rb +35 -12
- data/lib/active_model/serializer/{associations.rb → concerns/associations.rb} +13 -11
- data/lib/active_model/serializer/{attributes.rb → concerns/attributes.rb} +1 -1
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +72 -113
- data/lib/active_model/serializer/{configuration.rb → concerns/configuration.rb} +25 -1
- data/lib/active_model/serializer/{links.rb → concerns/links.rb} +0 -0
- data/lib/active_model/serializer/{meta.rb → concerns/meta.rb} +0 -0
- data/lib/active_model/serializer/{type.rb → concerns/type.rb} +0 -0
- data/lib/active_model/serializer/error_serializer.rb +11 -7
- data/lib/active_model/serializer/errors_serializer.rb +25 -20
- data/lib/active_model/serializer/has_many_reflection.rb +0 -3
- data/lib/active_model/serializer/has_one_reflection.rb +0 -3
- data/lib/active_model/serializer/lint.rb +134 -130
- data/lib/active_model/serializer/reflection.rb +37 -21
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model/serializer.rb +76 -37
- data/lib/active_model_serializers/adapter/attributes.rb +3 -66
- data/lib/active_model_serializers/adapter/base.rb +39 -39
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +30 -19
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +23 -9
- data/lib/active_model_serializers/adapter/json_api.rb +44 -43
- data/lib/active_model_serializers/adapter.rb +6 -0
- data/lib/active_model_serializers/deprecate.rb +1 -2
- data/lib/active_model_serializers/deserialization.rb +2 -0
- data/lib/active_model_serializers/lookup_chain.rb +80 -0
- data/lib/active_model_serializers/model.rb +108 -28
- data/lib/active_model_serializers/railtie.rb +3 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +44 -31
- data/lib/active_model_serializers/serializable_resource.rb +6 -5
- data/lib/active_model_serializers/serialization_context.rb +10 -3
- data/lib/active_model_serializers/test/schema.rb +2 -2
- data/lib/active_model_serializers.rb +15 -0
- data/lib/generators/rails/resource_override.rb +1 -1
- data/lib/generators/rails/serializer_generator.rb +4 -4
- data/lib/grape/active_model_serializers.rb +7 -5
- data/lib/grape/formatters/active_model_serializers.rb +19 -2
- data/lib/grape/helpers/active_model_serializers.rb +1 -0
- data/test/action_controller/adapter_selector_test.rb +14 -5
- data/test/action_controller/explicit_serializer_test.rb +5 -4
- data/test/action_controller/json/include_test.rb +106 -27
- data/test/action_controller/json_api/errors_test.rb +8 -9
- data/test/action_controller/json_api/fields_test.rb +66 -0
- data/test/action_controller/json_api/linked_test.rb +29 -24
- data/test/action_controller/json_api/pagination_test.rb +19 -19
- data/test/action_controller/json_api/transform_test.rb +11 -3
- data/test/action_controller/lookup_proc_test.rb +49 -0
- data/test/action_controller/namespace_lookup_test.rb +232 -0
- data/test/action_controller/serialization_scope_name_test.rb +12 -6
- data/test/action_controller/serialization_test.rb +12 -9
- data/test/active_model_serializers/json_pointer_test.rb +15 -13
- data/test/active_model_serializers/model_test.rb +137 -4
- data/test/active_model_serializers/railtie_test_isolated.rb +12 -7
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
- data/test/active_model_serializers/test/schema_test.rb +3 -2
- data/test/adapter/attributes_test.rb +40 -0
- data/test/adapter/json/collection_test.rb +14 -0
- data/test/adapter/json/has_many_test.rb +10 -2
- data/test/adapter/json/transform_test.rb +15 -15
- data/test/adapter/json_api/collection_test.rb +4 -3
- data/test/adapter/json_api/errors_test.rb +17 -19
- data/test/adapter/json_api/fields_test.rb +12 -3
- data/test/adapter/json_api/has_many_test.rb +49 -20
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +183 -0
- data/test/adapter/json_api/json_api_test.rb +5 -7
- data/test/adapter/json_api/linked_test.rb +33 -12
- data/test/adapter/json_api/links_test.rb +4 -2
- data/test/adapter/json_api/pagination_links_test.rb +35 -8
- data/test/adapter/json_api/relationship_test.rb +309 -73
- data/test/adapter/json_api/resource_identifier_test.rb +27 -2
- data/test/adapter/json_api/resource_meta_test.rb +3 -3
- data/test/adapter/json_api/transform_test.rb +263 -253
- data/test/adapter/json_api/type_test.rb +1 -1
- data/test/adapter/json_test.rb +8 -7
- data/test/adapter/null_test.rb +1 -2
- data/test/adapter/polymorphic_test.rb +5 -5
- data/test/adapter_test.rb +1 -1
- data/test/benchmark/app.rb +1 -1
- data/test/benchmark/benchmarking_support.rb +1 -1
- data/test/benchmark/bm_active_record.rb +81 -0
- data/test/benchmark/bm_adapter.rb +38 -0
- data/test/benchmark/bm_caching.rb +16 -16
- data/test/benchmark/bm_lookup_chain.rb +83 -0
- data/test/benchmark/bm_transform.rb +21 -10
- data/test/benchmark/controllers.rb +16 -17
- data/test/benchmark/fixtures.rb +72 -72
- data/test/cache_test.rb +235 -69
- data/test/collection_serializer_test.rb +25 -12
- data/test/fixtures/active_record.rb +45 -10
- data/test/fixtures/poro.rb +124 -181
- data/test/generators/serializer_generator_test.rb +23 -5
- data/test/grape_test.rb +170 -56
- data/test/lint_test.rb +1 -1
- data/test/logger_test.rb +13 -11
- data/test/serializable_resource_test.rb +18 -22
- data/test/serializers/association_macros_test.rb +3 -2
- data/test/serializers/associations_test.rb +132 -36
- data/test/serializers/attribute_test.rb +5 -3
- data/test/serializers/attributes_test.rb +1 -1
- data/test/serializers/caching_configuration_test_isolated.rb +6 -6
- data/test/serializers/fieldset_test.rb +1 -1
- data/test/serializers/meta_test.rb +12 -6
- data/test/serializers/options_test.rb +17 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +3 -3
- data/test/serializers/root_test.rb +1 -1
- data/test/serializers/serialization_test.rb +2 -2
- data/test/serializers/serializer_for_test.rb +12 -10
- data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
- data/test/support/isolated_unit.rb +5 -2
- data/test/support/rails5_shims.rb +8 -2
- data/test/support/rails_app.rb +2 -9
- data/test/support/serialization_testing.rb +23 -5
- data/test/test_helper.rb +13 -0
- metadata +104 -38
- data/.rubocop_todo.yml +0 -167
- data/docs/ARCHITECTURE.md +0 -126
- data/lib/active_model/serializer/include_tree.rb +0 -111
- data/lib/active_model_serializers/key_transform.rb +0 -70
- data/test/active_model_serializers/key_transform_test.rb +0 -263
- data/test/adapter/json_api/relationships_test.rb +0 -199
- 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
@@ -7,10 +7,9 @@ module ActiveModel
|
|
7
7
|
included do
|
8
8
|
with_options instance_writer: false, instance_reader: false do |serializer|
|
9
9
|
serializer.class_attribute :_cache # @api private : the cache store
|
10
|
-
serializer.class_attribute :_fragmented # @api private : @see ::fragmented
|
11
10
|
serializer.class_attribute :_cache_key # @api private : when present, is first item in cache_key. Ignored if the serializable object defines #cache_key.
|
12
|
-
serializer.class_attribute :_cache_only # @api private : when fragment caching, whitelists
|
13
|
-
serializer.class_attribute :_cache_except # @api private : when fragment caching, blacklists
|
11
|
+
serializer.class_attribute :_cache_only # @api private : when fragment caching, whitelists fetch_attributes. Cannot combine with except
|
12
|
+
serializer.class_attribute :_cache_except # @api private : when fragment caching, blacklists fetch_attributes. Cannot combine with only
|
14
13
|
serializer.class_attribute :_cache_options # @api private : used by CachedSerializer, passed to _cache.fetch
|
15
14
|
# _cache_options include:
|
16
15
|
# expires_in
|
@@ -19,7 +18,7 @@ module ActiveModel
|
|
19
18
|
# race_condition_ttl
|
20
19
|
# Passed to ::_cache as
|
21
20
|
# serializer.cache_store.fetch(cache_key, @klass._cache_options)
|
22
|
-
# Passed as second argument to serializer.cache_store.fetch(cache_key,
|
21
|
+
# Passed as second argument to serializer.cache_store.fetch(cache_key, serializer_class._cache_options)
|
23
22
|
serializer.class_attribute :_cache_digest_file_path # @api private : Derived at inheritance
|
24
23
|
end
|
25
24
|
end
|
@@ -69,19 +68,15 @@ module ActiveModel
|
|
69
68
|
_cache_options && _cache_options[:skip_digest]
|
70
69
|
end
|
71
70
|
|
72
|
-
def
|
73
|
-
_cache_only ? _cache_only : _attributes - _cache_except
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
# Used by FragmentCache on the CachedSerializer
|
82
|
-
# to call attribute methods on the fragmented cached serializer.
|
83
|
-
def fragmented(serializer)
|
84
|
-
self._fragmented = serializer
|
71
|
+
def fragmented_attributes
|
72
|
+
cached = _cache_only ? _cache_only : _attributes - _cache_except
|
73
|
+
cached = cached.map! { |field| _attributes_keys.fetch(field, field) }
|
74
|
+
non_cached = _attributes - cached
|
75
|
+
non_cached = non_cached.map! { |field| _attributes_keys.fetch(field, field) }
|
76
|
+
{
|
77
|
+
cached: cached,
|
78
|
+
non_cached: non_cached
|
79
|
+
}
|
85
80
|
end
|
86
81
|
|
87
82
|
# Enables a serializer to be automatically cached
|
@@ -163,10 +158,10 @@ module ActiveModel
|
|
163
158
|
|
164
159
|
# Read cache from cache_store
|
165
160
|
# @return [Hash]
|
166
|
-
def cache_read_multi(collection_serializer, adapter_instance,
|
161
|
+
def cache_read_multi(collection_serializer, adapter_instance, include_directive)
|
167
162
|
return {} if ActiveModelSerializers.config.cache_store.blank?
|
168
163
|
|
169
|
-
keys = object_cache_keys(collection_serializer, adapter_instance,
|
164
|
+
keys = object_cache_keys(collection_serializer, adapter_instance, include_directive)
|
170
165
|
|
171
166
|
return {} if keys.blank?
|
172
167
|
|
@@ -176,15 +171,15 @@ module ActiveModel
|
|
176
171
|
# Find all cache_key for the collection_serializer
|
177
172
|
# @param serializers [ActiveModel::Serializer::CollectionSerializer]
|
178
173
|
# @param adapter_instance [ActiveModelSerializers::Adapter::Base]
|
179
|
-
# @param
|
174
|
+
# @param include_directive [JSONAPI::IncludeDirective]
|
180
175
|
# @return [Array] all cache_key of collection_serializer
|
181
|
-
def object_cache_keys(collection_serializer, adapter_instance,
|
176
|
+
def object_cache_keys(collection_serializer, adapter_instance, include_directive)
|
182
177
|
cache_keys = []
|
183
178
|
|
184
179
|
collection_serializer.each do |serializer|
|
185
180
|
cache_keys << object_cache_key(serializer, adapter_instance)
|
186
181
|
|
187
|
-
serializer.associations(
|
182
|
+
serializer.associations(include_directive).each do |association|
|
188
183
|
if association.serializer.respond_to?(:each)
|
189
184
|
association.serializer.each do |sub_serializer|
|
190
185
|
cache_keys << object_cache_key(sub_serializer, adapter_instance)
|
@@ -202,117 +197,77 @@ module ActiveModel
|
|
202
197
|
def object_cache_key(serializer, adapter_instance)
|
203
198
|
return unless serializer.present? && serializer.object.present?
|
204
199
|
|
205
|
-
serializer.class.cache_enabled? ? serializer.cache_key(adapter_instance) : nil
|
200
|
+
(serializer.class.cache_enabled? || serializer.class.fragment_cache_enabled?) ? serializer.cache_key(adapter_instance) : nil
|
206
201
|
end
|
207
202
|
end
|
208
203
|
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
204
|
+
### INSTANCE METHODS
|
205
|
+
def fetch_attributes(fields, cached_attributes, adapter_instance)
|
206
|
+
if serializer_class.cache_enabled?
|
207
|
+
key = cache_key(adapter_instance)
|
208
|
+
cached_attributes.fetch(key) do
|
209
|
+
serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
|
210
|
+
attributes(fields, true)
|
211
|
+
end
|
212
|
+
end
|
213
|
+
elsif serializer_class.fragment_cache_enabled?
|
214
|
+
fetch_attributes_fragment(adapter_instance, cached_attributes)
|
215
|
+
else
|
216
|
+
attributes(fields, true)
|
215
217
|
end
|
216
218
|
end
|
217
219
|
|
218
|
-
def
|
219
|
-
if
|
220
|
-
|
220
|
+
def fetch(adapter_instance, cache_options = serializer_class._cache_options)
|
221
|
+
if serializer_class.cache_store
|
222
|
+
serializer_class.cache_store.fetch(cache_key(adapter_instance), cache_options) do
|
221
223
|
yield
|
222
224
|
end
|
223
|
-
elsif self.class.fragment_cache_enabled?
|
224
|
-
fetch_fragment_cache(adapter_instance)
|
225
225
|
else
|
226
226
|
yield
|
227
227
|
end
|
228
228
|
end
|
229
229
|
|
230
|
-
# 1.
|
231
|
-
# 2.
|
232
|
-
# 3.
|
233
|
-
#
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
# 1. Determine cached attributes from serializer class options
|
255
|
-
# 2. Add cached attributes to cached Serializer
|
256
|
-
# 3. Add non-cached attributes to non-cached Serializer
|
257
|
-
def fetch_fragment_cache(adapter_instance)
|
258
|
-
serializer_class_name = self.class.name.gsub('::'.freeze, '_'.freeze)
|
259
|
-
self.class._cache_options ||= {}
|
260
|
-
self.class._cache_options[:key] = self.class._cache_key if self.class._cache_key
|
261
|
-
|
262
|
-
cached_serializer = _get_or_create_fragment_cached_serializer(serializer_class_name)
|
263
|
-
cached_hash = ActiveModelSerializers::SerializableResource.new(
|
264
|
-
object,
|
265
|
-
serializer: cached_serializer,
|
266
|
-
adapter: adapter_instance.class
|
267
|
-
).serializable_hash
|
268
|
-
|
269
|
-
non_cached_serializer = _get_or_create_fragment_non_cached_serializer(serializer_class_name)
|
270
|
-
non_cached_hash = ActiveModelSerializers::SerializableResource.new(
|
271
|
-
object,
|
272
|
-
serializer: non_cached_serializer,
|
273
|
-
adapter: adapter_instance.class
|
274
|
-
).serializable_hash
|
275
|
-
|
230
|
+
# 1. Determine cached fields from serializer class options
|
231
|
+
# 2. Get non_cached_fields and fetch cache_fields
|
232
|
+
# 3. Merge the two hashes using adapter_instance#fragment_cache
|
233
|
+
# rubocop:disable Metrics/AbcSize
|
234
|
+
def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
|
235
|
+
serializer_class._cache_options ||= {}
|
236
|
+
serializer_class._cache_options[:key] = serializer_class._cache_key if serializer_class._cache_key
|
237
|
+
fields = serializer_class.fragmented_attributes
|
238
|
+
|
239
|
+
non_cached_fields = fields[:non_cached].dup
|
240
|
+
non_cached_hash = attributes(non_cached_fields, true)
|
241
|
+
include_directive = JSONAPI::IncludeDirective.new(non_cached_fields - non_cached_hash.keys)
|
242
|
+
non_cached_hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
|
243
|
+
|
244
|
+
cached_fields = fields[:cached].dup
|
245
|
+
key = cache_key(adapter_instance)
|
246
|
+
cached_hash =
|
247
|
+
cached_attributes.fetch(key) do
|
248
|
+
serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
|
249
|
+
hash = attributes(cached_fields, true)
|
250
|
+
include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys)
|
251
|
+
hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
|
252
|
+
end
|
253
|
+
end
|
276
254
|
# Merge both results
|
277
255
|
adapter_instance.fragment_cache(cached_hash, non_cached_hash)
|
278
256
|
end
|
279
|
-
|
280
|
-
def _get_or_create_fragment_cached_serializer(serializer_class_name)
|
281
|
-
cached_serializer = _get_or_create_fragment_serializer "Cached#{serializer_class_name}"
|
282
|
-
cached_serializer.cache(self.class._cache_options)
|
283
|
-
cached_serializer.type(self.class._type)
|
284
|
-
cached_serializer.fragmented(self)
|
285
|
-
self.class.cached_attributes.each do |attribute|
|
286
|
-
options = self.class._attributes_keys[attribute] || {}
|
287
|
-
cached_serializer.attribute(attribute, options)
|
288
|
-
end
|
289
|
-
cached_serializer
|
290
|
-
end
|
291
|
-
|
292
|
-
def _get_or_create_fragment_non_cached_serializer(serializer_class_name)
|
293
|
-
non_cached_serializer = _get_or_create_fragment_serializer "NonCached#{serializer_class_name}"
|
294
|
-
non_cached_serializer.type(self.class._type)
|
295
|
-
non_cached_serializer.fragmented(self)
|
296
|
-
self.class.non_cached_attributes.each do |attribute|
|
297
|
-
options = self.class._attributes_keys[attribute] || {}
|
298
|
-
non_cached_serializer.attribute(attribute, options)
|
299
|
-
end
|
300
|
-
non_cached_serializer
|
301
|
-
end
|
302
|
-
|
303
|
-
def _get_or_create_fragment_serializer(name)
|
304
|
-
return Object.const_get(name) if Object.const_defined?(name)
|
305
|
-
Object.const_set(name, Class.new(ActiveModel::Serializer))
|
306
|
-
end
|
257
|
+
# rubocop:enable Metrics/AbcSize
|
307
258
|
|
308
259
|
def cache_key(adapter_instance)
|
309
260
|
return @cache_key if defined?(@cache_key)
|
310
261
|
|
311
262
|
parts = []
|
312
263
|
parts << object_cache_key
|
313
|
-
parts << adapter_instance.
|
314
|
-
parts <<
|
315
|
-
@cache_key = parts
|
264
|
+
parts << adapter_instance.cache_key
|
265
|
+
parts << serializer_class._cache_digest unless serializer_class._skip_digest?
|
266
|
+
@cache_key = expand_cache_key(parts)
|
267
|
+
end
|
268
|
+
|
269
|
+
def expand_cache_key(parts)
|
270
|
+
ActiveSupport::Cache.expand_cache_key(parts)
|
316
271
|
end
|
317
272
|
|
318
273
|
# Use object's cache_key if available, else derive a key from the object
|
@@ -320,14 +275,18 @@ module ActiveModel
|
|
320
275
|
def object_cache_key
|
321
276
|
if object.respond_to?(:cache_key)
|
322
277
|
object.cache_key
|
323
|
-
elsif (serializer_cache_key = (
|
278
|
+
elsif (serializer_cache_key = (serializer_class._cache_key || serializer_class._cache_options[:key]))
|
324
279
|
object_time_safe = object.updated_at
|
325
280
|
object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime)
|
326
281
|
"#{serializer_cache_key}/#{object.id}-#{object_time_safe}"
|
327
282
|
else
|
328
|
-
fail UndefinedCacheKey, "#{object.class} must define #cache_key, or the 'key:' option must be passed into '#{
|
283
|
+
fail UndefinedCacheKey, "#{object.class} must define #cache_key, or the 'key:' option must be passed into '#{serializer_class}.cache'"
|
329
284
|
end
|
330
285
|
end
|
286
|
+
|
287
|
+
def serializer_class
|
288
|
+
@serializer_class ||= self.class
|
289
|
+
end
|
331
290
|
end
|
332
291
|
end
|
333
292
|
end
|
@@ -19,14 +19,38 @@ module ActiveModel
|
|
19
19
|
collection_serializer
|
20
20
|
end
|
21
21
|
|
22
|
+
config.default_includes = '*'
|
22
23
|
config.adapter = :attributes
|
24
|
+
config.key_transform = nil
|
25
|
+
config.jsonapi_pagination_links_enabled = true
|
23
26
|
config.jsonapi_resource_type = :plural
|
27
|
+
config.jsonapi_namespace_separator = '-'.freeze
|
24
28
|
config.jsonapi_version = '1.0'
|
25
29
|
config.jsonapi_toplevel_meta = {}
|
26
30
|
# Make JSON API top-level jsonapi member opt-in
|
27
31
|
# ref: http://jsonapi.org/format/#document-top-level
|
28
32
|
config.jsonapi_include_toplevel_object = false
|
29
|
-
config.
|
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
|
30
54
|
|
31
55
|
config.schema_path = 'test/support/schemas'
|
32
56
|
end
|
File without changes
|
File without changes
|
File without changes
|
@@ -1,10 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module ActiveModel
|
2
|
+
class Serializer
|
3
|
+
class ErrorSerializer < ActiveModel::Serializer
|
4
|
+
# @return [Hash<field_name,Array<error_message>>]
|
5
|
+
def as_json
|
6
|
+
object.errors.messages
|
7
|
+
end
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
+
def success?
|
10
|
+
false
|
11
|
+
end
|
12
|
+
end
|
9
13
|
end
|
10
14
|
end
|
@@ -1,27 +1,32 @@
|
|
1
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
2
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
end
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class ErrorsSerializer
|
6
|
+
include Enumerable
|
7
|
+
delegate :each, to: :@serializers
|
8
|
+
attr_reader :object, :root
|
15
9
|
|
16
|
-
|
17
|
-
|
18
|
-
|
10
|
+
def initialize(resources, options = {})
|
11
|
+
@root = options[:root]
|
12
|
+
@object = resources
|
13
|
+
@serializers = resources.map do |resource|
|
14
|
+
serializer_class = options.fetch(:serializer) { ActiveModel::Serializer::ErrorSerializer }
|
15
|
+
serializer_class.new(resource, options.except(:serializer))
|
16
|
+
end
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
def success?
|
20
|
+
false
|
21
|
+
end
|
23
22
|
|
24
|
-
|
23
|
+
def json_key
|
24
|
+
nil
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
+
protected
|
28
|
+
|
29
|
+
attr_reader :serializers
|
30
|
+
end
|
31
|
+
end
|
27
32
|
end
|