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
0
Merge Requests
0
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
Léo-Paul Géneau
gitlab-ce
Commits
0a0350be
Commit
0a0350be
authored
Jun 08, 2018
by
Alexis Reigel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
extract filter/sort/paging logic to finder class
parent
16d12491
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
106 additions
and
5 deletions
+106
-5
app/controllers/admin/runners_controller.rb
app/controllers/admin/runners_controller.rb
+1
-5
app/finders/admin/runners_finder.rb
app/finders/admin/runners_finder.rb
+42
-0
spec/finders/admin/runners_finder_spec.rb
spec/finders/admin/runners_finder_spec.rb
+63
-0
No files found.
app/controllers/admin/runners_controller.rb
View file @
0a0350be
...
...
@@ -2,11 +2,7 @@ class Admin::RunnersController < Admin::ApplicationController
before_action
:runner
,
except: :index
def
index
sort
=
params
[
:sort
]
==
'contacted_asc'
?
{
contacted_at: :asc
}
:
{
id: :desc
}
@runners
=
Ci
::
Runner
.
order
(
sort
)
@runners
=
@runners
.
search
(
params
[
:search
])
if
params
[
:search
].
present?
@runners
=
@runners
.
public_send
(
params
[
:status
])
if
params
[
:status
].
present?
&&
Ci
::
Runner
::
AVAILABLE_STATUSES
.
include?
(
params
[
:status
])
@runners
=
@runners
.
page
(
params
[
:page
]).
per
(
30
)
@runners
=
Admin
::
RunnersFinder
.
new
(
params:
params
).
execute
@active_runners_cnt
=
Ci
::
Runner
.
online
.
count
end
...
...
app/finders/admin/runners_finder.rb
0 → 100644
View file @
0a0350be
class
Admin::RunnersFinder
<
UnionFinder
NUMBER_OF_RUNNERS_PER_PAGE
=
30
def
initialize
(
params
:)
@params
=
params
end
def
execute
search!
filter_by_status!
sort!
paginate!
@runners
end
private
def
search!
@runners
=
if
@params
[
:search
].
present?
Ci
::
Runner
.
search
(
@params
[
:search
])
else
Ci
::
Runner
.
all
end
end
def
filter_by_status!
if
@params
[
:status
].
present?
&&
Ci
::
Runner
::
AVAILABLE_STATUSES
.
include?
(
@params
[
:status
])
@runners
=
@runners
.
public_send
(
@params
[
:status
])
# rubocop:disable GitlabSecurity/PublicSend
end
end
def
sort!
sort
=
@params
[
:sort
]
==
'contacted_asc'
?
{
contacted_at: :asc
}
:
{
id: :desc
}
@runners
=
@runners
.
order
(
sort
)
end
def
paginate!
@runners
=
@runners
.
page
(
@params
[
:page
]).
per
(
NUMBER_OF_RUNNERS_PER_PAGE
)
end
end
spec/finders/admin/runners_finder_spec.rb
0 → 100644
View file @
0a0350be
require
'spec_helper'
describe
Admin
::
RunnersFinder
do
describe
'#execute'
do
context
'with empty params'
do
it
'returns all runners'
do
runner1
=
create
:ci_runner
,
active:
true
runner2
=
create
:ci_runner
,
active:
false
expect
(
described_class
.
new
(
params:
{}).
execute
).
to
match_array
[
runner1
,
runner2
]
end
end
context
'filter by search term'
do
it
'calls Ci::Runner.search'
do
expect
(
Ci
::
Runner
).
to
receive
(
:search
).
with
(
'term'
).
and_call_original
described_class
.
new
(
params:
{
search:
'term'
}).
execute
end
end
context
'filter by status'
do
it
'calls the corresponding scope on Ci::Runner'
do
expect
(
Ci
::
Runner
).
to
receive
(
:paused
).
and_call_original
described_class
.
new
(
params:
{
status:
'paused'
}).
execute
end
end
context
'sort'
do
context
'without sort param'
do
it
'sorts by id'
do
runner1
=
create
:ci_runner
runner2
=
create
:ci_runner
runner3
=
create
:ci_runner
expect
(
described_class
.
new
(
params:
{}).
execute
).
to
eq
[
runner3
,
runner2
,
runner1
]
end
end
context
'with sort param'
do
it
'sorts by specified attribute'
do
runner1
=
create
:ci_runner
,
contacted_at:
1
.
minute
.
ago
runner2
=
create
:ci_runner
,
contacted_at:
3
.
minutes
.
ago
runner3
=
create
:ci_runner
,
contacted_at:
2
.
minutes
.
ago
expect
(
described_class
.
new
(
params:
{
sort:
'contacted_asc'
}).
execute
).
to
eq
[
runner2
,
runner3
,
runner1
]
end
end
end
context
'paginate'
do
it
'returns the runners for the specified page'
do
stub_const
(
'Admin::RunnersFinder::NUMBER_OF_RUNNERS_PER_PAGE'
,
1
)
runner1
=
create
:ci_runner
runner2
=
create
:ci_runner
expect
(
described_class
.
new
(
params:
{
page:
1
}).
execute
).
to
eq
[
runner2
]
expect
(
described_class
.
new
(
params:
{
page:
2
}).
execute
).
to
eq
[
runner1
]
end
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