fix(export): bug when combining revision and conditional

This commit is contained in:
simon lehericey 2022-10-18 11:00:44 +02:00 committed by LeSim
parent 1d82d5f7bb
commit c6e7db3622
2 changed files with 33 additions and 7 deletions

View file

@ -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

View file

@ -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