Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
e17d9529
Commit
e17d9529
authored
Aug 17, 2017
by
Toon Claes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Total Pages should be at least one
And the link to the last page cannot be `page=0`.
parent
a98d17a8
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
7 deletions
+13
-7
lib/api/helpers/pagination.rb
lib/api/helpers/pagination.rb
+11
-6
spec/lib/api/helpers/pagination_spec.rb
spec/lib/api/helpers/pagination_spec.rb
+2
-1
No files found.
lib/api/helpers/pagination.rb
View file @
e17d9529
...
...
@@ -11,7 +11,7 @@ module API
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-Total-Pages'
,
total_pages
(
paginated_data
)
.
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
...
...
@@ -26,20 +26,25 @@ module API
links
=
[]
request_params
[
:page
]
=
paginated_data
.
current_page
-
1
links
<<
%(<#{request_url}?#{request_params.to_query}>; rel="prev")
unless
paginated_data
.
first_page?
request_params
[
:page
]
=
paginated_data
.
prev_page
links
<<
%(<#{request_url}?#{request_params.to_query}>; rel="prev")
if
request_params
[
:page
]
request_params
[
:page
]
=
paginated_data
.
current_page
+
1
links
<<
%(<#{request_url}?#{request_params.to_query}>; rel="next")
unless
paginated_data
.
last_page?
||
paginated_data
.
out_of_range?
request_params
[
:page
]
=
paginated_data
.
next_page
links
<<
%(<#{request_url}?#{request_params.to_query}>; rel="next")
if
request_params
[
:page
]
request_params
[
:page
]
=
1
links
<<
%(<#{request_url}?#{request_params.to_query}>; rel="first")
request_params
[
:page
]
=
paginated_data
.
total_pages
request_params
[
:page
]
=
total_pages
(
paginated_data
)
links
<<
%(<#{request_url}?#{request_params.to_query}>; rel="last")
links
.
join
(
', '
)
end
def
total_pages
(
paginated_data
)
# Ensure there is in total at least 1 page
[
paginated_data
.
total_pages
,
1
].
max
end
end
end
end
spec/lib/api/helpers/pagination_spec.rb
View file @
e17d9529
...
...
@@ -107,7 +107,7 @@ describe API::Helpers::Pagination do
it
'adds appropriate headers'
do
expect_header
(
'X-Total'
,
'0'
)
expect_header
(
'X-Total-Pages'
,
'
0
'
)
expect_header
(
'X-Total-Pages'
,
'
1
'
)
expect_header
(
'X-Per-Page'
,
'2'
)
expect_header
(
'X-Page'
,
'1'
)
expect_header
(
'X-Next-Page'
,
''
)
...
...
@@ -118,6 +118,7 @@ describe API::Helpers::Pagination do
expect
(
val
).
to
include
(
'rel="last"'
)
expect
(
val
).
not_to
include
(
'rel="prev"'
)
expect
(
val
).
not_to
include
(
'rel="next"'
)
expect
(
val
).
not_to
include
(
'page=0'
)
end
subject
.
paginate
(
resource
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment