Merge pull request #7259 from betagouv/US/fix-repetition-add-commune

bug(type_de_champ.repetition): on repetition, when there is a TypeDeChamp::Commune, champs for export should contain 2 entries [one with commune (postal_code), the other for commune (code_insee)].
This commit is contained in:
mfo 2022-05-06 09:42:25 +02:00 committed by GitHub
commit 19acfe8795
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 9 deletions

View file

@ -1109,20 +1109,17 @@ class Dossier < ApplicationRecord
if procedure.routee?
columns << ['Groupe instructeur', groupe_instructeur.label]
end
columns + self.class.champs_for_export(champs + champs_private, types_de_champ)
end
# Get all the champs values for the types de champ in the final list.
# Dossier might not have corresponding champ display nil.
# 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)
# Index values by stable_id
values = champs.reject(&:exclude_from_export?)
.index_by(&:stable_id)
.transform_values(&:for_export)
# Get all the champs values for the types de champ in the final list.
# Dossier might not have corresponding champ display nil.
types_de_champ.flat_map do |type_de_champ|
Array.wrap(values[type_de_champ.stable_id] || [nil]).map.with_index do |champ_value, index|
champ_or_new = champs.find { |champ| champ.stable_id == type_de_champ.stable_id }
champ_or_new ||= type_de_champ.champ.build
Array.wrap(champ_or_new.for_export || [nil]).map.with_index do |champ_value, index|
[type_de_champ.libelle_for_export(index), champ_value]
end
end

View file

@ -190,6 +190,12 @@ FactoryBot.define do
end
end
trait :with_repetition_commune do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_repetition, types_de_champ: [build(:type_de_champ_communes)], procedure: procedure)
end
end
trait :with_number do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_number, procedure: procedure)

View file

@ -1458,6 +1458,17 @@ describe Dossier do
expect(repetition_second_revision_champs_for_export.map { |(libelle)| libelle }).to eq(procedure.types_de_champ_for_procedure_presentation.repetition.map(&:libelle_for_export))
expect(repetition_second_revision_champs_for_export.first.size).to eq(2)
end
context 'within a repetition having a type de champs commune (multiple values for export)' do
it 'works' do
proc_test = create(:procedure, :with_repetition_commune)
dossier_test = create(:dossier, procedure: proc_test)
repetition = proc_test.types_de_champ_for_procedure_presentation.repetition.first
type_champs = repetition.types_de_champ_for_revision(proc_test.active_revision).to_a
expect(type_champs.size).to eq(1)
expect(Dossier.champs_for_export(dossier.champs, type_champs).size).to eq(2)
end
end
end
context "when procedure brouillon" do