diff options
Diffstat (limited to 'config/initializers/active_record_keyset_pagination.rb')
-rw-r--r-- | config/initializers/active_record_keyset_pagination.rb | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/config/initializers/active_record_keyset_pagination.rb b/config/initializers/active_record_keyset_pagination.rb index f5692c95276..7f830cafd31 100644 --- a/config/initializers/active_record_keyset_pagination.rb +++ b/config/initializers/active_record_keyset_pagination.rb @@ -1,10 +1,36 @@ # frozen_string_literal: true module PaginatorExtension + KEYSET_ORDER_PLACEHOLDER = Object.new + # This method loads the records for the requested page and returns a keyset paginator object. def keyset_paginate(cursor: nil, per_page: 20, keyset_order_options: {}) Gitlab::Pagination::Keyset::Paginator.new(scope: self.dup, cursor: cursor, per_page: per_page, keyset_order_options: keyset_order_options) end + + # This modifies `reverse_sql_order` so that it is aware of Gitlab::Pagination::Keyset::Order which + # can reverse order clauses with NULLS LAST because we provide it a `reversed_order_expression`. + # This allows us to use `#last` on these relations. + # + # Overrides https://github.com/rails/rails/blob/v6.1.6.1/activerecord/lib/active_record/relation/query_methods.rb#L1331-L1358 + def reverse_sql_order(order_query) + return super if order_query.empty? + + keyset_order_values = [] + + order_query_without_keyset = order_query.flat_map do |o| + next o unless o.is_a?(Gitlab::Pagination::Keyset::Order) + + keyset_order_values << o + KEYSET_ORDER_PLACEHOLDER + end + + super(order_query_without_keyset).map do |o| + next o unless o == KEYSET_ORDER_PLACEHOLDER + + keyset_order_values.shift.reversed_order + end + end end ActiveSupport.on_load(:active_record) do |