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:
commit
19acfe8795
3 changed files with 23 additions and 9 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue