diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-20 23:50:22 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-20 23:50:22 +0000 |
commit | 9dc93a4519d9d5d7be48ff274127136236a3adb3 (patch) | |
tree | 70467ae3692a0e35e5ea56bcb803eb512a10bedb /app/models/preloaders/labels_preloader.rb | |
parent | 4b0f34b6d759d6299322b3a54453e930c6121ff0 (diff) | |
download | gitlab-ce-9dc93a4519d9d5d7be48ff274127136236a3adb3.tar.gz |
Add latest changes from gitlab-org/gitlab@13-11-stable-eev13.11.0-rc43
Diffstat (limited to 'app/models/preloaders/labels_preloader.rb')
-rw-r--r-- | app/models/preloaders/labels_preloader.rb | 34 |
1 files changed, 34 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..427f2869aac --- /dev/null +++ b/app/models/preloaders/labels_preloader.rb @@ -0,0 +1,34 @@ +# 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 + 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') |