preload all tdc once for all

This commit is contained in:
simon lehericey 2022-04-07 15:26:24 +02:00 committed by Paul Chavard
parent 4dbd44d72a
commit ca8dc34196
2 changed files with 17 additions and 7 deletions

View file

@ -113,6 +113,11 @@ class PiecesJustificativesService
pdfs = [] pdfs = []
procedure = dossiers.first.procedure procedure = dossiers.first.procedure
tdc_by_id = TypeDeChamp
.joins(:revisions)
.where(revisions: { id: procedure.revisions })
.to_a
.group_by(&:id)
dossiers dossiers
.includes(:champs, :champs_private, :commentaires, :individual, .includes(:champs, :champs_private, :commentaires, :individual,
@ -124,7 +129,8 @@ class PiecesJustificativesService
assigns: { assigns: {
include_infos_administration: true, include_infos_administration: true,
dossier: dossier, dossier: dossier,
procedure: procedure procedure: procedure,
tdc_by_id: tdc_by_id
}) })
a = FakeAttachment.new( a = FakeAttachment.new(

View file

@ -130,28 +130,30 @@ def add_identite_etablissement(pdf, etablissement)
end end
def add_single_champ(pdf, champ) def add_single_champ(pdf, champ)
tdc = @tdc_by_id[champ.type_de_champ_id].first
case champ.type case champ.type
when 'Champs::PieceJustificativeChamp', 'Champs::TitreIdentiteChamp' when 'Champs::PieceJustificativeChamp', 'Champs::TitreIdentiteChamp'
return return
when 'Champs::HeaderSectionChamp' when 'Champs::HeaderSectionChamp'
add_section_title(pdf, champ.libelle) add_section_title(pdf, tdc.libelle)
when 'Champs::ExplicationChamp' when 'Champs::ExplicationChamp'
format_in_2_lines(pdf, champ.libelle, champ.description) format_in_2_lines(pdf, tdc.libelle, tdc.description)
when 'Champs::CarteChamp' when 'Champs::CarteChamp'
format_in_2_lines(pdf, champ.libelle, champ.to_feature_collection.to_json) format_in_2_lines(pdf, tdc.libelle, champ.to_feature_collection.to_json)
when 'Champs::SiretChamp' when 'Champs::SiretChamp'
pdf.font 'marianne', style: :bold do pdf.font 'marianne', style: :bold do
pdf.text champ.libelle pdf.text tdc.libelle
end end
if champ.etablissement.present? if champ.etablissement.present?
add_identite_etablissement(pdf, champ.etablissement) add_identite_etablissement(pdf, champ.etablissement)
end end
when 'Champs::NumberChamp' when 'Champs::NumberChamp'
value = champ.to_s.empty? ? 'Non communiqué' : number_with_delimiter(champ.to_s) value = champ.to_s.empty? ? 'Non communiqué' : number_with_delimiter(champ.to_s)
format_in_2_lines(pdf, champ.libelle, value) format_in_2_lines(pdf, tdc.libelle, value)
else else
value = champ.to_s.empty? ? 'Non communiqué' : champ.to_s value = champ.to_s.empty? ? 'Non communiqué' : champ.to_s
format_in_2_lines(pdf, champ.libelle, value) format_in_2_lines(pdf, tdc.libelle, value)
end end
end end
@ -206,6 +208,8 @@ end
prawn_document(page_size: "A4") do |pdf| prawn_document(page_size: "A4") do |pdf|
@procedure ||= @dossier.procedure @procedure ||= @dossier.procedure
@tdc_by_id ||= @dossier.champs.map(&:type_de_champ).group_by(&:id)
pdf.font_families.update( 'marianne' => { pdf.font_families.update( 'marianne' => {
normal: Rails.root.join('lib/prawn/fonts/marianne/marianne-regular.ttf' ), normal: Rails.root.join('lib/prawn/fonts/marianne/marianne-regular.ttf' ),
bold: Rails.root.join('lib/prawn/fonts/marianne/marianne-bold.ttf' ), bold: Rails.root.join('lib/prawn/fonts/marianne/marianne-bold.ttf' ),