fix(export): no crash on empty repetitions

This commit is contained in:
Paul Chavard 2022-06-17 11:33:48 +02:00
parent 8e73a4f037
commit bbc44fe339
3 changed files with 16 additions and 3 deletions

View file

@ -482,7 +482,7 @@ class Dossier < ApplicationRecord
end
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

View file

@ -102,11 +102,12 @@ class ProcedureExportService
.repetition
.filter_map do |type_de_champ_repetition|
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,
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) }
}
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])
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