Skip to content

Commit

Permalink
perf(pj service): compute row_id without extraneous requests
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSim committed May 18, 2024
1 parent bd72575 commit d01f875
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
6 changes: 0 additions & 6 deletions app/models/champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ def stable_id_with_row
[row_id, stable_id].compact
end

def row_index
return nil if parent_id.nil?

Champ.where(parent_id:).pluck(:row_id).sort.index(row_id)
end

# used for the `required` html attribute
# check visibility to avoid hidden required input
# which prevent the form from being sent.
Expand Down
10 changes: 9 additions & 1 deletion app/services/pieces_justificatives_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,18 @@ def pjs_for_champs(dossiers)
champs = champs.reject(&:private?)
end

champs_id_row_index = champs.select { _1.row_id.present? }.group_by(&:dossier_id).values.each_with_object({}) do |champs_for_dossier, hash|
champs_for_dossier.group_by(&:stable_id).values.each do |champs_for_stable_id|
champs_for_stable_id.sort_by(&:row_id).each.with_index { |c, index| hash[c.id] = index }
end
end

champs.flat_map do |champ|
champ.piece_justificative_file_attachments.map.with_index do |attachment, index|
row_index = champs_id_row_index[champ.id]

if @export_template
@export_template.attachment_and_path(champ.dossier, attachment, index:, row_index: champ.row_index, champ:)
@export_template.attachment_and_path(champ.dossier, attachment, index:, row_index:, champ:)
else
ActiveStorage::DownloadableFile.pj_and_path(champ.dossier_id, attachment)
end
Expand Down
4 changes: 2 additions & 2 deletions 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(89)
expect(sql_count).to eq(58)

dossier = dossiers.first

Expand All @@ -58,7 +58,7 @@ def attachments(champ) = champ.piece_justificative_file.attachments
"export/dossier-#{dossier.id}/libelle-du-champ-2-#{dossier.id}-1-2.png"
]

expect(files.size).to eq(10 * 6 + 1)
expect(files.size).to eq(dossiers.count * 6 + 1)
expect(structure - files.map(&:filename)).to be_empty
end
FileUtils.remove_entry_secure('tmp.zip')
Expand Down

0 comments on commit d01f875

Please sign in to comment.