Fix invalid characters in repetition champs sheet_name for xls export

This commit is contained in:
Paul Chavard 2019-08-27 17:19:22 +02:00
parent b90c27a8f0
commit 15148a4fe1
2 changed files with 15 additions and 1 deletions

View file

@ -70,6 +70,10 @@ class ProcedureExportV2Service
row_style: { background_color: nil, color: "000000", font_size: 12 } row_style: { background_color: nil, color: "000000", font_size: 12 }
} }
def sanitize_sheet_name(name)
ActiveStorage::Filename.new(name.to_s).sanitized.truncate(30)
end
def options_for(table) def options_for(table)
case table case table
when :dossiers when :dossiers
@ -80,7 +84,7 @@ class ProcedureExportV2Service
{ instances: avis.to_a, sheet_name: 'Avis' }.merge(DEFAULT_STYLES) { instances: avis.to_a, sheet_name: 'Avis' }.merge(DEFAULT_STYLES)
when Array when Array
# We have to truncate the label here as spreadsheets have a (30 char) limit on length. # We have to truncate the label here as spreadsheets have a (30 char) limit on length.
{ instances: table.last, sheet_name: table.first.to_s.truncate(30) }.merge(DEFAULT_STYLES) { instances: table.last, sheet_name: sanitize_sheet_name(table.first) }.merge(DEFAULT_STYLES)
end end
end end
end end

View file

@ -184,6 +184,16 @@ describe ProcedureExportV2Service do
it 'should have data' do it 'should have data' do
expect(repetition_sheet.data.size).to eq(4) expect(repetition_sheet.data.size).to eq(4)
end end
context 'with invalid characters' do
before do
champ_repetition.type_de_champ.update(libelle: 'A / B \ C')
end
it 'should have valid sheet name' do
expect(subject.sheets.map(&:name)).to eq(['Dossiers', 'Etablissements', 'Avis', "A - B - C"])
end
end
end end
end end
end end