From f89f232d19a6c28a3504e964fbfd2b5eb344aa85 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 23 May 2018 10:22:28 +0200 Subject: [PATCH] Simplify pipelines preloader implementation --- .../projects/pipelines_controller.rb | 2 +- lib/gitlab/ci/pipeline/preloader.rb | 63 +++++++++---------- spec/lib/gitlab/ci/pipeline/preloader_spec.rb | 4 +- 3 files changed, 32 insertions(+), 37 deletions(-) diff --git a/app/controllers/projects/pipelines_controller.rb b/app/controllers/projects/pipelines_controller.rb index b4a8cdadd9b..2b758b960c2 100644 --- a/app/controllers/projects/pipelines_controller.rb +++ b/app/controllers/projects/pipelines_controller.rb @@ -24,7 +24,7 @@ class Projects::PipelinesController < Projects::ApplicationController @finished_count = limited_pipelines_count(project, 'finished') @pipelines_count = limited_pipelines_count(project) - Gitlab::Ci::Pipeline::Preloader.new(@pipelines).preload! + Gitlab::Ci::Pipeline::Preloader.preload!(@pipelines) respond_to do |format| format.html diff --git a/lib/gitlab/ci/pipeline/preloader.rb b/lib/gitlab/ci/pipeline/preloader.rb index 668c1b7189c..6db6554a606 100644 --- a/lib/gitlab/ci/pipeline/preloader.rb +++ b/lib/gitlab/ci/pipeline/preloader.rb @@ -6,46 +6,41 @@ module Gitlab # Class for preloading data associated with pipelines such as commit # authors. class Preloader - def initialize(pipelines) - @pipelines = pipelines - end - - def preload! - @pipelines.each do |pipeline| - Pipeline::Preloader::Instance.new(pipeline) - .preload_commits - .preload_pipeline_warnings - .preload_stages_warnings + def self.preload!(pipelines) + pipelines.each do |pipeline| + self.new(pipeline).tap do |preloader| + preloader.preload_commits + preloader.preload_pipeline_warnings + preloader.preload_stages_warnings + end end end - class Instance - def initialize(pipeline) - @pipeline = pipeline - end + def initialize(pipeline) + @pipeline = pipeline + end - def preload_commits - # This ensures that all the pipeline commits are eager loaded before we - # start using them. - # - # This also preloads the author of every commit. We're using "lazy_author" - # here since "author" immediately loads the data on the first call. - tap { @pipeline.commit.try(:lazy_author) } - end + def preload_commits + # This ensures that all the pipeline commits are eager loaded before we + # start using them. + # + # This also preloads the author of every commit. We're using "lazy_author" + # here since "author" immediately loads the data on the first call. + @pipeline.commit.try(:lazy_author) + end - def preload_pipeline_warnings - # This preloads the number of warnings for every pipeline, ensuring - # that Ci::Pipeline#has_warnings? doesn't execute any additional - # queries. - tap { @pipeline.number_of_warnings } - end + def preload_pipeline_warnings + # This preloads the number of warnings for every pipeline, ensuring + # that Ci::Pipeline#has_warnings? doesn't execute any additional + # queries. + @pipeline.number_of_warnings + end - def preload_stages_warnings - # This preloads the number of warnings for every stage, ensuring - # that Ci::Stage#has_warnings? doesn't execute any additional - # queries. - tap { @pipeline.stages.each { |stage| stage.number_of_warnings } } - end + def preload_stages_warnings + # This preloads the number of warnings for every stage, ensuring + # that Ci::Stage#has_warnings? doesn't execute any additional + # queries. + @pipeline.stages.each { |stage| stage.number_of_warnings } end end end diff --git a/spec/lib/gitlab/ci/pipeline/preloader_spec.rb b/spec/lib/gitlab/ci/pipeline/preloader_spec.rb index c8cfe2c696d..16d3631ec7c 100644 --- a/spec/lib/gitlab/ci/pipeline/preloader_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/preloader_spec.rb @@ -10,13 +10,13 @@ describe Gitlab::Ci::Pipeline::Preloader do double(:pipeline, commit: commit, stages: [stage]) end - describe '#preload!' do + describe '.preload!' do it 'preloads commit authors and number of warnings' do expect(commit).to receive(:lazy_author) expect(pipeline).to receive(:number_of_warnings) expect(stage).to receive(:number_of_warnings) - described_class.new([pipeline]).preload! + described_class.preload!([pipeline]) end end end