debian-mirror-gitlab/lib/gitlab/pagination/keyset/cursor_pager.rb

39 lines
959 B
Ruby
Raw Normal View History

2021-11-11 11:23:49 +05:30
# frozen_string_literal: true
module Gitlab
module Pagination
module Keyset
class CursorPager < Gitlab::Pagination::Base
attr_reader :cursor_based_request_context, :paginator
def initialize(cursor_based_request_context)
@cursor_based_request_context = cursor_based_request_context
end
def paginate(relation)
@paginator ||= relation.keyset_paginate(
per_page: cursor_based_request_context.per_page,
cursor: cursor_based_request_context.cursor
)
paginator.records
end
def finalize(_records = [])
# can be called only after executing `paginate(relation)`
apply_headers
end
private
def apply_headers
return unless paginator.has_next_page?
cursor_based_request_context
.apply_headers(paginator.cursor_for_next_page)
end
end
end
end
end