summaryrefslogtreecommitdiff
path: root/app/models/preloaders/commit_status_preloader.rb
blob: 79c2549e37178d517961e4226c68f4c71b301bb7 (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
# frozen_string_literal: true

module Preloaders
  class CommitStatusPreloader
    CLASSES = [::Ci::Build, ::Ci::Bridge, ::GenericCommitStatus].freeze

    def initialize(statuses)
      @statuses = statuses
    end

    def execute(relations)
      CLASSES.each do |klass|
        ActiveRecord::Associations::Preloader.new(
          records: objects(klass),
          associations: associations(klass, relations)
        ).call
      end
    end

    private

    def objects(klass)
      @statuses.select { |job| job.is_a?(klass) }
    end

    def associations(klass, relations)
      klass.reflections.keys.map(&:to_sym) & relations.map(&:to_sym)
    end
  end
end