Class: Preloaders::ProjectRootAncestorPreloader

Inherits:
Object
  • Object
show all
Defined in:
app/models/preloaders/project_root_ancestor_preloader.rb

Instance Method Summary collapse

Constructor Details

#initialize(projects, namespace_sti_name = :namespace, root_ancestor_preloads = []) ⇒ ProjectRootAncestorPreloader

Returns a new instance of ProjectRootAncestorPreloader.



5
6
7
8
9
# File 'app/models/preloaders/project_root_ancestor_preloader.rb', line 5

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

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/preloaders/project_root_ancestor_preloader.rb', line 11

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

  root_query = Namespace.joins("INNER JOIN (#{join_sql}) as root_query ON root_query.root_id = namespaces.id")
                    .select('namespaces.*, root_query.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