debian-mirror-gitlab/lib/api/helpers/pagination_strategies.rb

56 lines
1.8 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
module API
module Helpers
module PaginationStrategies
2020-05-24 23:13:21 +05:30
def paginate_with_strategies(relation, request_scope)
paginator = paginator(relation, request_scope)
2020-03-13 15:44:24 +05:30
yield(paginator.paginate(relation)).tap do |records, _|
paginator.finalize(records)
end
end
2020-05-24 23:13:21 +05:30
def paginator(relation, request_scope = nil)
return keyset_paginator(relation) if keyset_pagination_enabled?
2020-03-13 15:44:24 +05:30
2020-05-24 23:13:21 +05:30
offset_paginator(relation, request_scope)
end
private
2020-03-13 15:44:24 +05:30
2020-05-24 23:13:21 +05:30
def keyset_paginator(relation)
request_context = Gitlab::Pagination::Keyset::RequestContext.new(self)
2020-03-13 15:44:24 +05:30
unless Gitlab::Pagination::Keyset.available?(request_context, relation)
return error!('Keyset pagination is not yet available for this type of request', 405)
end
Gitlab::Pagination::Keyset::Pager.new(request_context)
end
2020-05-24 23:13:21 +05:30
def offset_paginator(relation, request_scope)
offset_limit = limit_for_scope(request_scope)
if Gitlab::Pagination::Keyset.available_for_type?(relation) && offset_limit_exceeded?(offset_limit)
return error!("Offset pagination has a maximum allowed offset of #{offset_limit} " \
"for requests that return objects of type #{relation.klass}. " \
"Remaining records can be retrieved using keyset pagination.", 405)
end
Gitlab::Pagination::OffsetPagination.new(self)
end
2020-03-13 15:44:24 +05:30
def keyset_pagination_enabled?
params[:pagination] == 'keyset'
end
2020-05-24 23:13:21 +05:30
def limit_for_scope(scope)
(scope || Plan.default).actual_limits.offset_pagination_limit
end
def offset_limit_exceeded?(offset_limit)
2020-10-24 23:57:45 +05:30
offset_limit > 0 && params[:page] * params[:per_page] > offset_limit
2020-05-24 23:13:21 +05:30
end
2020-03-13 15:44:24 +05:30
end
end
end