refactor(dossier): label indexes based on type_de_champ not champ

This commit is contained in:
Paul Chavard 2024-03-14 13:34:20 +01:00
parent 33125c691e
commit eff03aaf23
7 changed files with 61 additions and 49 deletions

View file

@ -20,7 +20,7 @@ class EditableChamp::HeaderSectionComponent < ApplicationComponent
class_names( class_names(
{ {
"section-#{level}": true, "section-#{level}": true,
'header-section': @champ.dossier.auto_numbering_section_headers_for?(@champ), 'header-section': @champ.dossier.auto_numbering_section_headers_for?(@champ.type_de_champ),
'hidden': !@champ.visible? 'hidden': !@champ.visible?
}.merge(@html_class) }.merge(@html_class)
) )

View file

@ -3,10 +3,6 @@ class Champs::HeaderSectionChamp < Champ
# The user cannot enter any information here so it doesnt make much sense to search # The user cannot enter any information here so it doesnt make much sense to search
end end
def libelle_with_section_index?
libelle =~ /^\d/
end
def level def level
type_de_champ.level_for_revision(dossier.revision) type_de_champ.level_for_revision(dossier.revision)
end end

View file

@ -2,38 +2,38 @@ module DossierSectionsConcern
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
def sections_for(champ) def sections_for(type_de_champ)
@sections = Hash.new do |hash, parent| @sections = Hash.new do |hash, parent|
case parent case parent
when :public when :public
hash[parent] = champs_for_revision(scope: :public, root: true).filter(&:header_section?) hash[parent] = revision.types_de_champ_public.filter(&:header_section?)
when :private when :private
hash[parent] = champs_for_revision(scope: :private, root: true).filter(&:header_section?) hash[parent] = revision.types_de_champ_private.filter(&:header_section?)
else else
hash[parent] = champs_for_revision(scope: parent.type_de_champ).filter(&:header_section?) hash[parent] = revision.children_of(parent).filter(&:header_section?)
end end
end end
@sections[champ.parent || (champ.public? ? :public : :private)] @sections[revision.parent_of(type_de_champ) || (type_de_champ.public? ? :public : :private)]
end end
def auto_numbering_section_headers_for?(champ) def auto_numbering_section_headers_for?(type_de_champ)
return false if champ.child? return false if revision.child?(type_de_champ)
sections_for(champ)&.none?(&:libelle_with_section_index?) sections_for(type_de_champ)&.none? { _1.libelle =~ /^\d/ }
end end
def index_for_section_header(champ) def index_for_section_header(type_de_champ)
champs = champ.private? ? champs_for_revision(scope: :private, root: true) : champs_for_revision(scope: :public, root: true) types_de_champ = type_de_champ.private? ? revision.types_de_champ_private : revision.types_de_champ_public
index = 1 index = 1
champs.each do |c| types_de_champ.each do |tdc|
if c.repetition? if tdc.repetition?
index_in_repetition = c.rows.flatten.filter { _1.stable_id == champ.stable_id }.find_index(champ) index_in_repetition = revision.children_of(tdc).find_index { _1.stable_id == type_de_champ.stable_id }
return "#{index}.#{index_in_repetition + 1}" if index_in_repetition return "#{index}.#{index_in_repetition + 1}" if index_in_repetition
else else
return index if c.stable_id == champ.stable_id return index if tdc.stable_id == type_de_champ.stable_id
next unless c.visible? next unless project_champ(tdc, nil).visible?
index += 1 if c.type_de_champ.header_section? index += 1 if tdc.header_section?
end end
end end
index index

View file

@ -181,6 +181,16 @@ class ProcedureRevision < ApplicationRecord
end end
end end
def parent_of(tdc)
revision_types_de_champ
.find { _1.type_de_champ_id == tdc.id }.parent&.type_de_champ
end
def child?(tdc)
revision_types_de_champ
.find { _1.type_de_champ_id == tdc.id }.child?
end
def remove_children_of(tdc) def remove_children_of(tdc)
children_of(tdc).each do |child| children_of(tdc).each do |child|
remove_type_de_champ(child.stable_id) remove_type_de_champ(child.stable_id)

View file

@ -145,10 +145,10 @@ def add_single_champ(pdf, champ)
when 'Champs::PieceJustificativeChamp', 'Champs::TitreIdentiteChamp' when 'Champs::PieceJustificativeChamp', 'Champs::TitreIdentiteChamp'
return return
when 'Champs::HeaderSectionChamp' when 'Champs::HeaderSectionChamp'
libelle = if @dossier.auto_numbering_section_headers_for?(champ) libelle = if @dossier.auto_numbering_section_headers_for?(tdc)
"#{@dossier.index_for_section_header(champ)}. #{champ.libelle}" "#{@dossier.index_for_section_header(tdc)}. #{tdc.libelle}"
else else
champ.libelle tdc.libelle
end end
add_section_title(pdf, libelle) add_section_title(pdf, libelle)
@ -198,12 +198,14 @@ def add_single_champ(pdf, champ)
end end
end end
def add_champs(pdf, champs) def add_champs(pdf, types_de_champ)
champs.each do |champ| types_de_champ.each do |tdc|
if champ.type == 'Champs::RepetitionChamp' champ = @dossier.project_champ(tdc, nil)
champ.rows.each do |row| if tdc.repetition?
row.each do |inner_champ| inner_types_de_champ = @dossier.revision.children_of(tdc)
add_single_champ(pdf, inner_champ) champ.row_ids.each do |row_id|
inner_types_de_champ.each do |inner_tdc|
add_single_champ(pdf, @dossier.project_champ(inner_tdc, row_id))
end end
end end
else else
@ -347,11 +349,11 @@ prawn_document(page_size: "A4") do |pdf|
end end
add_title(pdf, 'Formulaire') add_title(pdf, 'Formulaire')
add_champs(pdf, @dossier.champs_public) add_champs(pdf, @dossier.revision.types_de_champ_public)
if @acls[:include_infos_administration] && @dossier.has_annotations? if @acls[:include_infos_administration] && @dossier.has_annotations?
add_title(pdf, "Annotations privées") add_title(pdf, "Annotations privées")
add_champs(pdf, @dossier.champs_private) add_champs(pdf, @dossier.revision.types_de_champ_private)
end end
if @acls[:include_infos_administration] && @dossier.avis.present? if @acls[:include_infos_administration] && @dossier.avis.present?

View file

@ -8,34 +8,39 @@ describe DossierSectionsConcern do
let(:procedure) { create(:procedure, :for_individual, types_de_champ_public:, types_de_champ_private:) } let(:procedure) { create(:procedure, :for_individual, types_de_champ_public:, types_de_champ_private:) }
let(:dossier) { create(:dossier, procedure: procedure) } let(:dossier) { create(:dossier, procedure: procedure) }
let(:public_type_de_champ) { dossier.types_de_champ_public[1] }
let(:private_type_de_champ) { dossier.types_de_champ_private[1] }
context "with no section having number" do context "with no section having number" do
it { expect(dossier.auto_numbering_section_headers_for?(dossier.champs_public[1])).to eq(true) } it { expect(dossier.auto_numbering_section_headers_for?(public_type_de_champ)).to eq(true) }
it { expect(dossier.auto_numbering_section_headers_for?(dossier.champs_private[1])).to eq(true) } it { expect(dossier.auto_numbering_section_headers_for?(private_type_de_champ)).to eq(true) }
end end
context "with public section having number" do context "with public section having number" do
let(:public_libelle) { "1 - infos" } let(:public_libelle) { "1 - infos" }
it { expect(dossier.auto_numbering_section_headers_for?(dossier.champs_public[1])).to eq(false) } it { expect(dossier.auto_numbering_section_headers_for?(public_type_de_champ)).to eq(false) }
it { expect(dossier.auto_numbering_section_headers_for?(dossier.champs_private[1])).to eq(true) } it { expect(dossier.auto_numbering_section_headers_for?(private_type_de_champ)).to eq(true) }
end end
context "with private section having number" do context "with private section having number" do
let(:private_libelle) { "1 - infos private" } let(:private_libelle) { "1 - infos private" }
it { expect(dossier.auto_numbering_section_headers_for?(dossier.champs_public[1])).to eq(true) } it { expect(dossier.auto_numbering_section_headers_for?(public_type_de_champ)).to eq(true) }
it { expect(dossier.auto_numbering_section_headers_for?(dossier.champs_private[1])).to eq(false) } it { expect(dossier.auto_numbering_section_headers_for?(private_type_de_champ)).to eq(false) }
end end
context "header_section in a repetition are not auto-numbered" do context "header_section in a repetition are not auto-numbered" do
let(:types_de_champ_public) { [{ type: :header_section, libelle: public_libelle }, { type: :repetition, mandatory: true, children: [{ type: :header_section, libelle: "Enfant" }, { type: :text }] }] } let(:types_de_champ_public) { [{ type: :header_section, libelle: public_libelle }, { type: :repetition, mandatory: true, children: [{ type: :header_section, libelle: "Enfant" }, { type: :text }] }] }
let(:public_type_de_champ) { dossier.revision.children_of(dossier.types_de_champ_public[1]).first }
context "with parent section having headers with number" do context "with parent section having headers with number" do
let(:public_libelle) { "1. Infos" } let(:public_libelle) { "1. Infos" }
it { expect(dossier.auto_numbering_section_headers_for?(dossier.champs_public[1].rows[0][0])).to eq(false) } it { expect(dossier.auto_numbering_section_headers_for?(public_type_de_champ)).to eq(false) }
end end
context "with parent section having headers without number" do context "with parent section having headers without number" do
let(:public_libelle) { "infos" } let(:public_libelle) { "infos" }
it { expect(dossier.auto_numbering_section_headers_for?(dossier.champs_public[1].rows[0][0])).to eq(false) } it { expect(dossier.auto_numbering_section_headers_for?(public_type_de_champ)).to eq(false) }
end end
end end
end end
@ -53,19 +58,19 @@ describe DossierSectionsConcern do
let(:procedure) { create(:procedure, :for_individual, types_de_champ_public: types_de_champ) } let(:procedure) { create(:procedure, :for_individual, types_de_champ_public: types_de_champ) }
let(:dossier) { create(:dossier, procedure: procedure) } let(:dossier) { create(:dossier, procedure: procedure) }
let(:headers) { dossier.champs_public.filter(&:header_section?) } let(:headers) { dossier.revision.types_de_champ_public.filter(&:header_section?) }
let(:number_value) { nil } let(:number_value) { nil }
before do before do
dossier.champs_public.find { _1.stable_id == number_stable_id }.update(value: number_value) dossier.champs.find { _1.stable_id == number_stable_id }.update(value: number_value)
dossier.reload dossier.reload
end end
context "when there are invisible sections" do context "when there are invisible sections" do
it "index accordingly header sections" do it "index accordingly header sections" do
expect(dossier.index_for_section_header(headers[0])).to eq(1) expect(dossier.index_for_section_header(headers[0])).to eq(1)
expect(headers[1]).not_to be_visible expect(dossier.project_champ(headers[1], nil)).not_to be_visible
expect(dossier.index_for_section_header(headers[2])).to eq(2) expect(dossier.index_for_section_header(headers[2])).to eq(2)
end end
end end
@ -74,7 +79,7 @@ describe DossierSectionsConcern do
let(:number_value) { 5 } let(:number_value) { 5 }
it "index accordingly header sections" do it "index accordingly header sections" do
expect(dossier.index_for_section_header(headers[0])).to eq(1) expect(dossier.index_for_section_header(headers[0])).to eq(1)
expect(headers[1]).to be_visible expect(dossier.project_champ(headers[1], nil)).to be_visible
expect(dossier.index_for_section_header(headers[1])).to eq(2) expect(dossier.index_for_section_header(headers[1])).to eq(2)
expect(dossier.index_for_section_header(headers[2])).to eq(3) expect(dossier.index_for_section_header(headers[2])).to eq(3)
end end

View file

@ -1617,13 +1617,12 @@ describe Dossier, type: :model do
end end
describe 'index_for_section_header' do describe 'index_for_section_header' do
let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) } let(:types_de_champ_public) { [{ type: :repetition, mandatory: true, children: [{ type: :header_section }] }] }
let(:dossier) { create(:dossier, procedure: procedure) } let(:procedure) { create(:procedure, types_de_champ_public:) }
let(:types_de_champ) { [{ type: :repetition, mandatory: true, children: [{ type: :header_section }] }] } let(:dossier) { create(:dossier, procedure:) }
let(:header_in_repetition) { dossier.revision.types_de_champ.find(&:header_section?) }
it 'index classly' do it 'index classly' do
repetition = dossier.champs.find(&:repetition?)
header_in_repetition = repetition.champs.find(&:header_section?)
expect(dossier.index_for_section_header(header_in_repetition)).to eq("1.1") expect(dossier.index_for_section_header(header_in_repetition)).to eq("1.1")
end end
end end