mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Merge pull request #1050 from bf4/json_api_member
Add top-level jsonapi member to JSON API adapter
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
require 'thread_safe'
|
||||
require 'active_model/serializer/adapter'
|
||||
require 'active_model/serializer/array_serializer'
|
||||
require 'active_model/serializer/include_tree'
|
||||
require 'active_model/serializer/associations'
|
||||
@@ -11,6 +10,7 @@ module ActiveModel
|
||||
class Serializer
|
||||
include Configuration
|
||||
include Associations
|
||||
require 'active_model/serializer/adapter'
|
||||
|
||||
# Matches
|
||||
# "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"
|
||||
|
||||
@@ -6,6 +6,41 @@ module ActiveModel
|
||||
autoload :PaginationLinks
|
||||
autoload :FragmentCache
|
||||
|
||||
# TODO: if we like this abstraction and other API objects to it,
|
||||
# then extract to its own file and require it.
|
||||
module ApiObjects
|
||||
module JsonApi
|
||||
ActiveModel::Serializer.config.jsonapi_version = '1.0'
|
||||
ActiveModel::Serializer.config.jsonapi_toplevel_meta = {}
|
||||
# Make JSON API top-level jsonapi member opt-in
|
||||
# ref: http://jsonapi.org/format/#document-top-level
|
||||
ActiveModel::Serializer.config.jsonapi_include_toplevel_object = false
|
||||
|
||||
module_function
|
||||
|
||||
def add!(hash)
|
||||
hash.merge!(object) if include_object?
|
||||
end
|
||||
|
||||
def include_object?
|
||||
ActiveModel::Serializer.config.jsonapi_include_toplevel_object
|
||||
end
|
||||
|
||||
# TODO: see if we can cache this
|
||||
def object
|
||||
object = {
|
||||
jsonapi: {
|
||||
version: ActiveModel::Serializer.config.jsonapi_version,
|
||||
meta: ActiveModel::Serializer.config.jsonapi_toplevel_meta
|
||||
}
|
||||
}
|
||||
object[:jsonapi].reject! { |_, v| v.blank? }
|
||||
|
||||
object
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(serializer, options = {})
|
||||
super
|
||||
@include_tree = IncludeTree.from_include_args(options[:include])
|
||||
@@ -21,11 +56,16 @@ module ActiveModel
|
||||
def serializable_hash(options = nil)
|
||||
options ||= {}
|
||||
|
||||
if serializer.respond_to?(:each)
|
||||
serializable_hash_for_collection(options)
|
||||
else
|
||||
serializable_hash_for_single_resource(options)
|
||||
end
|
||||
hash =
|
||||
if serializer.respond_to?(:each)
|
||||
serializable_hash_for_collection(options)
|
||||
else
|
||||
serializable_hash_for_single_resource(options)
|
||||
end
|
||||
|
||||
ApiObjects::JsonApi.add!(hash)
|
||||
|
||||
hash
|
||||
end
|
||||
|
||||
def fragment_cache(cached_hash, non_cached_hash)
|
||||
|
||||
@@ -4,6 +4,8 @@ module ActiveModel
|
||||
include ActiveSupport::Configurable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
# Configuration options may also be set in
|
||||
# Serializers and Adapters
|
||||
included do |base|
|
||||
base.config.array_serializer = ActiveModel::Serializer::ArraySerializer
|
||||
base.config.adapter = :attributes
|
||||
|
||||
Reference in New Issue
Block a user