Merge pull request #10150 from tchak/refactor-dossier-sections

refactor(dossier): label indexes based on type_de_champ not champ
This commit is contained in:
Paul Chavard 2024-03-20 09:06:37 +00:00 committed by GitHub
commit 61a9bedfff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 61 additions and 49 deletions

View file

@ -20,7 +20,7 @@ class EditableChamp::HeaderSectionComponent < ApplicationComponent
class_names(
{
"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?
}.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
end
def libelle_with_section_index?
libelle =~ /^\d/
end
def level
type_de_champ.level_for_revision(dossier.revision)
end

View file

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

View file

@ -181,6 +181,16 @@ class ProcedureRevision < ApplicationRecord
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)
children_of(tdc).each do |child|
remove_type_de_champ(child.stable_id)

View file

@ -145,10 +145,10 @@ def add_single_champ(pdf, champ)
when 'Champs::PieceJustificativeChamp', 'Champs::TitreIdentiteChamp'
return
when 'Champs::HeaderSectionChamp'
libelle = if @dossier.auto_numbering_section_headers_for?(champ)
"#{@dossier.index_for_section_header(champ)}. #{champ.libelle}"
libelle = if @dossier.auto_numbering_section_headers_for?(tdc)
"#{@dossier.index_for_section_header(tdc)}. #{tdc.libelle}"
else
champ.libelle
tdc.libelle
end
add_section_title(pdf, libelle)
@ -198,12 +198,14 @@ def add_single_champ(pdf, champ)
end
end
def add_champs(pdf, champs)
champs.each do |champ|
if champ.type == 'Champs::RepetitionChamp'
champ.rows.each do |row|
row.each do |inner_champ|
add_single_champ(pdf, inner_champ)
def add_champs(pdf, types_de_champ)
types_de_champ.each do |tdc|
champ = @dossier.project_champ(tdc, nil)
if tdc.repetition?
inner_types_de_champ = @dossier.revision.children_of(tdc)
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
else
@ -347,11 +349,11 @@ prawn_document(page_size: "A4") do |pdf|
end
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?
add_title(pdf, "Annotations privées")
add_champs(pdf, @dossier.champs_private)
add_champs(pdf, @dossier.revision.types_de_champ_private)
end
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(: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
it { expect(dossier.auto_numbering_section_headers_for?(dossier.champs_public[1])).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?(public_type_de_champ)).to eq(true) }
it { expect(dossier.auto_numbering_section_headers_for?(private_type_de_champ)).to eq(true) }
end
context "with public section having number" do
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?(dossier.champs_private[1])).to eq(true) }
it { expect(dossier.auto_numbering_section_headers_for?(public_type_de_champ)).to eq(false) }
it { expect(dossier.auto_numbering_section_headers_for?(private_type_de_champ)).to eq(true) }
end
context "with private section having number" do
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?(dossier.champs_private[1])).to eq(false) }
it { expect(dossier.auto_numbering_section_headers_for?(public_type_de_champ)).to eq(true) }
it { expect(dossier.auto_numbering_section_headers_for?(private_type_de_champ)).to eq(false) }
end
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(:public_type_de_champ) { dossier.revision.children_of(dossier.types_de_champ_public[1]).first }
context "with parent section having headers with number" do
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
context "with parent section having headers without number" do
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
@ -53,19 +58,19 @@ describe DossierSectionsConcern do
let(:procedure) { create(:procedure, :for_individual, types_de_champ_public: types_de_champ) }
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 }
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
end
context "when there are invisible sections" do
it "index accordingly header sections" do
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)
end
end
@ -74,7 +79,7 @@ describe DossierSectionsConcern do
let(:number_value) { 5 }
it "index accordingly header sections" do
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[2])).to eq(3)
end

View file

@ -1617,13 +1617,12 @@ describe Dossier, type: :model do
end
describe 'index_for_section_header' do
let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) }
let(:dossier) { create(:dossier, procedure: procedure) }
let(:types_de_champ) { [{ type: :repetition, mandatory: true, children: [{ type: :header_section }] }] }
let(:types_de_champ_public) { [{ type: :repetition, mandatory: true, children: [{ type: :header_section }] }] }
let(:procedure) { create(:procedure, types_de_champ_public:) }
let(:dossier) { create(:dossier, procedure:) }
let(:header_in_repetition) { dossier.revision.types_de_champ.find(&:header_section?) }
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")
end
end