From 9de2c7c85acee628d4db2edbb5ee5db0e6ffe44f Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Thu, 30 Apr 2020 16:03:05 +0200 Subject: [PATCH] improve csv generation for bilans bdf --- app/models/etablissement.rb | 6 ++++-- spec/models/etablissement_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/models/etablissement.rb b/app/models/etablissement.rb index a5b2b62f7..50585c6a2 100644 --- a/app/models/etablissement.rb +++ b/app/models/etablissement.rb @@ -139,8 +139,10 @@ class Etablissement < ApplicationRecord end def entreprise_bilans_bdf_to_csv - headers = entreprise_bilans_bdf[0].keys - data = entreprise_bilans_bdf.map(&:values) + headers = entreprise_bilans_bdf.flat_map(&:keys).uniq + data = entreprise_bilans_bdf.map do |bilan| + headers.map { |h| bilan[h] } + end SpreadsheetArchitect.to_csv(headers: headers, data: data) end diff --git a/spec/models/etablissement_spec.rb b/spec/models/etablissement_spec.rb index 31ae4acef..c329a519e 100644 --- a/spec/models/etablissement_spec.rb +++ b/spec/models/etablissement_spec.rb @@ -35,4 +35,28 @@ describe Etablissement do end end end + + describe '.entreprise_bilans_bdf_to_csv' do + let(:etablissement) { build(:etablissement, entreprise_bilans_bdf: bilans) } + let(:bilans) do + [ + { + "total_passif": "1200", + "chiffres_affaires_ht": "40000" + }, + { + "total_passif": "0", + "evolution_total_dettes_stables": "30" + } + ] + end + + subject { etablissement.entreprise_bilans_bdf_to_csv.split("\n") } + + it "build a csv with all keys" do + expect(subject[0].split(',').sort).to eq(["total_passif", "chiffres_affaires_ht", "evolution_total_dettes_stables"].sort) + expect(subject[1].split(',')).to eq(["1200", "40000"]) + expect(subject[2].split(',')).to eq(["0", "", "30"]) + end + end end