Merge pull request #6398 from betagouv/export-communes

ajoute une colonne *Code insee* pour l'export d'un champ commune
This commit is contained in:
krichtof 2021-08-19 08:12:13 +02:00 committed by GitHub
commit 602c71f94d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 49 additions and 11 deletions

View file

@ -18,4 +18,7 @@
# type_de_champ_id :integer
#
class Champs::CommuneChamp < Champs::TextChamp
def for_export
[value, external_id]
end
end

View file

@ -876,15 +876,16 @@ class Dossier < ApplicationRecord
def champs_for_export(types_de_champ)
# Index values by stable_id
values = (champs + champs_private).reject(&:exclude_from_export?).reduce({}) do |champs, champ|
champs[champ.stable_id] = champ.for_export
champs
end
values = (champs + champs_private).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.map do |type_de_champ|
[type_de_champ.libelle, values[type_de_champ.stable_id]]
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|
[type_de_champ.libelle_for_export(index), champ_value]
end
end
end

View file

@ -61,7 +61,7 @@ class TypeDeChamp < ApplicationRecord
has_many :revision_types_de_champ, class_name: 'ProcedureRevisionTypeDeChamp', dependent: :destroy, inverse_of: :type_de_champ
has_many :revisions, through: :revision_types_de_champ
delegate :tags_for_template, to: :dynamic_type
delegate :tags_for_template, :libelle_for_export, to: :dynamic_type
class WithIndifferentAccess
def self.load(options)

View file

@ -1,2 +1,5 @@
class TypesDeChamp::CommuneTypeDeChamp < TypesDeChamp::TypeDeChampBase
def libelle_for_export(index)
[libelle, "#{libelle} (Code insee)"][index]
end
end

View file

@ -21,6 +21,10 @@ class TypesDeChamp::TypeDeChampBase
]
end
def libelle_for_export(index)
libelle
end
def build_champ
@type_de_champ.champ.build
end

View file

@ -163,6 +163,12 @@ FactoryBot.define do
end
end
trait :with_commune do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_communes, procedure: procedure)
end
end
trait :with_piece_justificative do
after(:build) do |procedure, _evaluator|
build(:type_de_champ_piece_justificative, procedure: procedure)

View file

@ -0,0 +1,10 @@
describe Champs::CommuneChamp do
let(:type_de_champ) { create(:type_de_champ_communes, libelle: 'Ma commune') }
let(:champ) { Champs::CommuneChamp.new(value: value, external_id: code_insee, type_de_champ: type_de_champ) }
let(:value) { 'Châteldon (63290)' }
let(:code_insee) { '63102' }
it { expect(champ.value).to eq('Châteldon (63290)') }
it { expect(champ.external_id).to eq('63102') }
it { expect(champ.for_export).to eq(['Châteldon (63290)', '63102']) }
end

View file

@ -1341,11 +1341,12 @@ describe Dossier do
end
describe "champs_for_export" do
let(:procedure) { create(:procedure, :with_type_de_champ, :with_datetime, :with_yes_no, :with_explication) }
let(:procedure) { create(:procedure, :with_type_de_champ, :with_datetime, :with_yes_no, :with_explication, :with_commune) }
let(:text_type_de_champ) { procedure.types_de_champ.find { |type_de_champ| type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:text) } }
let(:yes_no_type_de_champ) { procedure.types_de_champ.find { |type_de_champ| type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:yes_no) } }
let(:datetime_type_de_champ) { procedure.types_de_champ.find { |type_de_champ| type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:datetime) } }
let(:explication_type_de_champ) { procedure.types_de_champ.find { |type_de_champ| type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:explication) } }
let(:commune_type_de_champ) { procedure.types_de_champ.find { |type_de_champ| type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:communes) } }
let(:dossier) { create(:dossier, procedure: procedure) }
let(:dossier_second_revision) { create(:dossier, procedure: procedure) }
@ -1356,15 +1357,16 @@ describe Dossier do
procedure.draft_revision.remove_type_de_champ(text_type_de_champ.stable_id)
procedure.draft_revision.add_type_de_champ(type_champ: TypeDeChamp.type_champs.fetch(:text), libelle: 'New text field')
procedure.draft_revision.find_or_clone_type_de_champ(yes_no_type_de_champ.stable_id).update(libelle: 'Updated yes/no')
procedure.draft_revision.find_or_clone_type_de_champ(commune_type_de_champ.stable_id).update(libelle: 'Commune de naissance')
procedure.update(published_revision: procedure.draft_revision, draft_revision: procedure.create_new_revision)
dossier.reload
procedure.reload
end
it "should have champs from all revisions" do
expect(dossier.types_de_champ.map(&:libelle)).to eq([text_type_de_champ.libelle, datetime_type_de_champ.libelle, "Yes/no", explication_type_de_champ.libelle])
expect(dossier_second_revision.types_de_champ.map(&:libelle)).to eq([datetime_type_de_champ.libelle, "Updated yes/no", explication_type_de_champ.libelle, "New text field"])
expect(dossier.champs_for_export(dossier.procedure.types_de_champ_for_procedure_presentation).map { |(libelle)| libelle }).to eq([text_type_de_champ.libelle, datetime_type_de_champ.libelle, "Updated yes/no", "New text field"])
expect(dossier.types_de_champ.map(&:libelle)).to eq([text_type_de_champ.libelle, datetime_type_de_champ.libelle, "Yes/no", explication_type_de_champ.libelle, commune_type_de_champ.libelle])
expect(dossier_second_revision.types_de_champ.map(&:libelle)).to eq([datetime_type_de_champ.libelle, "Updated yes/no", explication_type_de_champ.libelle, 'Commune de naissance', "New text field"])
expect(dossier.champs_for_export(dossier.procedure.types_de_champ_for_procedure_presentation).map { |(libelle)| libelle }).to eq([text_type_de_champ.libelle, datetime_type_de_champ.libelle, "Updated yes/no", "Commune de naissance", "Commune de naissance (Code insee)", "New text field"])
expect(dossier.champs_for_export(dossier.procedure.types_de_champ_for_procedure_presentation)).to eq(dossier_second_revision.champs_for_export(dossier_second_revision.procedure.types_de_champ_for_procedure_presentation))
end
end

View file

@ -0,0 +1,6 @@
describe TypesDeChamp::CommuneTypeDeChamp do
let(:subject) { create(:type_de_champ_communes, libelle: 'Ma commune') }
it { expect(subject.libelle_for_export(0)).to eq('Ma commune') }
it { expect(subject.libelle_for_export(1)).to eq('Ma commune (Code insee)') }
end

View file

@ -68,6 +68,7 @@ describe ProcedureExportService do
"regions",
"departements",
"communes",
"communes (Code insee)",
"engagement",
"dossier_link",
"piece_justificative",
@ -154,6 +155,7 @@ describe ProcedureExportService do
"regions",
"departements",
"communes",
"communes (Code insee)",
"engagement",
"dossier_link",
"piece_justificative",
@ -236,6 +238,7 @@ describe ProcedureExportService do
"regions",
"departements",
"communes",
"communes (Code insee)",
"engagement",
"dossier_link",
"piece_justificative",