File: commit_status_preloader.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (47 lines) | stat: -rw-r--r-- 1,144 bytes parent folder | download
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
# 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 = klass.reflections.keys.map(&:to_sym).to_set

      result = []
      relations.each do |entry|
        if entry.respond_to?(:to_sym)
          result << entry.to_sym if klass_reflections.include?(entry.to_sym)
        elsif entry.is_a?(Hash)
          entry = entry.select do |key, _value|
            klass_reflections.include?(key.to_sym)
          end

          result << entry if entry.present?
        else
          raise ArgumentError, "Invalid relation: #{entry.inspect}"
        end
      end

      result
    end
  end
end