BigW Consortium Gitlab

Commit cc1d1411 by Andreas Brandl

Refactor and extract DefaultPaginationStrategy.

parent 7e78eacd
......@@ -2,6 +2,18 @@ module API
module Helpers
module Pagination
def paginate(relation)
DefaultPaginationStrategy.new(self).paginate(relation)
end
class DefaultPaginationStrategy
attr_reader :ctx
delegate :params, :header, :request, to: :ctx
def initialize(ctx)
@ctx = ctx
end
def paginate(relation)
relation = add_default_order(relation)
relation.page(params[:page]).per(params[:per_page]).tap do |data|
......@@ -11,6 +23,14 @@ module API
private
def add_default_order(relation)
if relation.is_a?(ActiveRecord::Relation) && relation.order_values.empty?
relation = relation.order(:id)
end
relation
end
def add_pagination_headers(paginated_data)
header 'X-Per-Page', paginated_data.limit_value.to_s
header 'X-Page', paginated_data.current_page.to_s
......@@ -53,17 +73,10 @@ module API
[paginated_data.total_pages, 1].max
end
def add_default_order(relation)
if relation.is_a?(ActiveRecord::Relation) && relation.order_values.empty?
relation = relation.order(:id)
end
relation
end
def data_without_counts?(paginated_data)
paginated_data.is_a?(Kaminari::PaginatableWithoutCount)
end
end
end
end
end
......@@ -17,8 +17,6 @@ module Gitlab
end
end
private
# Methods needed by `API::Helpers::Pagination`
#
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment