diff options
Diffstat (limited to 'lib/gitlab/pagination')
-rw-r--r-- | lib/gitlab/pagination/cursor_based_keyset.rb | 27 | ||||
-rw-r--r-- | lib/gitlab/pagination/keyset/cursor_based_request_context.rb | 15 |
2 files changed, 37 insertions, 5 deletions
diff --git a/lib/gitlab/pagination/cursor_based_keyset.rb b/lib/gitlab/pagination/cursor_based_keyset.rb new file mode 100644 index 00000000000..f19cdf06d9a --- /dev/null +++ b/lib/gitlab/pagination/cursor_based_keyset.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module Gitlab + module Pagination + module CursorBasedKeyset + SUPPORTED_ORDERING = { + Group => { name: :asc } + }.freeze + + def self.available_for_type?(relation) + SUPPORTED_ORDERING.key?(relation.klass) + end + + def self.available?(cursor_based_request_context, relation) + available_for_type?(relation) && + order_satisfied?(relation, cursor_based_request_context) + end + + def self.order_satisfied?(relation, cursor_based_request_context) + order_by_from_request = cursor_based_request_context.order_by + + SUPPORTED_ORDERING[relation.klass] == order_by_from_request + end + private_class_method :order_satisfied? + end + end +end diff --git a/lib/gitlab/pagination/keyset/cursor_based_request_context.rb b/lib/gitlab/pagination/keyset/cursor_based_request_context.rb index d9c118ceef5..18390f5b59d 100644 --- a/lib/gitlab/pagination/keyset/cursor_based_request_context.rb +++ b/lib/gitlab/pagination/keyset/cursor_based_request_context.rb @@ -4,11 +4,12 @@ module Gitlab module Pagination module Keyset class CursorBasedRequestContext - attr_reader :request - delegate :params, :header, to: :request + DEFAULT_SORT_DIRECTION = :desc + attr_reader :request_context + delegate :params, to: :request_context - def initialize(request) - @request = request + def initialize(request_context) + @request_context = request_context end def per_page @@ -21,9 +22,13 @@ module Gitlab def apply_headers(cursor_for_next_page) Gitlab::Pagination::Keyset::HeaderBuilder - .new(self) + .new(request_context) .add_next_page_header({ cursor: cursor_for_next_page }) end + + def order_by + { params[:order_by].to_sym => params[:sort]&.to_sym || DEFAULT_SORT_DIRECTION } + end end end end |