mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Split serializable_hash into two methods.
This commit is contained in:
parent
572ff7db20
commit
285cdf841e
@ -5,8 +5,6 @@ class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapt
|
|||||||
|
|
||||||
def initialize(serializer, options = {})
|
def initialize(serializer, options = {})
|
||||||
super
|
super
|
||||||
@hash = { data: [] }
|
|
||||||
|
|
||||||
@included = ActiveModel::Serializer::Utils.include_args_to_hash(@options[:include])
|
@included = ActiveModel::Serializer::Utils.include_args_to_hash(@options[:include])
|
||||||
fields = options.delete(:fields)
|
fields = options.delete(:fields)
|
||||||
if fields
|
if fields
|
||||||
@ -19,38 +17,50 @@ class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapt
|
|||||||
def serializable_hash(options = nil)
|
def serializable_hash(options = nil)
|
||||||
options ||= {}
|
options ||= {}
|
||||||
if serializer.respond_to?(:each)
|
if serializer.respond_to?(:each)
|
||||||
serializer.each do |s|
|
serializable_hash_for_collection(serializer, options)
|
||||||
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash(options)
|
|
||||||
@hash[:data] << result[:data]
|
|
||||||
|
|
||||||
if result[:included]
|
|
||||||
@hash[:included] ||= []
|
|
||||||
@hash[:included] |= result[:included]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if serializer.paginated?
|
|
||||||
@hash[:links] ||= {}
|
|
||||||
@hash[:links].update(links_for(serializer, options))
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
primary_data = primary_data_for(serializer, options)
|
serializable_hash_for_single_resource(serializer, options)
|
||||||
relationships = relationships_for(serializer)
|
|
||||||
included = included_for(serializer)
|
|
||||||
@hash[:data] = primary_data
|
|
||||||
@hash[:data][:relationships] = relationships if relationships.any?
|
|
||||||
@hash[:included] = included if included.any?
|
|
||||||
end
|
end
|
||||||
@hash
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def fragment_cache(cached_hash, non_cached_hash)
|
def fragment_cache(cached_hash, non_cached_hash)
|
||||||
root = false if @options.include?(:include)
|
root = false if @options.include?(:include)
|
||||||
ActiveModel::Serializer::Adapter::JsonApi::FragmentCache.new().fragment_cache(root, cached_hash, non_cached_hash)
|
ActiveModel::Serializer::Adapter::JsonApi::FragmentCache.new.fragment_cache(root, cached_hash, non_cached_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def serializable_hash_for_collection(serializer, options)
|
||||||
|
hash = { data: [] }
|
||||||
|
serializer.each do |s|
|
||||||
|
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash(options)
|
||||||
|
hash[:data] << result[:data]
|
||||||
|
|
||||||
|
if result[:included]
|
||||||
|
hash[:included] ||= []
|
||||||
|
hash[:included] |= result[:included]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if serializer.paginated?
|
||||||
|
hash[:links] ||= {}
|
||||||
|
hash[:links].update(links_for(serializer, options))
|
||||||
|
end
|
||||||
|
|
||||||
|
hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def serializable_hash_for_single_resource(serializer, options)
|
||||||
|
primary_data = primary_data_for(serializer, options)
|
||||||
|
relationships = relationships_for(serializer)
|
||||||
|
included = included_for(serializer)
|
||||||
|
hash = { data: primary_data }
|
||||||
|
hash[:data][:relationships] = relationships if relationships.any?
|
||||||
|
hash[:included] = included if included.any?
|
||||||
|
|
||||||
|
hash
|
||||||
|
end
|
||||||
|
|
||||||
def resource_identifier_type_for(serializer)
|
def resource_identifier_type_for(serializer)
|
||||||
if ActiveModel::Serializer.config.jsonapi_resource_type == :singular
|
if ActiveModel::Serializer.config.jsonapi_resource_type == :singular
|
||||||
serializer.object.class.model_name.singular
|
serializer.object.class.model_name.singular
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user