From aa0aea15432b6c1f6e479347ab1e501ac6502e2b Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 12 Mar 2024 13:42:06 +0100 Subject: [PATCH] refactor(dossier_vide): use types_de_champ instead of empty dossier --- app/controllers/users/commencer_controller.rb | 2 +- .../champs/linked_drop_down_list_champ.rb | 2 +- app/models/procedure_revision.rb | 9 -- app/models/type_de_champ.rb | 2 + app/views/dossiers/dossier_vide.pdf.prawn | 127 +++++++++--------- spec/models/procedure_spec.rb | 20 --- .../dossiers/dossier_vide.pdf.prawn_spec.rb | 3 +- 7 files changed, 67 insertions(+), 98 deletions(-) diff --git a/app/controllers/users/commencer_controller.rb b/app/controllers/users/commencer_controller.rb index bdbced324..42e8d9ddc 100644 --- a/app/controllers/users/commencer_controller.rb +++ b/app/controllers/users/commencer_controller.rb @@ -151,7 +151,7 @@ module Users end def generate_empty_pdf(revision) - @dossier = revision.new_dossier + @revision = revision data = render_to_string(template: 'dossiers/dossier_vide', formats: [:pdf]) send_data(data, filename: "#{revision.procedure.libelle}.pdf") end diff --git a/app/models/champs/linked_drop_down_list_champ.rb b/app/models/champs/linked_drop_down_list_champ.rb index 461fb355d..ee2d3fb6e 100644 --- a/app/models/champs/linked_drop_down_list_champ.rb +++ b/app/models/champs/linked_drop_down_list_champ.rb @@ -1,5 +1,5 @@ class Champs::LinkedDropDownListChamp < Champ - delegate :primary_options, :secondary_options, to: 'type_de_champ.dynamic_type' + delegate :primary_options, :secondary_options, to: :type_de_champ def options? drop_down_list_options? diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index ff9959f1e..4f8029614 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -133,15 +133,6 @@ class ProcedureRevision < ApplicationRecord changes end - def new_dossier - Dossier.new( - revision: self, - champs_public: build_champs_public, - champs_private: build_champs_private, - groupe_instructeur: procedure.defaut_groupe_instructeur - ) - end - def dossier_for_preview(user) dossier = Dossier .create_with(autorisation_donnees: true) diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 87c5acba4..b07219d0e 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -167,6 +167,8 @@ class TypeDeChamp < ApplicationRecord attr_reader :dynamic_type + delegate :primary_options, :secondary_options, to: :dynamic_type + scope :public_only, -> { where(private: false) } scope :private_only, -> { where(private: true) } scope :repetition, -> { where(type_champ: type_champs.fetch(:repetition)) } diff --git a/app/views/dossiers/dossier_vide.pdf.prawn b/app/views/dossiers/dossier_vide.pdf.prawn index fd11c3abf..266ae50c6 100644 --- a/app/views/dossiers/dossier_vide.pdf.prawn +++ b/app/views/dossiers/dossier_vide.pdf.prawn @@ -70,9 +70,9 @@ def add_page_numbering(pdf) pdf.number_pages string, options end -def add_procedure(pdf, dossier) +def add_procedure(pdf, procedure) pdf.repeat(lambda { |page| page > 1 }) do - pdf.draw_text dossier.procedure.libelle, :at => pdf.bounds.top_left + pdf.draw_text procedure.libelle, :at => pdf.bounds.top_left end end @@ -80,7 +80,7 @@ def format_date(date) I18n.l(date, format: :message_date_with_year) end -def add_identite_individual(pdf, dossier) +def add_identite_individual(pdf) format_in_2_columns(pdf, "Civilité") format_in_2_columns(pdf, "Nom") format_in_2_columns(pdf, "Prénom") @@ -106,96 +106,92 @@ def add_title(pdf, title) pdf.text "\n" end -def add_libelle(pdf, champ) - add_single_line(pdf, champ.libelle, 9, :bold) +def add_libelle(pdf, type_de_champ) + add_single_line(pdf, type_de_champ.libelle, 9, :bold) end def add_explanation(pdf, explanation) add_single_line(pdf, explanation, 9, :italic) end -def add_optionnal_description(pdf, champ) - add_explanation(pdf, strip_tags(champ.description).strip + "\n\n") if champ.description.present? +def add_optionnal_description(pdf, type_de_champ) + add_explanation(pdf, strip_tags(type_de_champ.description).strip + "\n\n") if type_de_champ.description.present? end -def render_single_champ(pdf, champ) - case champ.type - when 'Champs::RepetitionChamp' - raise 'There should not be a RepetitionChamp here !' - when 'Champs::PieceJustificativeChamp' +def render_single_champ(pdf, revision, type_de_champ) + case type_de_champ.type_champ + when TypeDeChamp.type_champs.fetch(:repetition) + add_libelle(pdf, type_de_champ) + types_de_champ = revision.children_of(type_de_champ) + + 3.times do + types_de_champ.each do |type_de_champ| + render_single_champ(pdf, revision, type_de_champ) + end + end + when TypeDeChamp.type_champs.fetch(:piece_justificative) add_single_line(pdf, 'Pièce justificative à joindre en complément du dossier', 9, :bold) - format_with_checkbox(pdf, champ.libelle) - add_optionnal_description(pdf, champ) + format_with_checkbox(pdf, type_de_champ.libelle) + add_optionnal_description(pdf, type_de_champ) pdf.text "\n" - when 'Champs::YesNoChamp', 'Champs::CheckboxChamp' - add_libelle(pdf, champ) - add_optionnal_description(pdf, champ) + when TypeDeChamp.type_champs.fetch(:yes_no), TypeDeChamp.type_champs.fetch(:checkbox) + add_libelle(pdf, type_de_champ) + add_optionnal_description(pdf, type_de_champ) add_explanation(pdf, 'Cochez la mention applicable') format_with_checkbox(pdf, 'Oui') format_with_checkbox(pdf, 'Non') pdf.text "\n" - when 'Champs::CiviliteChamp' - add_libelle(pdf, champ) - add_optionnal_description(pdf, champ) + when TypeDeChamp.type_champs.fetch(:civilite) + add_libelle(pdf, type_de_champ) + add_optionnal_description(pdf, type_de_champ) format_with_checkbox(pdf, Individual::GENDER_FEMALE) format_with_checkbox(pdf, Individual::GENDER_MALE) pdf.text "\n" - when 'Champs::HeaderSectionChamp' - add_single_line(pdf, champ.libelle, 14, :bold) - add_optionnal_description(pdf, champ) + when TypeDeChamp.type_champs.fetch(:header_section) + add_single_line(pdf, type_de_champ.libelle, 14, :bold) + add_optionnal_description(pdf, type_de_champ) pdf.text "\n" - when 'Champs::ExplicationChamp' - add_libelle(pdf, champ) - pdf.text champ.description + when TypeDeChamp.type_champs.fetch(:explication) + add_libelle(pdf, type_de_champ) + pdf.text type_de_champ.description pdf.text "\n" - when 'Champs::AddressChamp', 'Champs::CarteChamp', 'Champs::TextareaChamp' - format_in_2_lines(pdf, champ, 5) - when 'Champs::DropDownListChamp' - add_libelle(pdf, champ) - add_optionnal_description(pdf, champ) + when TypeDeChamp.type_champs.fetch(:address), TypeDeChamp.type_champs.fetch(:carte), TypeDeChamp.type_champs.fetch(:textarea) + format_in_2_lines(pdf, type_de_champ, 5) + when TypeDeChamp.type_champs.fetch(:drop_down_list) + add_libelle(pdf, type_de_champ) + add_optionnal_description(pdf, type_de_champ) add_explanation(pdf, 'Cochez la mention applicable, une seule valeur possible') - champ.enabled_non_empty_options.each do |option| + type_de_champ.drop_down_list_enabled_non_empty_options.each do |option| format_with_checkbox(pdf, option) end pdf.text "\n" - when 'Champs::MultipleDropDownListChamp' - add_libelle(pdf, champ) - add_optionnal_description(pdf, champ) + when TypeDeChamp.type_champs.fetch(:multiple_drop_down_list) + add_libelle(pdf, type_de_champ) + add_optionnal_description(pdf, type_de_champ) add_explanation(pdf, 'Cochez la mention applicable, plusieurs valeurs possibles') - champ.enabled_non_empty_options.each do |option| + type_de_champ.drop_down_list_enabled_non_empty_options.each do |option| format_with_checkbox(pdf, option) end pdf.text "\n" - when 'Champs::LinkedDropDownListChamp' - add_libelle(pdf, champ) - champ.primary_options.compact_blank.each do |o| + when TypeDeChamp.type_champs.fetch(:linked_drop_down_list) + add_libelle(pdf, type_de_champ) + type_de_champ.primary_options.compact_blank.each do |o| format_with_checkbox(pdf, o) - champ.secondary_options[o].compact_blank.each do |secondary_option| + type_de_champ.secondary_options[o].compact_blank.each do |secondary_option| format_with_checkbox(pdf, secondary_option, 15) end end pdf.text "\n" - when 'Champs::SiretChamp' - add_identite_etablissement(pdf, champ.libelle) + when TypeDeChamp.type_champs.fetch(:siret) + add_identite_etablissement(pdf, type_de_champ.libelle) else - format_in_2_lines(pdf, champ) + format_in_2_lines(pdf, type_de_champ) end end -def add_champs(pdf, champs) - champs.each do |champ| - if champ.type == 'Champs::RepetitionChamp' - add_libelle(pdf, champ) - added_champs = champ.add_row(champ.type_de_champ.revision) - - 3.times do - added_champs.each do |champ| - render_single_champ(pdf, champ) - end - end - else - render_single_champ(pdf, champ) - end +def add_champs(pdf, revision, types_de_champ) + types_de_champ.each do |type_de_champ| + render_single_champ(pdf, revision, type_de_champ) end end @@ -209,23 +205,24 @@ prawn_document(page_size: "A4") do |pdf| pdf.image DOSSIER_PDF_EXPORT_LOGO_SRC, width: 300, position: :center pdf.move_down(40) - render_in_2_columns(pdf, 'Démarche', @dossier.procedure.libelle) - render_in_2_columns(pdf, 'Organisme', @dossier.procedure.organisation_name || "En attente de saisi") + render_in_2_columns(pdf, 'Démarche', @procedure.libelle) + render_in_2_columns(pdf, 'Organisme', @procedure.organisation_name || "En attente de saisi") pdf.text "\n" add_title(pdf, "Identité du demandeur") format_in_2_columns(pdf, "Email") - if @dossier.procedure.for_individual? - add_identite_individual(pdf, @dossier) + + if @procedure.for_individual? + add_identite_individual(pdf) else - render_identite_etablissement(pdf, @dossier.etablissement) if @dossier.etablissement.present? + add_identite_etablissement(pdf, 'Etablissement') end pdf.text "\n" add_title(pdf, 'Formulaire') - add_single_line(pdf, @dossier.procedure.description + "\n", 9, :italic) if @dossier.procedure.description.present? - add_champs(pdf, @dossier.champs_public) + add_single_line(pdf, @procedure.description + "\n", 9, :italic) if @procedure.description.present? + add_champs(pdf, @revision, @revision.types_de_champ_public) add_page_numbering(pdf) - add_procedure(pdf, @dossier) + add_procedure(pdf, @procedure) end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index c1aedbd90..956560a02 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -1366,26 +1366,6 @@ describe Procedure do it { expect(Procedure.default_sort).to eq({ "table" => "self", "column" => "id", "order" => "desc" }) } end - describe '#new_dossier' do - let(:procedure) do - create(:procedure, - types_de_champ_public: [{}, { type: :number }], - types_de_champ_private: [{ type: :textarea }]) - end - - let(:dossier) { procedure.active_revision.new_dossier } - - it { expect(dossier.procedure).to eq(procedure) } - - it { expect(dossier.champs_public.size).to eq(2) } - it { expect(dossier.champs_public.first.type).to eq("Champs::TextChamp") } - - it { expect(dossier.champs_private.size).to eq(1) } - it { expect(dossier.champs_private.first.type).to eq("Champs::TextareaChamp") } - - it { expect(Champ.count).to eq(0) } - end - describe "#organisation_name" do subject { procedure.organisation_name } context 'when the procedure has a service (and no organization)' do diff --git a/spec/views/dossiers/dossier_vide.pdf.prawn_spec.rb b/spec/views/dossiers/dossier_vide.pdf.prawn_spec.rb index c25707c2f..ad1fb2756 100644 --- a/spec/views/dossiers/dossier_vide.pdf.prawn_spec.rb +++ b/spec/views/dossiers/dossier_vide.pdf.prawn_spec.rb @@ -1,10 +1,9 @@ describe 'dossiers/dossier_vide', type: :view do let(:procedure) { create(:procedure, :with_all_champs) } - let(:dossier) { create(:dossier, procedure: procedure) } before do assign(:procedure, procedure) - assign(:dossier, dossier) + assign(:revision, procedure.draft_revision) end subject { render }