mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Meta no longer handled in Base adapter.
This commit is contained in:
parent
7485c8487e
commit
e804d37924
@ -48,11 +48,6 @@ module ActiveModelSerializers
|
||||
Attributes.new(association.serializer, opts).serializable_hash(options)
|
||||
end
|
||||
|
||||
# no-op: Attributes adapter does not include meta data, because it does not support root.
|
||||
def include_meta(json)
|
||||
json
|
||||
end
|
||||
|
||||
# Set @cached_attributes
|
||||
def cache_attributes
|
||||
return if @cached_attributes.present?
|
||||
|
||||
@ -26,9 +26,7 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def as_json(options = nil)
|
||||
hash = serializable_hash(options)
|
||||
include_meta(hash)
|
||||
hash
|
||||
serializable_hash(options)
|
||||
end
|
||||
|
||||
def fragment_cache(cached_hash, non_cached_hash)
|
||||
@ -49,23 +47,10 @@ module ActiveModelSerializers
|
||||
options ||= {} # rubocop:disable Lint/UselessAssignment
|
||||
end
|
||||
|
||||
def meta
|
||||
instance_options.fetch(:meta, nil)
|
||||
end
|
||||
|
||||
def meta_key
|
||||
instance_options.fetch(:meta_key, 'meta'.freeze)
|
||||
end
|
||||
|
||||
def root
|
||||
serializer.json_key.to_sym if serializer.json_key
|
||||
end
|
||||
|
||||
def include_meta(json)
|
||||
json[meta_key] = meta unless meta.blank?
|
||||
json
|
||||
end
|
||||
|
||||
class << self
|
||||
# Sets the default transform for the adapter.
|
||||
#
|
||||
|
||||
@ -4,8 +4,18 @@ module ActiveModelSerializers
|
||||
def serializable_hash(options = nil)
|
||||
options = serialization_options(options)
|
||||
serialized_hash = { root => Attributes.new(serializer, instance_options).serializable_hash(options) }
|
||||
serialized_hash[meta_key] = meta unless meta.blank?
|
||||
|
||||
self.class.transform_key_casing!(serialized_hash, instance_options)
|
||||
end
|
||||
|
||||
def meta
|
||||
instance_options.fetch(:meta, nil)
|
||||
end
|
||||
|
||||
def meta_key
|
||||
instance_options.fetch(:meta_key, 'meta'.freeze)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -67,6 +67,7 @@ module ActiveModelSerializers
|
||||
# links: toplevel_links,
|
||||
# jsonapi: toplevel_jsonapi
|
||||
# }.reject! {|_,v| v.nil? }
|
||||
# rubocop:disable Metrics/CyclomaticComplexity
|
||||
def success_document
|
||||
is_collection = serializer.respond_to?(:each)
|
||||
serializers = is_collection ? serializer : [serializer]
|
||||
@ -130,8 +131,11 @@ module ActiveModelSerializers
|
||||
hash[:links].update(pagination_links_for(serializer))
|
||||
end
|
||||
|
||||
hash[:meta] = instance_options[:meta] if instance_options[:meta].is_a?(Hash)
|
||||
|
||||
hash
|
||||
end
|
||||
# rubocop:enable Metrics/CyclomaticComplexity
|
||||
|
||||
# {http://jsonapi.org/format/#errors JSON API Errors}
|
||||
# TODO: look into caching
|
||||
|
||||
@ -92,7 +92,7 @@ module ActiveModel
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_meta_key_is_used_with_json_api
|
||||
def test_meta_key_is_not_used_with_json_api
|
||||
actual = ActiveModelSerializers::SerializableResource.new(
|
||||
@blog,
|
||||
adapter: :json_api,
|
||||
@ -105,25 +105,25 @@ module ActiveModel
|
||||
type: 'blogs',
|
||||
attributes: { title: 'AMS Hints' }
|
||||
},
|
||||
'haha_meta' => { total: 10 }
|
||||
meta: { total: 10 }
|
||||
}
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
def test_meta_key_is_not_present_when_blank_object_with_json_api
|
||||
def test_meta_key_is_present_when_empty_hash_with_json_api
|
||||
actual = ActiveModelSerializers::SerializableResource.new(
|
||||
@blog,
|
||||
adapter: :json_api,
|
||||
serializer: AlternateBlogSerializer,
|
||||
meta: {},
|
||||
meta_key: 'haha_meta'
|
||||
meta: {}
|
||||
).as_json
|
||||
expected = {
|
||||
data: {
|
||||
id: '1',
|
||||
type: 'blogs',
|
||||
attributes: { title: 'AMS Hints' }
|
||||
}
|
||||
},
|
||||
meta: {}
|
||||
}
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
@ -133,8 +133,7 @@ module ActiveModel
|
||||
@blog,
|
||||
adapter: :json_api,
|
||||
serializer: AlternateBlogSerializer,
|
||||
meta: '',
|
||||
meta_key: 'haha_meta'
|
||||
meta: ''
|
||||
).as_json
|
||||
expected = {
|
||||
data: {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user