Merge pull request #7928 from betagouv/fix_conditionnal_export_2
fix(export): corrige un bug apparaissant avec des revisions et du conditionnel
This commit is contained in:
commit
875751de69
2 changed files with 33 additions and 7 deletions
|
@ -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
|
# 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)
|
def self.champs_for_export(champs, types_de_champ)
|
||||||
types_de_champ.flat_map do |type_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 = champs.find { |champ| champ.stable_id == type_de_champ.stable_id }
|
||||||
champ_or_new ||= type_de_champ.champ.build
|
|
||||||
|
|
||||||
champ_values = if champ_or_new.visible?
|
exported_values = if champ.nil? || !champ.visible?
|
||||||
champ_or_new.for_export || [nil]
|
# 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
|
else
|
||||||
[nil]
|
champ.for_export
|
||||||
end
|
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]
|
[type_de_champ.libelle_for_export(index), champ_value]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1556,8 +1556,9 @@ describe Dossier do
|
||||||
let(:dossier) { create(:dossier, procedure:) }
|
let(:dossier) { create(:dossier, procedure:) }
|
||||||
let(:yes_no_tdc) { procedure.types_de_champ.first }
|
let(:yes_no_tdc) { procedure.types_de_champ.first }
|
||||||
let(:text_tdc) { procedure.types_de_champ.second }
|
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
|
before do
|
||||||
text_tdc.update(condition: ds_eq(champ_value(yes_no_tdc.stable_id), constant(true)))
|
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]]) }
|
it { is_expected.to eq([[yes_no_tdc.libelle, "Non"], [text_tdc.libelle, nil]]) }
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue