summaryrefslogtreecommitdiff
path: root/app/finders/admin/runners_finder.rb
blob: 3c2d7ee7d76d37f7eb4767b02f6703707ff62b9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true

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

  def sort_key
    if @params[:sort] == 'contacted_asc'
      'contacted_asc'
    else
      'created_date'
    end
  end

  private

  def search!
    @runners =
      if @params[:search].present?
        Ci::Runner.search(@params[:search])
      else
        Ci::Runner.all
      end
  end

  def filter_by_status!
    status = @params[:status_status]
    if status.present? && Ci::Runner::AVAILABLE_STATUSES.include?(status)
      @runners = @runners.public_send(status) # rubocop:disable GitlabSecurity/PublicSend
    end
  end

  def sort!
    @runners = @runners.order_by(sort_key)
  end

  def paginate!
    @runners = @runners.page(@params[:page]).per(NUMBER_OF_RUNNERS_PER_PAGE)
  end
end