Merge pull request #10103 from tchak/dossier-vide-from-procedure

refactor(dossier_vide): use types_de_champ instead of empty dossier
This commit is contained in:
Colin Darie 2024-03-12 15:29:24 +00:00 committed by GitHub
commit fc1c6bb412
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 67 additions and 98 deletions

View file

@ -155,7 +155,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

View file

@ -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?

View file

@ -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)

View file

@ -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)) }

View file

@ -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

View file

@ -1380,26 +1380,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

View file

@ -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 }