diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 10:08:36 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-10-21 10:08:36 +0300 |
commit | 48aff82709769b098321c738f3444b9bdaa694c6 (patch) | |
tree | e00c7c43e2d9b603a5a6af576b1685e400410dee /app/models/preloaders | |
parent | 879f5329ee916a948223f8f43d77fba4da6cd028 (diff) |
Add latest changes from gitlab-org/gitlab@13-5-stable-eev13.5.0-rc42
Diffstat (limited to 'app/models/preloaders')
-rw-r--r-- | app/models/preloaders/merge_request_diff_preloader.rb | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/app/models/preloaders/merge_request_diff_preloader.rb b/app/models/preloaders/merge_request_diff_preloader.rb new file mode 100644 index 00000000000..ee9995c497d --- /dev/null +++ b/app/models/preloaders/merge_request_diff_preloader.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module Preloaders + # 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 + class MergeRequestDiffPreloader + def initialize(merge_requests) + @merge_requests = merge_requests + end + + 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 + end +end |