Merge pull request #7472 from tchak/fix-export

fix(export): no crash on empty repetitions
This commit is contained in:
Paul Chavard 2022-06-17 11:43:06 +02:00 committed by GitHub
commit 988397ac11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 3 deletions

View file

@ -482,7 +482,7 @@ class Dossier < ApplicationRecord
end end
dossiers.each do |dossier| dossiers.each do |dossier|
load_dossier(dossier, champs_by_dossier[dossier.id], champs_by_dossier_by_parent[dossier.id]) load_dossier(dossier, champs_by_dossier[dossier.id], champs_by_dossier_by_parent[dossier.id] || {})
end end
end end

View file

@ -102,11 +102,12 @@ class ProcedureExportService
.repetition .repetition
.filter_map do |type_de_champ_repetition| .filter_map do |type_de_champ_repetition|
types_de_champ = procedure.types_de_champ_for_procedure_presentation(type_de_champ_repetition).to_a types_de_champ = procedure.types_de_champ_for_procedure_presentation(type_de_champ_repetition).to_a
rows = champs_by_stable_id.fetch(type_de_champ_repetition.stable_id, []).flat_map(&:rows_for_export)
if types_de_champ.present? if types_de_champ.present? && rows.present?
{ {
sheet_name: type_de_champ_repetition.libelle_for_export, sheet_name: type_de_champ_repetition.libelle_for_export,
instances: champs_by_stable_id.fetch(type_de_champ_repetition.stable_id, []).flat_map(&:rows_for_export), instances: rows,
spreadsheet_columns: Proc.new { |instance| instance.spreadsheet_columns(types_de_champ) } spreadsheet_columns: Proc.new { |instance| instance.spreadsheet_columns(types_de_champ) }
} }
end end

View file

@ -405,6 +405,18 @@ describe ProcedureExportService do
expect(subject.sheets.map(&:name)).to eq(['Dossiers', 'Etablissements', 'Avis', another_champ_repetition.libelle_for_export, champ_repetition.libelle_for_export]) expect(subject.sheets.map(&:name)).to eq(['Dossiers', 'Etablissements', 'Avis', another_champ_repetition.libelle_for_export, champ_repetition.libelle_for_export])
end end
end end
context 'with empty repetition' do
before do
dossiers.flat_map { |dossier| dossier.champs.filter(&:repetition?) }.each do |champ|
champ.champs.destroy_all
end
end
it 'should not have data' do
expect(repetition_sheet).to be_nil
end
end
end end
end end