mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Remove the last of ApiObjects
This commit is contained in:
parent
e30b2a4417
commit
5d7a1a4889
@ -81,7 +81,7 @@ Example supported requests
|
|||||||
| linkage | String(type), String(id), meta | type, id | AMS::Adapter::JsonApi#primary_data_for
|
| linkage | String(type), String(id), meta | type, id | AMS::Adapter::JsonApi#primary_data_for
|
||||||
| pagination | pageObject(first), pageObject(last),<br>pageObject(prev), pageObject(next) | | AMS::Adapter::JsonApi::PaginationLinks#serializable_hash
|
| pagination | pageObject(first), pageObject(last),<br>pageObject(prev), pageObject(next) | | AMS::Adapter::JsonApi::PaginationLinks#serializable_hash
|
||||||
| pagination.pageObject | oneOf(Uri, null) | |
|
| pagination.pageObject | oneOf(Uri, null) | |
|
||||||
| jsonapi | String(version), meta | | AMS::Adapter::JsonApi::ApiObjects::JsonApi#as_json
|
| jsonapi | String(version), meta | | AMS::Adapter::JsonApi::Jsonapi#as_json
|
||||||
| error | String(id), links, String(status),<br>String(code), String(title),<br>String(detail), error.source, meta | | AM::S::ErrorSerializer, AMS::Adapter::JsonApi::Error.resource_errors
|
| error | String(id), links, String(status),<br>String(code), String(title),<br>String(detail), error.source, meta | | AM::S::ErrorSerializer, AMS::Adapter::JsonApi::Error.resource_errors
|
||||||
| error.source | String(pointer), String(parameter) | | AMS::Adapter::JsonApi::Error.error_source
|
| error.source | String(pointer), String(parameter) | | AMS::Adapter::JsonApi::Error.error_source
|
||||||
| pointer | [JSON Pointer RFC6901](https://tools.ietf.org/html/rfc6901) | | AMS::JsonPointer
|
| pointer | [JSON Pointer RFC6901](https://tools.ietf.org/html/rfc6901) | | AMS::JsonPointer
|
||||||
|
|||||||
@ -21,6 +21,12 @@ module ActiveModel
|
|||||||
|
|
||||||
config.adapter = :attributes
|
config.adapter = :attributes
|
||||||
config.jsonapi_resource_type = :plural
|
config.jsonapi_resource_type = :plural
|
||||||
|
config.jsonapi_version = '1.0'
|
||||||
|
config.jsonapi_toplevel_meta = {}
|
||||||
|
# Make JSON API top-level jsonapi member opt-in
|
||||||
|
# ref: http://jsonapi.org/format/#document-top-level
|
||||||
|
config.jsonapi_include_toplevel_object = false
|
||||||
|
|
||||||
config.schema_path = 'test/support/schemas'
|
config.schema_path = 'test/support/schemas'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -2,51 +2,16 @@ module ActiveModelSerializers
|
|||||||
module Adapter
|
module Adapter
|
||||||
class JsonApi < Base
|
class JsonApi < Base
|
||||||
extend ActiveSupport::Autoload
|
extend ActiveSupport::Autoload
|
||||||
autoload :PaginationLinks
|
|
||||||
autoload :FragmentCache
|
autoload :FragmentCache
|
||||||
autoload :Link
|
autoload :Jsonapi
|
||||||
autoload :Meta
|
|
||||||
autoload :ResourceIdentifier
|
autoload :ResourceIdentifier
|
||||||
autoload :Relationship
|
autoload :Relationship
|
||||||
|
autoload :Link
|
||||||
|
autoload :PaginationLinks
|
||||||
|
autoload :Meta
|
||||||
autoload :Error
|
autoload :Error
|
||||||
autoload :Deserialization
|
autoload :Deserialization
|
||||||
|
|
||||||
# TODO: if we like this abstraction and other API objects to it,
|
|
||||||
# then extract to its own file and require it.
|
|
||||||
module ApiObjects
|
|
||||||
# {http://jsonapi.org/format/#document-jsonapi-object Jsonapi Object}
|
|
||||||
module Jsonapi
|
|
||||||
ActiveModelSerializers.config.jsonapi_version = '1.0'
|
|
||||||
ActiveModelSerializers.config.jsonapi_toplevel_meta = {}
|
|
||||||
# Make JSON API top-level jsonapi member opt-in
|
|
||||||
# ref: http://jsonapi.org/format/#document-top-level
|
|
||||||
ActiveModelSerializers.config.jsonapi_include_toplevel_object = false
|
|
||||||
|
|
||||||
module_function
|
|
||||||
|
|
||||||
def add!(hash)
|
|
||||||
hash.merge!(object) if include_object?
|
|
||||||
end
|
|
||||||
|
|
||||||
def include_object?
|
|
||||||
ActiveModelSerializers.config.jsonapi_include_toplevel_object
|
|
||||||
end
|
|
||||||
|
|
||||||
# TODO: see if we can cache this
|
|
||||||
def object
|
|
||||||
object = {
|
|
||||||
jsonapi: {
|
|
||||||
version: ActiveModelSerializers.config.jsonapi_version,
|
|
||||||
meta: ActiveModelSerializers.config.jsonapi_toplevel_meta
|
|
||||||
}
|
|
||||||
}
|
|
||||||
object[:jsonapi].reject! { |_, v| v.blank? }
|
|
||||||
|
|
||||||
object
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize(serializer, options = {})
|
def initialize(serializer, options = {})
|
||||||
super
|
super
|
||||||
@include_tree = ActiveModel::Serializer::IncludeTree.from_include_args(options[:include])
|
@include_tree = ActiveModel::Serializer::IncludeTree.from_include_args(options[:include])
|
||||||
@ -74,7 +39,7 @@ module ActiveModelSerializers
|
|||||||
hash[:data] = is_collection ? primary_data : primary_data[0]
|
hash[:data] = is_collection ? primary_data : primary_data[0]
|
||||||
hash[:included] = included if included.any?
|
hash[:included] = included if included.any?
|
||||||
|
|
||||||
ApiObjects::Jsonapi.add!(hash)
|
Jsonapi.add!(hash)
|
||||||
|
|
||||||
if instance_options[:links]
|
if instance_options[:links]
|
||||||
hash[:links] ||= {}
|
hash[:links] ||= {}
|
||||||
@ -100,7 +65,7 @@ module ActiveModelSerializers
|
|||||||
def failure_document
|
def failure_document
|
||||||
hash = {}
|
hash = {}
|
||||||
# PR Please :)
|
# PR Please :)
|
||||||
# ApiObjects::Jsonapi.add!(hash)
|
# Jsonapi.add!(hash)
|
||||||
|
|
||||||
if serializer.respond_to?(:each)
|
if serializer.respond_to?(:each)
|
||||||
hash[:errors] = serializer.flat_map do |error_serializer|
|
hash[:errors] = serializer.flat_map do |error_serializer|
|
||||||
@ -114,7 +79,7 @@ module ActiveModelSerializers
|
|||||||
|
|
||||||
def fragment_cache(cached_hash, non_cached_hash)
|
def fragment_cache(cached_hash, non_cached_hash)
|
||||||
root = false if instance_options.include?(:include)
|
root = false if instance_options.include?(:include)
|
||||||
ActiveModelSerializers::Adapter::JsonApi::FragmentCache.new.fragment_cache(root, cached_hash, non_cached_hash)
|
FragmentCache.new.fragment_cache(root, cached_hash, non_cached_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
@ -217,7 +182,7 @@ module ActiveModelSerializers
|
|||||||
|
|
||||||
# {http://jsonapi.org/format/#fetching-pagination Pagination Links}
|
# {http://jsonapi.org/format/#fetching-pagination Pagination Links}
|
||||||
def pagination_links_for(serializer, options)
|
def pagination_links_for(serializer, options)
|
||||||
JsonApi::PaginationLinks.new(serializer.object, options[:serialization_context]).serializable_hash(options)
|
PaginationLinks.new(serializer.object, options[:serialization_context]).serializable_hash(options)
|
||||||
end
|
end
|
||||||
|
|
||||||
# {http://jsonapi.org/format/#document-meta Docment Meta}
|
# {http://jsonapi.org/format/#document-meta Docment Meta}
|
||||||
|
|||||||
31
lib/active_model_serializers/adapter/json_api/jsonapi.rb
Normal file
31
lib/active_model_serializers/adapter/json_api/jsonapi.rb
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
module ActiveModelSerializers
|
||||||
|
module Adapter
|
||||||
|
class JsonApi < Base
|
||||||
|
# {http://jsonapi.org/format/#document-jsonapi-object Jsonapi Object}
|
||||||
|
module Jsonapi
|
||||||
|
module_function
|
||||||
|
|
||||||
|
def add!(hash)
|
||||||
|
hash.merge!(object) if include_object?
|
||||||
|
end
|
||||||
|
|
||||||
|
def include_object?
|
||||||
|
ActiveModelSerializers.config.jsonapi_include_toplevel_object
|
||||||
|
end
|
||||||
|
|
||||||
|
# TODO: see if we can cache this
|
||||||
|
def object
|
||||||
|
object = {
|
||||||
|
jsonapi: {
|
||||||
|
version: ActiveModelSerializers.config.jsonapi_version,
|
||||||
|
meta: ActiveModelSerializers.config.jsonapi_toplevel_meta
|
||||||
|
}
|
||||||
|
}
|
||||||
|
object[:jsonapi].reject! { |_, v| v.blank? }
|
||||||
|
|
||||||
|
object
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user