From bbb6309b4f67f81cdff5ea2c4a92a091434965aa Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Sun, 3 Mar 2024 10:27:48 +0100 Subject: [PATCH] procedure: add pieces_jointes_exportables_list --- app/models/procedure.rb | 26 ++++++++++++++++++++++---- app/models/procedure_revision.rb | 1 + spec/models/procedure_spec.rb | 26 +++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index aa0c5695c..02b1a3d15 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -992,6 +992,12 @@ class Procedure < ApplicationRecord end end + def pieces_jointes_exportables_list + pieces_jointes_list(with_private: true, with_titre_identite: false, with_repetition_parent: false) do |base_scope| + base_scope + end.flatten + end + def pieces_jointes_list_with_conditionnal pieces_jointes_list do |base_scope| base_scope.where.not(types_de_champ: { condition: nil }) @@ -1025,15 +1031,27 @@ class Procedure < ApplicationRecord private - def pieces_jointes_list - scope = yield active_revision.revision_types_de_champ_public + def pieces_jointes_list(with_private: false, with_titre_identite: true, with_repetition_parent: true) + types_de_champ = with_private ? + active_revision.revision_types_de_champ_private_and_public : + active_revision.revision_types_de_champ_public + + type_champs = ['repetition', 'piece_justificative'] + type_champs << 'titre_identite' if with_titre_identite + + scope = yield types_de_champ .includes(:type_de_champ, revision_types_de_champ: :type_de_champ) - .where(types_de_champ: { type_champ: ['repetition', 'piece_justificative', 'titre_identite'] }) + .where(types_de_champ: { type_champ: [type_champs] }) scope.each_with_object([]) do |rtdc, list| if rtdc.type_de_champ.repetition? rtdc.revision_types_de_champ.each do |rtdc_in_repetition| - list << [rtdc_in_repetition.type_de_champ, rtdc.type_de_champ] if rtdc_in_repetition.type_de_champ.piece_justificative? + if rtdc_in_repetition.type_de_champ.piece_justificative? + to_add = [] + to_add << rtdc_in_repetition.type_de_champ + to_add << rtdc.type_de_champ if with_repetition_parent + list << to_add + end end else list << [rtdc.type_de_champ] diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index c8daa1bec..8326c6533 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -8,6 +8,7 @@ class ProcedureRevision < ApplicationRecord has_many :revision_types_de_champ, -> { order(:position, :id) }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision has_many :revision_types_de_champ_public, -> { root.public_only.ordered }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision has_many :revision_types_de_champ_private, -> { root.private_only.ordered }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision + has_many :revision_types_de_champ_private_and_public, -> { root.ordered }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision has_many :types_de_champ, through: :revision_types_de_champ, source: :type_de_champ has_many :types_de_champ_public, through: :revision_types_de_champ_public, source: :type_de_champ has_many :types_de_champ_private, through: :revision_types_de_champ_private, source: :type_de_champ diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 01f937995..8b036cddc 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -1748,13 +1748,23 @@ describe Procedure do describe '#pieces_jointes_list' do include Logic - let(:procedure) { create(:procedure, types_de_champ_public:) } + let(:procedure) { create(:procedure, types_de_champ_public:, types_de_champ_private:) } let(:types_de_champ_public) do [ { type: :integer_number, stable_id: 900 }, { type: :piece_justificative, libelle: "PJ", mandatory: true, stable_id: 910 }, { type: :piece_justificative, libelle: "PJ-cond", mandatory: true, stable_id: 911, condition: ds_eq(champ_value(900), constant(1)) }, - { type: :repetition, libelle: "Répétition", stable_id: 920, children: [{ type: :piece_justificative, libelle: "PJ2", stable_id: 921 }] } + { type: :repetition, libelle: "Répétition", stable_id: 920, children: [{ type: :piece_justificative, libelle: "PJ2", stable_id: 921 }] }, + { type: :titre_identite, libelle: "CNI", mandatory: true, stable_id: 930 } + ] + end + + let(:types_de_champ_private) do + [ + { type: :integer_number, stable_id: 950 }, + { type: :piece_justificative, libelle: "PJ", mandatory: true, stable_id: 960 }, + { type: :piece_justificative, libelle: "PJ-cond", mandatory: true, stable_id: 961, condition: ds_eq(champ_value(900), constant(1)) }, + { type: :repetition, libelle: "Répétition", stable_id: 970, children: [{ type: :piece_justificative, libelle: "PJ2", stable_id: 971 }] } ] end @@ -1762,14 +1772,24 @@ describe Procedure do let(:pjcond) { procedure.active_revision.types_de_champ.find { _1.stable_id == 911 } } let(:repetition) { procedure.active_revision.types_de_champ.find { _1.stable_id == 920 } } let(:pj2) { procedure.active_revision.types_de_champ.find { _1.stable_id == 921 } } + let(:pj3) { procedure.active_revision.types_de_champ.find { _1.stable_id == 930 } } + + let(:pj5) { procedure.active_revision.types_de_champ.find { _1.stable_id == 960 } } + let(:pjcond2) { procedure.active_revision.types_de_champ.find { _1.stable_id == 961 } } + let(:repetition2) { procedure.active_revision.types_de_champ.find { _1.stable_id == 970 } } + let(:pj6) { procedure.active_revision.types_de_champ.find { _1.stable_id == 971 } } it "returns the list of pieces jointes without conditional" do - expect(procedure.pieces_jointes_list_without_conditionnal).to match_array([[pj1], [pj2, repetition]]) + expect(procedure.pieces_jointes_list_without_conditionnal).to match_array([[pj1], [pj2, repetition], [pj3]]) end it "returns the list of pieces jointes having conditional" do expect(procedure.pieces_jointes_list_with_conditionnal).to match_array([[pjcond]]) end + + it "returns the list of pieces jointes with private, without parent repetition, without titre identite" do + expect(procedure.pieces_jointes_exportables_list).to match_array([pj1, pj2, pjcond, pj5, pjcond2, pj6]) + end end describe "#attestation_template" do