From f0e7b5e7a321383aa847790d36b1328a70c2fe2c Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 14 Sep 2018 15:05:46 +0200 Subject: Cleaned up CI runner administration code In https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/19625 some changes were introduced that do not meet our abstraction reuse rules. This commit cleans up some of these changes so the requirements are met. Most notably, sorting of the runners in Admin::RunnersFinder has been delegated to Ci::Runner.order_by, similar to how we order data in models that include the Sortable module. If we need more sort orders in the future we can include Sortable and have Ci::Runner.order_by call `super` to delegate to Sortable.order_by. --- app/controllers/admin/runners_controller.rb | 2 -- app/finders/admin/runners_finder.rb | 3 +-- app/models/ci/runner.rb | 11 +++++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/controllers/admin/runners_controller.rb b/app/controllers/admin/runners_controller.rb index 2ac14ecd79c..911603bac17 100644 --- a/app/controllers/admin/runners_controller.rb +++ b/app/controllers/admin/runners_controller.rb @@ -1,14 +1,12 @@ class Admin::RunnersController < Admin::ApplicationController before_action :runner, except: :index - # rubocop: disable CodeReuse/ActiveRecord def index finder = Admin::RunnersFinder.new(params: params) @runners = finder.execute @active_runners_count = Ci::Runner.online.count @sort = finder.sort_key end - # rubocop: enable CodeReuse/ActiveRecord def show assign_builds_and_projects diff --git a/app/finders/admin/runners_finder.rb b/app/finders/admin/runners_finder.rb index 7adee486e33..3c2d7ee7d76 100644 --- a/app/finders/admin/runners_finder.rb +++ b/app/finders/admin/runners_finder.rb @@ -43,8 +43,7 @@ class Admin::RunnersFinder < UnionFinder end def sort! - sort = sort_key == 'contacted_asc' ? { contacted_at: :asc } : { created_at: :desc } - @runners = @runners.order(sort) + @runners = @runners.order_by(sort_key) end def paginate! diff --git a/app/models/ci/runner.rb b/app/models/ci/runner.rb index 45fd15a6211..eabb41c29d7 100644 --- a/app/models/ci/runner.rb +++ b/app/models/ci/runner.rb @@ -72,6 +72,9 @@ module Ci .project_type end + scope :order_contacted_at_asc, -> { order(contacted_at: :asc) } + scope :order_created_at_desc, -> { order(created_at: :desc) } + validate :tag_constraints validates :access_level, presence: true validates :runner_type, presence: true @@ -124,6 +127,14 @@ module Ci ONLINE_CONTACT_TIMEOUT.ago end + def self.order_by(order) + if order == 'contacted_asc' + order_contacted_at_asc + else + order_created_at_desc + end + end + def set_default_values self.token = SecureRandom.hex(15) if self.token.blank? end -- cgit v1.2.3