debian-mirror-gitlab/lib/gitlab/graphql/pagination/keyset/last_items.rb

26 lines
897 B
Ruby
Raw Normal View History

2021-01-03 14:25:43 +05:30
# frozen_string_literal: true
module Gitlab
module Graphql
module Pagination
module Keyset
# This class handles the last(N) ActiveRecord call even if a special ORDER BY configuration is present.
# For the last(N) call, ActiveRecord calls reverse_order, however for some cases it raises
# ActiveRecord::IrreversibleOrderError error.
class LastItems
# rubocop: disable CodeReuse/ActiveRecord
def self.take_items(scope, count)
2021-04-17 20:07:23 +05:30
if Gitlab::Pagination::Keyset::Order.keyset_aware?(scope)
order = Gitlab::Pagination::Keyset::Order.extract_keyset_order_object(scope)
items = scope.reorder(order.reversed_order).first(count)
2021-01-03 14:25:43 +05:30
items.is_a?(Array) ? items.reverse : items
else
scope.last(count)
end
end
end
end
end
end
end