diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-07 18:09:18 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-07 18:09:18 +0300 |
commit | 40b78ea2b6f5f0ef730c2cd811911be3449562e6 (patch) | |
tree | 4298f7f47855273b4e5f0c6791bed283b0c51003 /app/models/preloaders | |
parent | 6d5f18a3c1007e810b141b9ba671fd85a3e83e7f (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/preloaders')
-rw-r--r-- | app/models/preloaders/labels_preloader.rb | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/app/models/preloaders/labels_preloader.rb b/app/models/preloaders/labels_preloader.rb new file mode 100644 index 00000000000..aad51748e65 --- /dev/null +++ b/app/models/preloaders/labels_preloader.rb @@ -0,0 +1,32 @@ +# 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, @user, @project = labels, user, project + end + + def preload_all + preloader = ActiveRecord::Associations::Preloader.new + + preloader.preload(labels.select {|l| l.is_a? ProjectLabel }, { project: [:project_feature, namespace: :route] }) + preloader.preload(labels.select {|l| l.is_a? GroupLabel }, { group: :route }) + labels.each do |label| + label.lazy_subscription(user) + label.lazy_subscription(user, project) if project.present? + end + end + end +end + +Preloaders::LabelsPreloader.prepend_if_ee('EE::Preloaders::LabelsPreloader') |