refactor: memoize flat_tags

This commit is contained in:
simon lehericey 2024-05-14 17:19:52 +02:00
parent e0c867f222
commit d60e7906e0
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
2 changed files with 9 additions and 7 deletions

View file

@ -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

View file

@ -40,7 +40,7 @@ describe ProcedureExportService do
subject
end
expect(sql_count).to eq(474)
expect(sql_count).to eq(296)
dossier = dossiers.first