2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Pagination
|
|
|
|
class OffsetPagination < Base
|
|
|
|
attr_reader :request_context
|
2022-05-07 20:08:51 +05:30
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
delegate :params, :header, :request, to: :request_context
|
|
|
|
|
|
|
|
def initialize(request_context)
|
|
|
|
@request_context = request_context
|
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
def paginate(relation, exclude_total_headers: false, skip_default_order: false, without_count: false)
|
|
|
|
ordered_relation = add_default_order(relation, skip_default_order: skip_default_order)
|
|
|
|
|
|
|
|
paginate_with_limit_optimization(ordered_relation, without_count: without_count).tap do |data|
|
2021-01-03 14:25:43 +05:30
|
|
|
add_pagination_headers(data, exclude_total_headers)
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
def paginate_with_limit_optimization(relation, without_count:)
|
2021-09-30 23:02:18 +05:30
|
|
|
pagination_data = if needs_pagination?(relation)
|
2020-06-23 00:09:42 +05:30
|
|
|
relation.page(params[:page]).per(params[:per_page])
|
2021-09-30 23:02:18 +05:30
|
|
|
else
|
|
|
|
relation
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
return pagination_data unless pagination_data.is_a?(ActiveRecord::Relation)
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
if without_count || exceeeds_count?(pagination_data)
|
2019-12-26 22:10:19 +05:30
|
|
|
# The call to `total_count_with_limit` memoizes `@arel` because of a call to `references_eager_loaded_tables?`
|
|
|
|
# We need to call `reset` because `without_count` relies on `@arel` being unmemoized
|
|
|
|
pagination_data.reset.without_count
|
|
|
|
else
|
|
|
|
pagination_data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def needs_pagination?(relation)
|
|
|
|
return true unless relation.respond_to?(:current_page)
|
|
|
|
return true if params[:page].present? && relation.current_page != params[:page].to_i
|
|
|
|
return true if params[:per_page].present? && relation.limit_value != params[:per_page].to_i
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
def add_default_order(relation, skip_default_order: false)
|
|
|
|
return relation if skip_default_order
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
if relation.is_a?(ActiveRecord::Relation) && relation.order_values.empty?
|
|
|
|
relation = relation.order(:id) # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
end
|
|
|
|
|
|
|
|
relation
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def add_pagination_headers(paginated_data, exclude_total_headers)
|
2021-02-22 17:27:13 +05:30
|
|
|
Gitlab::Pagination::OffsetHeaderBuilder.new(
|
|
|
|
request_context: self, per_page: paginated_data.limit_value, page: paginated_data.current_page,
|
|
|
|
next_page: paginated_data.next_page, prev_page: paginated_data.prev_page,
|
|
|
|
total: total_count(paginated_data), total_pages: total_pages(paginated_data)
|
|
|
|
).execute(exclude_total_headers: exclude_total_headers, data_without_counts: data_without_counts?(paginated_data))
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def data_without_counts?(paginated_data)
|
|
|
|
paginated_data.is_a?(Kaminari::PaginatableWithoutCount)
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def total_count(paginated_data)
|
|
|
|
paginated_data.total_count unless data_without_counts?(paginated_data)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def total_pages(paginated_data)
|
|
|
|
return if data_without_counts?(paginated_data)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
# Ensure there is in total at least 1 page
|
|
|
|
[paginated_data.total_pages, 1].max
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
def exceeeds_count?(paginated_data)
|
|
|
|
limited_total_count = paginated_data.total_count_with_limit
|
|
|
|
|
|
|
|
limited_total_count > Kaminari::ActiveRecordRelationMethods::MAX_COUNT_LIMIT
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|