From c6e7db3622295e00f28937481fa9c55ee844cf7d Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 18 Oct 2022 11:00:44 +0200 Subject: [PATCH] fix(export): bug when combining revision and conditional --- app/models/dossier.rb | 19 +++++++++++++------ spec/models/dossier_spec.rb | 21 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index bc97fa9e6..477e10c15 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -1091,16 +1091,23 @@ class Dossier < ApplicationRecord # To do so, we build a virtual champ when there is no value so we can call for_export with all indexes def self.champs_for_export(champs, types_de_champ) types_de_champ.flat_map do |type_de_champ| - champ_or_new = champs.find { |champ| champ.stable_id == type_de_champ.stable_id } - champ_or_new ||= type_de_champ.champ.build + champ = champs.find { |champ| champ.stable_id == type_de_champ.stable_id } - champ_values = if champ_or_new.visible? - champ_or_new.for_export || [nil] + exported_values = if champ.nil? || !champ.visible? + # some champs export multiple columns + # ex: commune.for_export => [commune, insee, departement] + # so we build a fake champ to have the right export + type_de_champ.champ.build.for_export else - [nil] + champ.for_export end - Array.wrap(champ_values).map.with_index do |champ_value, index| + # nil => [nil] + # text => [text] + # [commune, insee, departement] => [commune, insee, departement] + wrapped_exported_values = [exported_values].flatten + + wrapped_exported_values.map.with_index do |champ_value, index| [type_de_champ.libelle_for_export(index), champ_value] end end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 268540ea5..beeac792e 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1556,8 +1556,9 @@ describe Dossier do let(:dossier) { create(:dossier, procedure:) } let(:yes_no_tdc) { procedure.types_de_champ.first } let(:text_tdc) { procedure.types_de_champ.second } + let(:tdcs) { dossier.champs.map(&:type_de_champ) } - subject { Dossier.champs_for_export(dossier.champs, dossier.champs.map(&:type_de_champ)) } + subject { Dossier.champs_for_export(dossier.champs, tdcs) } before do text_tdc.update(condition: ds_eq(champ_value(yes_no_tdc.stable_id), constant(true))) @@ -1578,6 +1579,24 @@ describe Dossier do it { is_expected.to eq([[yes_no_tdc.libelle, "Non"], [text_tdc.libelle, nil]]) } end + + context 'with another revision' do + let(:tdc_from_another_revision) { create(:type_de_champ_communes, libelle: 'commune', condition: ds_eq(constant(true), constant(true))) } + let(:tdcs) { dossier.champs.map(&:type_de_champ) << tdc_from_another_revision } + let(:yes_no_value) { 'true' } + + let(:expected) do + [ + [yes_no_tdc.libelle, "Oui"], + [text_tdc.libelle, "text"], + ["commune", nil], + ["commune (Code insee)", nil], + ["commune (Département)", ""] + ] + end + + it { is_expected.to eq(expected) } + end end end