mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #1121 from beauby/fix-jsonapi-links
Refactor `add_links` in JSONAPI adapter.
This commit is contained in:
commit
479146c02a
@ -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,26 +17,10 @@ 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
|
|
||||||
|
|
||||||
add_links(options)
|
|
||||||
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)
|
||||||
@ -48,6 +30,37 @@ class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapt
|
|||||||
|
|
||||||
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
|
||||||
@ -139,20 +152,7 @@ class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapt
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_links(options)
|
def links_for(serializer, options)
|
||||||
links = @hash.fetch(:links) { {} }
|
JsonApi::PaginationLinks.new(serializer.object, options[:context]).serializable_hash(options)
|
||||||
collection = serializer.object
|
|
||||||
@hash[:links] = add_pagination_links(links, collection, options) if paginated?(collection)
|
|
||||||
end
|
|
||||||
|
|
||||||
def add_pagination_links(links, resources, options)
|
|
||||||
pagination_links = ActiveModel::Serializer::Adapter::JsonApi::PaginationLinks.new(resources, options[:context]).serializable_hash(options)
|
|
||||||
links.update(pagination_links)
|
|
||||||
end
|
|
||||||
|
|
||||||
def paginated?(collection)
|
|
||||||
collection.respond_to?(:current_page) &&
|
|
||||||
collection.respond_to?(:total_pages) &&
|
|
||||||
collection.respond_to?(:size)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -29,6 +29,12 @@ module ActiveModel
|
|||||||
key = root || @serializers.first.try(:json_key) || object.try(:name).try(:underscore)
|
key = root || @serializers.first.try(:json_key) || object.try(:name).try(:underscore)
|
||||||
key.try(:pluralize)
|
key.try(:pluralize)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def paginated?
|
||||||
|
object.respond_to?(:current_page) &&
|
||||||
|
object.respond_to?(:total_pages) &&
|
||||||
|
object.respond_to?(:size)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user