mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06: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 = {})
|
||||
super
|
||||
@hash = { data: [] }
|
||||
|
||||
@included = ActiveModel::Serializer::Utils.include_args_to_hash(@options[:include])
|
||||
fields = options.delete(:fields)
|
||||
if fields
|
||||
@ -19,26 +17,10 @@ class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapt
|
||||
def serializable_hash(options = nil)
|
||||
options ||= {}
|
||||
if serializer.respond_to?(:each)
|
||||
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
|
||||
|
||||
add_links(options)
|
||||
serializable_hash_for_collection(serializer, options)
|
||||
else
|
||||
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?
|
||||
serializable_hash_for_single_resource(serializer, options)
|
||||
end
|
||||
@hash
|
||||
end
|
||||
|
||||
def fragment_cache(cached_hash, non_cached_hash)
|
||||
@ -48,6 +30,37 @@ class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapt
|
||||
|
||||
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)
|
||||
if ActiveModel::Serializer.config.jsonapi_resource_type == :singular
|
||||
serializer.object.class.model_name.singular
|
||||
@ -139,20 +152,7 @@ class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapt
|
||||
end
|
||||
end
|
||||
|
||||
def add_links(options)
|
||||
links = @hash.fetch(:links) { {} }
|
||||
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)
|
||||
def links_for(serializer, options)
|
||||
JsonApi::PaginationLinks.new(serializer.object, options[:context]).serializable_hash(options)
|
||||
end
|
||||
end
|
||||
|
||||
@ -29,6 +29,12 @@ module ActiveModel
|
||||
key = root || @serializers.first.try(:json_key) || object.try(:name).try(:underscore)
|
||||
key.try(:pluralize)
|
||||
end
|
||||
|
||||
def paginated?
|
||||
object.respond_to?(:current_page) &&
|
||||
object.respond_to?(:total_pages) &&
|
||||
object.respond_to?(:size)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user