Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cursor_pager.rb « keyset « pagination « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0b49aa87a027141b8aab222013162e6d25665d05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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