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
c1ed642f
Commit
c1ed642f
authored
Dec 14, 2016
by
James Lopez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added spec and pagination class to emulate paginary for using with Redis returned arrays.
parent
f1ddbea6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
216 additions
and
0 deletions
+216
-0
lib/gitlab/pagination_delegate.rb
lib/gitlab/pagination_delegate.rb
+67
-0
spec/lib/gitlab/pagination_delegate_spec.rb
spec/lib/gitlab/pagination_delegate_spec.rb
+149
-0
No files found.
lib/gitlab/pagination_delegate.rb
0 → 100644
View file @
c1ed642f
module
Gitlab
class
PaginationDelegate
DEFAULT_PER_PAGE
=
Kaminari
.
config
.
default_per_page
MAX_PER_PAGE
=
Kaminari
.
config
.
max_per_page
def
initialize
(
page
:,
per_page
:,
count
:,
options:
{})
@count
=
count
@options
=
{
default_per_page:
DEFAULT_PER_PAGE
,
max_per_page:
MAX_PER_PAGE
}.
merge
(
options
)
@page
=
sanitize_page
(
page
)
@per_page
=
sanitize_per_page
(
per_page
)
end
def
total_count
@count
end
def
total_pages
(
total_count
.
to_f
/
@per_page
).
ceil
end
def
next_page
current_page
+
1
unless
last_page?
end
def
prev_page
return
nil
if
first_page?
current_page
-
1
unless
first_page?
end
def
current_page
@page
end
def
limit_value
@per_page
end
def
first_page?
current_page
==
1
end
def
last_page?
current_page
>=
total_pages
end
def
offset
(
current_page
-
1
)
*
limit_value
end
private
def
sanitize_per_page
(
per_page
)
return
@options
[
:default_per_page
]
unless
per_page
&&
per_page
>
0
per_page
>
@options
[
:max_per_page
]
?
@options
[
:max_per_page
]
:
per_page
end
def
sanitize_page
(
page
)
return
1
unless
page
&&
page
>
1
page
>
total_count
?
total_count
:
page
end
end
end
spec/lib/gitlab/pagination_delegate_spec.rb
0 → 100644
View file @
c1ed642f
require
'spec_helper'
describe
Gitlab
::
PaginationDelegate
,
lib:
true
do
context
'no data'
do
let
(
:delegate
)
{
described_class
.
new
(
page:
1
,
per_page:
10
,
count:
0
)
}
it
'shows the correct total count'
do
expect
(
delegate
.
total_count
).
to
eq
(
0
)
end
it
'shows the correct total pages'
do
expect
(
delegate
.
total_pages
).
to
eq
(
0
)
end
it
'shows the correct next page'
do
expect
(
delegate
.
next_page
).
to
be_nil
end
it
'shows the correct previous page'
do
expect
(
delegate
.
prev_page
).
to
be_nil
end
it
'shows the correct current page'
do
expect
(
delegate
.
current_page
).
to
eq
(
1
)
end
it
'shows the correct limit value'
do
expect
(
delegate
.
limit_value
).
to
eq
(
10
)
end
it
'shows the correct first page'
do
expect
(
delegate
.
first_page?
).
to
be
true
end
it
'shows the correct last page'
do
expect
(
delegate
.
last_page?
).
to
be
true
end
it
'shows the correct offset'
do
expect
(
delegate
.
offset
).
to
eq
(
0
)
end
end
context
'with data'
do
let
(
:delegate
)
{
described_class
.
new
(
page:
5
,
per_page:
100
,
count:
1000
)
}
it
'shows the correct total count'
do
expect
(
delegate
.
total_count
).
to
eq
(
1000
)
end
it
'shows the correct total pages'
do
expect
(
delegate
.
total_pages
).
to
eq
(
10
)
end
it
'shows the correct next page'
do
expect
(
delegate
.
next_page
).
to
eq
(
6
)
end
it
'shows the correct previous page'
do
expect
(
delegate
.
prev_page
).
to
eq
(
4
)
end
it
'shows the correct current page'
do
expect
(
delegate
.
current_page
).
to
eq
(
5
)
end
it
'shows the correct limit value'
do
expect
(
delegate
.
limit_value
).
to
eq
(
100
)
end
it
'shows the correct first page'
do
expect
(
delegate
.
first_page?
).
to
be
false
end
it
'shows the correct last page'
do
expect
(
delegate
.
last_page?
).
to
be
false
end
it
'shows the correct offset'
do
expect
(
delegate
.
offset
).
to
eq
(
400
)
end
end
context
'last page'
do
let
(
:delegate
)
{
described_class
.
new
(
page:
10
,
per_page:
100
,
count:
1000
)
}
it
'shows the correct total count'
do
expect
(
delegate
.
total_count
).
to
eq
(
1000
)
end
it
'shows the correct total pages'
do
expect
(
delegate
.
total_pages
).
to
eq
(
10
)
end
it
'shows the correct next page'
do
expect
(
delegate
.
next_page
).
to
be_nil
end
it
'shows the correct previous page'
do
expect
(
delegate
.
prev_page
).
to
eq
(
9
)
end
it
'shows the correct current page'
do
expect
(
delegate
.
current_page
).
to
eq
(
10
)
end
it
'shows the correct limit value'
do
expect
(
delegate
.
limit_value
).
to
eq
(
100
)
end
it
'shows the correct first page'
do
expect
(
delegate
.
first_page?
).
to
be
false
end
it
'shows the correct last page'
do
expect
(
delegate
.
last_page?
).
to
be
true
end
it
'shows the correct offset'
do
expect
(
delegate
.
offset
).
to
eq
(
900
)
end
end
context
'limits and defaults'
do
it
'has a maximum limit per page'
do
expect
(
described_class
.
new
(
page:
nil
,
per_page:
1000
,
count:
0
).
limit_value
).
to
eq
(
described_class
::
MAX_PER_PAGE
)
end
it
'has a default per page'
do
expect
(
described_class
.
new
(
page:
nil
,
per_page:
nil
,
count:
0
).
limit_value
).
to
eq
(
described_class
::
DEFAULT_PER_PAGE
)
end
it
'has a maximum page'
do
expect
(
described_class
.
new
(
page:
100
,
per_page:
10
,
count:
1
).
current_page
).
to
eq
(
1
)
end
end
end
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