From d60e7906e040ff3e49a5f04957cf4d2f312b92b8 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 14 May 2024 17:19:52 +0200 Subject: [PATCH] refactor: memoize flat_tags --- app/models/concerns/tags_substitution_concern.rb | 14 ++++++++------ spec/services/procedure_export_service_zip_spec.rb | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/models/concerns/tags_substitution_concern.rb b/app/models/concerns/tags_substitution_concern.rb index bb99cbe5c..e68dae815 100644 --- a/app/models/concerns/tags_substitution_concern.rb +++ b/app/models/concerns/tags_substitution_concern.rb @@ -283,17 +283,19 @@ module TagsSubstitutionConcern @escape_unsafe_tags = escape - flat_tags = available_tags(dossier).each_with_object({}) do |tags, result| - valid_tags = tags_for_dossier_state(tags) + if @flat_tags.nil? + @flat_tags = available_tags(dossier).each_with_object({}) do |tags, result| + valid_tags = tags_for_dossier_state(tags) - valid_tags.each do |tag| - result[tag[:id]] = tag + valid_tags.each do |tag| + result[tag[:id]] = tag + end end end tags_and_libelles.each_with_object({}) do |(tag_id, libelle), substitutions| - substitutions[tag_id] = if flat_tags[tag_id].present? - replace_tag(flat_tags[tag_id], dossier) + substitutions[tag_id] = if @flat_tags[tag_id].present? + replace_tag(@flat_tags[tag_id], dossier) else # champ not in dossier, for example during preview on draft revision libelle end diff --git a/spec/services/procedure_export_service_zip_spec.rb b/spec/services/procedure_export_service_zip_spec.rb index 70ca5fc75..85237c724 100644 --- a/spec/services/procedure_export_service_zip_spec.rb +++ b/spec/services/procedure_export_service_zip_spec.rb @@ -40,7 +40,7 @@ describe ProcedureExportService do subject end - expect(sql_count).to eq(474) + expect(sql_count).to eq(296) dossier = dossiers.first