debian-mirror-gitlab/app/models/preloaders/labels_preloader.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

52 lines
1.5 KiB
Ruby
Raw Normal View History

2021-04-29 21:17:54 +05:30
# frozen_string_literal: true
module Preloaders
# This class preloads the `project`, `group`, and subscription associations for the given
# labels, user, and project (if provided). A Label can be of type ProjectLabel or GroupLabel
# and the preloader supports both.
#
# Usage:
# labels = Label.where(...)
# Preloaders::LabelsPreloader.new(labels, current_user, @project).preload_all
# labels.first.project # won't fire any query
class LabelsPreloader
attr_reader :labels, :user, :project
def initialize(labels, user, project = nil)
@labels = labels
@user = user
@project = project
end
def preload_all
2023-05-27 22:25:52 +05:30
ActiveRecord::Associations::Preloader.new(
2023-06-20 00:43:36 +05:30
records: project_labels,
2023-05-27 22:25:52 +05:30
associations: { project: [:project_feature, namespace: :route] }
).call
ActiveRecord::Associations::Preloader.new(
2023-06-20 00:43:36 +05:30
records: group_labels,
2023-05-27 22:25:52 +05:30
associations: { group: :route }
).call
2022-11-25 23:54:43 +05:30
2023-06-20 00:43:36 +05:30
Preloaders::UserMaxAccessLevelInProjectsPreloader.new(project_labels.map(&:project), user).execute
2021-04-29 21:17:54 +05:30
labels.each do |label|
label.lazy_subscription(user)
label.lazy_subscription(user, project) if project.present?
end
end
2023-06-20 00:43:36 +05:30
private
def group_labels
@group_labels ||= labels.select { |l| l.is_a? GroupLabel }
end
def project_labels
@project_labels ||= labels.select { |l| l.is_a? ProjectLabel }
end
2021-04-29 21:17:54 +05:30
end
end
2021-06-08 01:23:25 +05:30
Preloaders::LabelsPreloader.prepend_mod_with('Preloaders::LabelsPreloader')