From 84214f4bd4aded317ee5afa31df48f1a9acbc976 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 23 Mar 2021 09:55:37 +0100 Subject: [PATCH 1/2] Do not export non exportable champs on draft procedures --- app/models/procedure.rb | 2 +- spec/factories/procedure.rb | 6 ++++++ spec/models/dossier_spec.rb | 41 +++++++++++++++++++++++-------------- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 37c97e980..aef21aef0 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -93,7 +93,7 @@ class Procedure < ApplicationRecord def types_de_champ_for_export if brouillon? - draft_types_de_champ + draft_types_de_champ.reject(&:exclude_from_export?) else all_types_de_champ .uniq diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index 516c3e927..a966e222a 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -193,6 +193,12 @@ FactoryBot.define do end end + trait :with_explication do + after(:build) do |procedure, _evaluator| + build(:type_de_champ_explication, procedure: procedure) + end + end + trait :published do after(:build) do |procedure, _evaluator| procedure.path = generate(:published_path) diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index fabbe9366..8def1471c 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1345,29 +1345,40 @@ describe Dossier do end describe "champs_for_export" do - let(:procedure) { create(:procedure, :with_type_de_champ, :with_datetime, :with_yes_no) } + let(:procedure) { create(:procedure, :with_type_de_champ, :with_datetime, :with_yes_no, :with_explication) } 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(:dossier) { create(:dossier, procedure: procedure) } let(:dossier_second_revision) { create(:dossier, procedure: procedure) } - before do - procedure.publish! - dossier - 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.update(published_revision: procedure.draft_revision, draft_revision: procedure.create_new_revision) - dossier.reload - procedure.reload + context "when procedure published" do + before do + procedure.publish! + dossier + 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.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_export).map { |(libelle)| libelle }).to eq([datetime_type_de_champ.libelle, "Updated yes/no", "New text field"]) + expect(dossier.champs_for_export(dossier.procedure.types_de_champ_for_export)).to eq(dossier_second_revision.champs_for_export(dossier_second_revision.procedure.types_de_champ_for_export)) + end 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"]) - expect(dossier_second_revision.types_de_champ.map(&:libelle)).to eq([datetime_type_de_champ.libelle, "Updated yes/no", "New text field"]) - expect(dossier.champs_for_export(dossier.procedure.types_de_champ_for_export).map { |(libelle)| libelle }).to eq([datetime_type_de_champ.libelle, "Updated yes/no", "New text field"]) - expect(dossier.champs_for_export(dossier.procedure.types_de_champ_for_export)).to eq(dossier_second_revision.champs_for_export(dossier_second_revision.procedure.types_de_champ_for_export)) + context "when procedure brouillon" do + let(:procedure) { create(:procedure, :with_type_de_champ, :with_explication) } + + it "should not contain explication type de champ" do + expect(dossier.champs_for_export(dossier.procedure.types_de_champ_for_export).map { |(libelle)| libelle }).to eq([text_type_de_champ.libelle]) + end end end From 224c7ec6c331ee135f45038dce3d9ecea458f262 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 23 Mar 2021 10:51:55 +0100 Subject: [PATCH 2/2] Update spec/models/dossier_spec.rb Co-authored-by: Pierre de La Morinerie --- spec/models/dossier_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 8def1471c..179158d6d 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1376,7 +1376,7 @@ describe Dossier do context "when procedure brouillon" do let(:procedure) { create(:procedure, :with_type_de_champ, :with_explication) } - it "should not contain explication type de champ" do + it "should not contain non-exportable types de champ" do expect(dossier.champs_for_export(dossier.procedure.types_de_champ_for_export).map { |(libelle)| libelle }).to eq([text_type_de_champ.libelle]) end end