From 157c3778dfd11450a596af809c5d699397a66780 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 10 Oct 2022 12:31:22 +0200 Subject: [PATCH] refactor(dossier_preloader): add option to laod pj_template --- app/models/dossier_preloader.rb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/app/models/dossier_preloader.rb b/app/models/dossier_preloader.rb index 8a566c2cf..8e1d0f113 100644 --- a/app/models/dossier_preloader.rb +++ b/app/models/dossier_preloader.rb @@ -11,14 +11,14 @@ class DossierPreloader dossiers end - def all + def all(pj_template: false) dossiers = @dossiers.to_a - load_dossiers(dossiers) + load_dossiers(dossiers, pj_template:) dossiers end def self.load_one(dossier) - DossierPreloader.new([dossier]).all.first + DossierPreloader.new([dossier]).all(pj_template: true).first end private @@ -34,9 +34,17 @@ class DossierPreloader end end - def load_dossiers(dossiers) + def load_dossiers(dossiers, pj_template: false) + to_include = [piece_justificative_file_attachment: :blob] + + if pj_template + to_include << { type_de_champ: { piece_justificative_template_attachment: :blob } } + else + to_include << :type_de_champ + end + all_champs = Champ - .includes(type_de_champ: { piece_justificative_template_attachment: :blob }, piece_justificative_file_attachment: :blob) + .includes(to_include) .where(dossier_id: dossiers) .to_a