Class: Preloaders::LabelsPreloader

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(labels, user, project = nil) ⇒ LabelsPreloader

Returns a new instance of LabelsPreloader.


15
16
17
18
19
# File 'app/models/preloaders/labels_preloader.rb', line 15

def initialize(labels, user, project = nil)
  @labels = labels
  @user = user
  @project = project
end

Instance Attribute Details

#labelsObject (readonly)

Returns the value of attribute labels.


13
14
15
# File 'app/models/preloaders/labels_preloader.rb', line 13

def labels
  @labels
end

#projectObject (readonly)

Returns the value of attribute project.


13
14
15
# File 'app/models/preloaders/labels_preloader.rb', line 13

def project
  @project
end

#userObject (readonly)

Returns the value of attribute user.


13
14
15
# File 'app/models/preloaders/labels_preloader.rb', line 13

def user
  @user
end

Instance Method Details

#preload_allObject


21
22
23
24
25
26
27
28
29
30
# File 'app/models/preloaders/labels_preloader.rb', line 21

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