mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Refactor add_links in JSONAPI adapter.
This commit is contained in:
parent
7e2f3d122b
commit
572ff7db20
@ -29,7 +29,10 @@ class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapt
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
add_links(options)
|
if serializer.paginated?
|
||||||
|
@hash[:links] ||= {}
|
||||||
|
@hash[:links].update(links_for(serializer, options))
|
||||||
|
end
|
||||||
else
|
else
|
||||||
primary_data = primary_data_for(serializer, options)
|
primary_data = primary_data_for(serializer, options)
|
||||||
relationships = relationships_for(serializer)
|
relationships = relationships_for(serializer)
|
||||||
@ -145,20 +148,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