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