summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2016-01-14 12:08:44 +0100
committerRémy Coutable <remy@rymai.me>2016-01-14 12:10:27 +0100
commit3183092ca94b14d6e61f5e8ba51069554646baf8 (patch)
tree05e7d558cbf946710db76f1597e05e1ff9100904 /lib
parent9f8c38bdac3d6f532b50ecab1d769652ffb5acc3 (diff)
downloadgitlab-ce-3183092ca94b14d6e61f5e8ba51069554646baf8.tar.gz
Add pagination headers to already paginated API resourcesadd-pagination-headers-to-api
Diffstat (limited to 'lib')
-rw-r--r--lib/api/helpers.rb30
-rw-r--r--lib/api/notes.rb12
2 files changed, 22 insertions, 20 deletions
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb
index a4df810e755..312ef90915f 100644
--- a/lib/api/helpers.rb
+++ b/lib/api/helpers.rb
@@ -97,11 +97,9 @@ module API
end
def paginate(relation)
- per_page = params[:per_page].to_i
- paginated = relation.page(params[:page]).per(per_page)
- add_pagination_headers(paginated, per_page)
-
- paginated
+ relation.page(params[:page]).per(params[:per_page].to_i).tap do |data|
+ add_pagination_headers(data)
+ end
end
def authenticate!
@@ -327,16 +325,26 @@ module API
private
- def add_pagination_headers(paginated, per_page)
+ def add_pagination_headers(paginated_data)
+ header 'X-Total', paginated_data.total_count.to_s
+ header 'X-Total-Pages', paginated_data.total_pages.to_s
+ header 'X-Per-Page', paginated_data.limit_value.to_s
+ header 'X-Page', paginated_data.current_page.to_s
+ header 'X-Next-Page', paginated_data.next_page.to_s
+ header 'X-Prev-Page', paginated_data.prev_page.to_s
+ header 'Link', pagination_links(paginated_data)
+ end
+
+ def pagination_links(paginated_data)
request_url = request.url.split('?').first
links = []
- links << %(<#{request_url}?page=#{paginated.current_page - 1}&per_page=#{per_page}>; rel="prev") unless paginated.first_page?
- links << %(<#{request_url}?page=#{paginated.current_page + 1}&per_page=#{per_page}>; rel="next") unless paginated.last_page?
- links << %(<#{request_url}?page=1&per_page=#{per_page}>; rel="first")
- links << %(<#{request_url}?page=#{paginated.total_pages}&per_page=#{per_page}>; rel="last")
+ links << %(<#{request_url}?page=#{paginated_data.current_page - 1}&per_page=#{paginated_data.limit_value}>; rel="prev") unless paginated_data.first_page?
+ links << %(<#{request_url}?page=#{paginated_data.current_page + 1}&per_page=#{paginated_data.limit_value}>; rel="next") unless paginated_data.last_page?
+ links << %(<#{request_url}?page=1&per_page=#{paginated_data.limit_value}>; rel="first")
+ links << %(<#{request_url}?page=#{paginated_data.total_pages}&per_page=#{paginated_data.limit_value}>; rel="last")
- header 'Link', links.join(', ')
+ links.join(', ')
end
def abilities
diff --git a/lib/api/notes.rb b/lib/api/notes.rb
index 174473f5371..ebd9e97148c 100644
--- a/lib/api/notes.rb
+++ b/lib/api/notes.rb
@@ -22,17 +22,11 @@ module API
@noteable = user_project.send(:"#{noteables_str}").find(params[:"#{noteable_id_str}"])
# We exclude notes that are cross-references and that cannot be viewed
- # by the current user. By doing this exclusion at this level and not
- # at the DB query level (which we cannot in that case), the current
- # page can have less elements than :per_page even if
- # there's more than one page.
+ # by the current user.
notes =
- # paginate() only works with a relation. This could lead to a
- # mismatch between the pagination headers info and the actual notes
- # array returned, but this is really a edge-case.
- paginate(@noteable.notes).
+ @noteable.notes.
reject { |n| n.cross_reference_not_visible_for?(current_user) }
- present notes, with: Entities::Note
+ present paginate(Kaminari.paginate_array(notes)), with: Entities::Note
end
# Get a single +noteable+ note