Class: Preloaders::MergeRequestDiffPreloader

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

Overview

This class preloads the `merge_request_diff` association for the given merge request models.

Usage:

merge_requests = MergeRequest.where(...)
Preloaders::MergeRequestDiffPreloader.new(merge_requests).preload_all
merge_requests.first.merge_request_diff # won't fire any query

Instance Method Summary collapse

Constructor Details

#initialize(merge_requests) ⇒ MergeRequestDiffPreloader

Returns a new instance of MergeRequestDiffPreloader.


11
12
13
# File 'app/models/preloaders/merge_request_diff_preloader.rb', line 11

def initialize(merge_requests)
  @merge_requests = merge_requests
end

Instance Method Details

#preload_allObject


15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/preloaders/merge_request_diff_preloader.rb', line 15

def preload_all
  merge_request_diffs = MergeRequestDiff.latest_diff_for_merge_requests(@merge_requests)
  cache = merge_request_diffs.index_by { |diff| diff.merge_request_id }

  @merge_requests.each do |merge_request|
    merge_request_diff = cache[merge_request.id]

    merge_request.association(:merge_request_diff).target = merge_request_diff
    merge_request.association(:merge_request_diff).loaded!
  end
end