mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Add pagination links automatically
Pagination links will be included in your response automatically as long as the resource is paginated using Kaminari or WillPaginate and if you are using a JSON-API adapter. The others adapters does not have this feature.
This commit is contained in:
@@ -25,9 +25,10 @@ module ActionController
|
||||
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource#serialize"
|
||||
options[:adapter] = false
|
||||
end
|
||||
if options[:pagination]
|
||||
options[:original_url] = original_url
|
||||
options[:query_parameters] = query_parameters
|
||||
if resource.respond_to?(:current_page) && resource.respond_to?(:total_pages)
|
||||
options[:pagination] = {}
|
||||
options[:pagination][:original_url] = original_url
|
||||
options[:pagination][:query_parameters] = query_parameters
|
||||
end
|
||||
ActiveModel::SerializableResource.serialize(resource, options) do |serializable_resource|
|
||||
if serializable_resource.serializer?
|
||||
|
||||
@@ -2,7 +2,7 @@ require "set"
|
||||
module ActiveModel
|
||||
class SerializableResource
|
||||
|
||||
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :pagination])
|
||||
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter])
|
||||
|
||||
def initialize(resource, options = {})
|
||||
@resource = resource
|
||||
|
||||
@@ -164,7 +164,7 @@ module ActiveModel
|
||||
def add_links(options)
|
||||
links = @hash.fetch(:links) { {} }
|
||||
resources = serializer.instance_variable_get(:@resource)
|
||||
@hash[:links] = add_pagination_links(links, resources, options) if @options[:pagination]
|
||||
@hash[:links] = add_pagination_links(links, resources, options) if options[:pagination]
|
||||
end
|
||||
|
||||
def add_pagination_links(links, resources, options)
|
||||
|
||||
@@ -14,7 +14,7 @@ module ActiveModel
|
||||
|
||||
def serializable_hash(options = {})
|
||||
pages_from.each_with_object({}) do |(key, value), hash|
|
||||
query_parameters = options.fetch(:query_parameters) { {} }
|
||||
query_parameters = options[:pagination].fetch(:query_parameters) { {} }
|
||||
params = query_parameters.merge(page: { number: value, size: collection.size }).to_query
|
||||
|
||||
hash[key] = "#{url(options)}?#{params}"
|
||||
@@ -51,7 +51,7 @@ module ActiveModel
|
||||
|
||||
def url(options)
|
||||
self_link = options.fetch(:links) {{}}
|
||||
self_link.fetch(:self) {} ? options[:links][:self] : options[:original_url]
|
||||
self_link.fetch(:self) {} ? options[:links][:self] : options[:pagination][:original_url]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user