Skip to content

Commit

Permalink
refactor: memoize flat_tags
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSim committed May 14, 2024
1 parent 6157df9 commit d9458bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions app/models/concerns/tags_substitution_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,19 @@ def tags_substitutions(tags_and_libelles, dossier, escape: true)

@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
Expand Down
2 changes: 1 addition & 1 deletion spec/services/procedure_export_service_zip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def attachments(champ) = champ.piece_justificative_file.attachments
subject
end

expect(sql_count).to eq(474)
expect(sql_count).to eq(296)

dossier = dossiers.first

Expand Down

0 comments on commit d9458bf

Please sign in to comment.