mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 06:46:50 +00:00
Always include pagination keys but set null values when not needed
This commit is contained in:
parent
ff71ef26eb
commit
16e5204eab
@ -3,7 +3,6 @@ module ActiveModelSerializers
|
|||||||
class JsonApi < Base
|
class JsonApi < Base
|
||||||
class PaginationLinks
|
class PaginationLinks
|
||||||
MissingSerializationContextError = Class.new(KeyError)
|
MissingSerializationContextError = Class.new(KeyError)
|
||||||
FIRST_PAGE = 1
|
|
||||||
|
|
||||||
attr_reader :collection, :context
|
attr_reader :collection, :context
|
||||||
|
|
||||||
@ -20,12 +19,13 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
|
|
||||||
def as_json
|
def as_json
|
||||||
per_page = collection.try(:per_page) || collection.try(:limit_value) || collection.size
|
{
|
||||||
pages_from.each_with_object({}) do |(key, value), hash|
|
"self": location_url,
|
||||||
params = query_parameters.merge(page: { number: value, size: per_page }).to_query
|
"first": first_page_url,
|
||||||
|
"prev": prev_page_url,
|
||||||
hash[key] = "#{url(adapter_options)}?#{params}"
|
"next": next_page_url,
|
||||||
end
|
"last": last_page_url
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
@ -34,37 +34,37 @@ module ActiveModelSerializers
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def pages_from
|
def location_url
|
||||||
{}.tap do |pages|
|
url_for_page(collection.current_page)
|
||||||
pages[:self] = collection.current_page
|
|
||||||
pages[:first] = FIRST_PAGE
|
|
||||||
pages[:last] = collection.total_pages
|
|
||||||
|
|
||||||
if collection.total_pages > 0
|
|
||||||
unless collection.current_page == FIRST_PAGE
|
|
||||||
pages[:prev] = collection.current_page - FIRST_PAGE
|
|
||||||
end
|
|
||||||
|
|
||||||
unless collection.current_page == collection.total_pages
|
|
||||||
pages[:next] = collection.current_page + FIRST_PAGE
|
|
||||||
end
|
|
||||||
else
|
|
||||||
pages[:last] = FIRST_PAGE
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def url(options)
|
def first_page_url
|
||||||
@url ||= options.fetch(:links, {}).fetch(:self, nil) || request_url
|
url_for_page(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
def request_url
|
def prev_page_url
|
||||||
@request_url ||= context.request_url
|
return nil if collection.first_page?
|
||||||
|
url_for_page(collection.prev_page)
|
||||||
|
end
|
||||||
|
|
||||||
|
def next_page_url
|
||||||
|
return nil if collection.last_page? || collection.out_of_range?
|
||||||
|
url_for_page(collection.next_page)
|
||||||
|
end
|
||||||
|
|
||||||
|
def url_for_page(number)
|
||||||
|
params = query_parameters.dup
|
||||||
|
params[:page] = { page: per_page, number: number }
|
||||||
|
context.url_for(action: :index, params: params)
|
||||||
end
|
end
|
||||||
|
|
||||||
def query_parameters
|
def query_parameters
|
||||||
@query_parameters ||= context.query_parameters
|
@query_parameters ||= context.query_parameters
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def per_page
|
||||||
|
@per_page ||= collection.try(:per_page) || collection.try(:limit_value) || collection.size
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user