refactor(dossier_vide): use types_de_champ instead of empty dossier

This commit is contained in:
Paul Chavard 2024-03-12 13:42:06 +01:00
parent a106394e89
commit aa0aea1543
7 changed files with 67 additions and 98 deletions

View file

@ -151,7 +151,7 @@ module Users
end end
def generate_empty_pdf(revision) def generate_empty_pdf(revision)
@dossier = revision.new_dossier @revision = revision
data = render_to_string(template: 'dossiers/dossier_vide', formats: [:pdf]) data = render_to_string(template: 'dossiers/dossier_vide', formats: [:pdf])
send_data(data, filename: "#{revision.procedure.libelle}.pdf") send_data(data, filename: "#{revision.procedure.libelle}.pdf")
end end

View file

@ -1,5 +1,5 @@
class Champs::LinkedDropDownListChamp < Champ 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? def options?
drop_down_list_options? drop_down_list_options?

View file

@ -133,15 +133,6 @@ class ProcedureRevision < ApplicationRecord
changes changes
end 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) def dossier_for_preview(user)
dossier = Dossier dossier = Dossier
.create_with(autorisation_donnees: true) .create_with(autorisation_donnees: true)

View file

@ -167,6 +167,8 @@ class TypeDeChamp < ApplicationRecord
attr_reader :dynamic_type attr_reader :dynamic_type
delegate :primary_options, :secondary_options, to: :dynamic_type
scope :public_only, -> { where(private: false) } scope :public_only, -> { where(private: false) }
scope :private_only, -> { where(private: true) } scope :private_only, -> { where(private: true) }
scope :repetition, -> { where(type_champ: type_champs.fetch(:repetition)) } scope :repetition, -> { where(type_champ: type_champs.fetch(:repetition)) }

View file

@ -70,9 +70,9 @@ def add_page_numbering(pdf)
pdf.number_pages string, options pdf.number_pages string, options
end end
def add_procedure(pdf, dossier) def add_procedure(pdf, procedure)
pdf.repeat(lambda { |page| page > 1 }) do 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
end end
@ -80,7 +80,7 @@ def format_date(date)
I18n.l(date, format: :message_date_with_year) I18n.l(date, format: :message_date_with_year)
end end
def add_identite_individual(pdf, dossier) def add_identite_individual(pdf)
format_in_2_columns(pdf, "Civilité") format_in_2_columns(pdf, "Civilité")
format_in_2_columns(pdf, "Nom") format_in_2_columns(pdf, "Nom")
format_in_2_columns(pdf, "Prénom") format_in_2_columns(pdf, "Prénom")
@ -106,96 +106,92 @@ def add_title(pdf, title)
pdf.text "\n" pdf.text "\n"
end end
def add_libelle(pdf, champ) def add_libelle(pdf, type_de_champ)
add_single_line(pdf, champ.libelle, 9, :bold) add_single_line(pdf, type_de_champ.libelle, 9, :bold)
end end
def add_explanation(pdf, explanation) def add_explanation(pdf, explanation)
add_single_line(pdf, explanation, 9, :italic) add_single_line(pdf, explanation, 9, :italic)
end end
def add_optionnal_description(pdf, champ) def add_optionnal_description(pdf, type_de_champ)
add_explanation(pdf, strip_tags(champ.description).strip + "\n\n") if champ.description.present? add_explanation(pdf, strip_tags(type_de_champ.description).strip + "\n\n") if type_de_champ.description.present?
end end
def render_single_champ(pdf, champ) def render_single_champ(pdf, revision, type_de_champ)
case champ.type case type_de_champ.type_champ
when 'Champs::RepetitionChamp' when TypeDeChamp.type_champs.fetch(:repetition)
raise 'There should not be a RepetitionChamp here !' add_libelle(pdf, type_de_champ)
when 'Champs::PieceJustificativeChamp' 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) add_single_line(pdf, 'Pièce justificative à joindre en complément du dossier', 9, :bold)
format_with_checkbox(pdf, champ.libelle) format_with_checkbox(pdf, type_de_champ.libelle)
add_optionnal_description(pdf, champ) add_optionnal_description(pdf, type_de_champ)
pdf.text "\n" pdf.text "\n"
when 'Champs::YesNoChamp', 'Champs::CheckboxChamp' when TypeDeChamp.type_champs.fetch(:yes_no), TypeDeChamp.type_champs.fetch(:checkbox)
add_libelle(pdf, champ) add_libelle(pdf, type_de_champ)
add_optionnal_description(pdf, champ) add_optionnal_description(pdf, type_de_champ)
add_explanation(pdf, 'Cochez la mention applicable') add_explanation(pdf, 'Cochez la mention applicable')
format_with_checkbox(pdf, 'Oui') format_with_checkbox(pdf, 'Oui')
format_with_checkbox(pdf, 'Non') format_with_checkbox(pdf, 'Non')
pdf.text "\n" pdf.text "\n"
when 'Champs::CiviliteChamp' when TypeDeChamp.type_champs.fetch(:civilite)
add_libelle(pdf, champ) add_libelle(pdf, type_de_champ)
add_optionnal_description(pdf, champ) add_optionnal_description(pdf, type_de_champ)
format_with_checkbox(pdf, Individual::GENDER_FEMALE) format_with_checkbox(pdf, Individual::GENDER_FEMALE)
format_with_checkbox(pdf, Individual::GENDER_MALE) format_with_checkbox(pdf, Individual::GENDER_MALE)
pdf.text "\n" pdf.text "\n"
when 'Champs::HeaderSectionChamp' when TypeDeChamp.type_champs.fetch(:header_section)
add_single_line(pdf, champ.libelle, 14, :bold) add_single_line(pdf, type_de_champ.libelle, 14, :bold)
add_optionnal_description(pdf, champ) add_optionnal_description(pdf, type_de_champ)
pdf.text "\n" pdf.text "\n"
when 'Champs::ExplicationChamp' when TypeDeChamp.type_champs.fetch(:explication)
add_libelle(pdf, champ) add_libelle(pdf, type_de_champ)
pdf.text champ.description pdf.text type_de_champ.description
pdf.text "\n" pdf.text "\n"
when 'Champs::AddressChamp', 'Champs::CarteChamp', 'Champs::TextareaChamp' when TypeDeChamp.type_champs.fetch(:address), TypeDeChamp.type_champs.fetch(:carte), TypeDeChamp.type_champs.fetch(:textarea)
format_in_2_lines(pdf, champ, 5) format_in_2_lines(pdf, type_de_champ, 5)
when 'Champs::DropDownListChamp' when TypeDeChamp.type_champs.fetch(:drop_down_list)
add_libelle(pdf, champ) add_libelle(pdf, type_de_champ)
add_optionnal_description(pdf, champ) add_optionnal_description(pdf, type_de_champ)
add_explanation(pdf, 'Cochez la mention applicable, une seule valeur possible') 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) format_with_checkbox(pdf, option)
end end
pdf.text "\n" pdf.text "\n"
when 'Champs::MultipleDropDownListChamp' when TypeDeChamp.type_champs.fetch(:multiple_drop_down_list)
add_libelle(pdf, champ) add_libelle(pdf, type_de_champ)
add_optionnal_description(pdf, champ) add_optionnal_description(pdf, type_de_champ)
add_explanation(pdf, 'Cochez la mention applicable, plusieurs valeurs possibles') 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) format_with_checkbox(pdf, option)
end end
pdf.text "\n" pdf.text "\n"
when 'Champs::LinkedDropDownListChamp' when TypeDeChamp.type_champs.fetch(:linked_drop_down_list)
add_libelle(pdf, champ) add_libelle(pdf, type_de_champ)
champ.primary_options.compact_blank.each do |o| type_de_champ.primary_options.compact_blank.each do |o|
format_with_checkbox(pdf, 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) format_with_checkbox(pdf, secondary_option, 15)
end end
end end
pdf.text "\n" pdf.text "\n"
when 'Champs::SiretChamp' when TypeDeChamp.type_champs.fetch(:siret)
add_identite_etablissement(pdf, champ.libelle) add_identite_etablissement(pdf, type_de_champ.libelle)
else else
format_in_2_lines(pdf, champ) format_in_2_lines(pdf, type_de_champ)
end end
end end
def add_champs(pdf, champs) def add_champs(pdf, revision, types_de_champ)
champs.each do |champ| types_de_champ.each do |type_de_champ|
if champ.type == 'Champs::RepetitionChamp' render_single_champ(pdf, revision, type_de_champ)
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
end end
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.image DOSSIER_PDF_EXPORT_LOGO_SRC, width: 300, position: :center
pdf.move_down(40) pdf.move_down(40)
render_in_2_columns(pdf, 'Démarche', @dossier.procedure.libelle) render_in_2_columns(pdf, 'Démarche', @procedure.libelle)
render_in_2_columns(pdf, 'Organisme', @dossier.procedure.organisation_name || "En attente de saisi") render_in_2_columns(pdf, 'Organisme', @procedure.organisation_name || "En attente de saisi")
pdf.text "\n" pdf.text "\n"
add_title(pdf, "Identité du demandeur") add_title(pdf, "Identité du demandeur")
format_in_2_columns(pdf, "Email") 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 else
render_identite_etablissement(pdf, @dossier.etablissement) if @dossier.etablissement.present? add_identite_etablissement(pdf, 'Etablissement')
end end
pdf.text "\n" pdf.text "\n"
add_title(pdf, 'Formulaire') add_title(pdf, 'Formulaire')
add_single_line(pdf, @dossier.procedure.description + "\n", 9, :italic) if @dossier.procedure.description.present? add_single_line(pdf, @procedure.description + "\n", 9, :italic) if @procedure.description.present?
add_champs(pdf, @dossier.champs_public) add_champs(pdf, @revision, @revision.types_de_champ_public)
add_page_numbering(pdf) add_page_numbering(pdf)
add_procedure(pdf, @dossier) add_procedure(pdf, @procedure)
end end

View file

@ -1366,26 +1366,6 @@ describe Procedure do
it { expect(Procedure.default_sort).to eq({ "table" => "self", "column" => "id", "order" => "desc" }) } it { expect(Procedure.default_sort).to eq({ "table" => "self", "column" => "id", "order" => "desc" }) }
end 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 describe "#organisation_name" do
subject { procedure.organisation_name } subject { procedure.organisation_name }
context 'when the procedure has a service (and no organization)' do context 'when the procedure has a service (and no organization)' do

View file

@ -1,10 +1,9 @@
describe 'dossiers/dossier_vide', type: :view do describe 'dossiers/dossier_vide', type: :view do
let(:procedure) { create(:procedure, :with_all_champs) } let(:procedure) { create(:procedure, :with_all_champs) }
let(:dossier) { create(:dossier, procedure: procedure) }
before do before do
assign(:procedure, procedure) assign(:procedure, procedure)
assign(:dossier, dossier) assign(:revision, procedure.draft_revision)
end end
subject { render } subject { render }