Skip to content

Commit

Permalink
refactor(pj_service): do not query for pj_index
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSim committed May 1, 2024
1 parent cadded2 commit 65e5a32
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions app/services/pieces_justificatives_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,16 @@ def pjs_for_champs(dossiers)
.includes(:blob)
.where(record_type: "Champ", record_id: champ_id_dossier_id.keys)
.filter { |a| safe_attachment(a) }
.map do |a, _i|
dossier_id = champ_id_dossier_id[a.record_id]
pj_index = Champ.find(a.record_id).piece_justificative_file.blobs.map(&:id).index(a.blob_id)
if @export_template
@export_template.attachment_and_path(Dossier.find(dossier_id), a, index: pj_index, row_index: a.record.row_index)
else
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a)
.group_by(&:record_id)
.flat_map do |champ_id, attachments|
dossier_id = champ_id_dossier_id[champ_id]

attachments.map.with_index do |attachment, index|
if @export_template
@export_template.attachment_and_path(Dossier.find(dossier_id), attachment, index: index, row_index: attachment.record.row_index)
else
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, attachment)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/services/pieces_justificatives_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def attachments(champ) = champ.piece_justificative_file.attachments
subject
end

expect(count).to eq(18)
expect(count).to eq(12)
end
end
end
Expand Down

0 comments on commit 65e5a32

Please sign in to comment.