diff --git a/app/models/concerns/tags_substitution_concern.rb b/app/models/concerns/tags_substitution_concern.rb index c9b7262af..373ad3018 100644 --- a/app/models/concerns/tags_substitution_concern.rb +++ b/app/models/concerns/tags_substitution_concern.rb @@ -275,7 +275,7 @@ module TagsSubstitutionConcern used_tags_and_libelle_for(text).map { _1.first.nil? ? _1.second : _1.first } end - def tags_substitutions(tags_and_libelles, dossier, escape: true) + def tags_substitutions(tags_and_libelles, dossier, escape: true, memoize: false) # NOTE: # - tags_and_libelles est un simple Set de couples (tag_id, libelle) (pas la même structure que dans replace_tags) # - dans `replace_tags`, on fait référence à des tags avec ou sans id, mais pas ici, @@ -283,16 +283,20 @@ module TagsSubstitutionConcern @escape_unsafe_tags = escape - if @flat_tags.nil? - @flat_tags = available_tags(dossier) + flat_tags = if memoize && @flat_tags.present? + @flat_tags + else + available_tags(dossier) .flatten .then { tags_for_dossier_state(_1) } .index_by { _1[:id] } end + @flat_tags = flat_tags if memoize + 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/app/models/export_template.rb b/app/models/export_template.rb index 4d9bc5f39..dfbb57f3f 100644 --- a/app/models/export_template.rb +++ b/app/models/export_template.rb @@ -67,7 +67,7 @@ class ExportTemplate < ApplicationRecord def render_attributes_for(content_for, dossier, attachment = nil) tiptap = TiptapService.new used_tags = tiptap.used_tags_and_libelle_for(content_for.deep_symbolize_keys) - substitutions = tags_substitutions(used_tags, dossier, escape: false) + substitutions = tags_substitutions(used_tags, dossier, escape: false, memoize: true) substitutions['original-filename'] = attachment.filename.base if attachment tiptap.to_path(content_for.deep_symbolize_keys, substitutions) end