Welcome to mirror list, hosted at ThFree Co, Russian Federation.

project_root_ancestor_preloader.rb « preloaders « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9baa469e60dc4ddef9f0bbbe964d6d872a6771b8 (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
# frozen_string_literal: true

module Preloaders
  class ProjectRootAncestorPreloader
    def initialize(projects, namespace_sti_name = :namespace, root_ancestor_preloads = [])
      @projects = projects
      @namespace_sti_name = namespace_sti_name
      @root_ancestor_preloads = root_ancestor_preloads
    end

    def execute
      return unless @projects.is_a?(ActiveRecord::Relation)

      root_query = Namespace.joins("INNER JOIN (#{join_sql}) as root_query ON root_query.root_id = namespaces.id")
                        .select('namespaces.*, root_query.project_id as source_id')

      root_query = root_query.preload(*@root_ancestor_preloads) if @root_ancestor_preloads.any?

      root_ancestors_by_id = root_query.group_by(&:source_id)

      ActiveRecord::Associations::Preloader.new(records: @projects, associations: :namespace).call
      @projects.each do |project|
        root_ancestor = root_ancestors_by_id[project.id]&.first
        project.namespace.root_ancestor = root_ancestor if root_ancestor.present?
      end
    end

    private

    def join_sql
      @projects
        .joins(@namespace_sti_name)
        .select('projects.id as project_id, namespaces.traversal_ids[1] as root_id')
        .to_sql
    end
  end
end