refactor(dossier): extract sections logic in a concern

This commit is contained in:
Colin Darie 2023-02-21 11:42:30 +01:00
parent 8156e1cc01
commit ae93d3927e
4 changed files with 106 additions and 99 deletions

View file

@ -0,0 +1,35 @@
module DossierSectionsConcern
extend ActiveSupport::Concern
included do
def sections_for(champ)
@sections = Hash.new do |hash, parent|
case parent
when :public
hash[parent] = champs_public.filter(&:header_section?)
when :private
hash[parent] = champs_private.filter(&:header_section?)
else
hash[parent] = parent.champs.filter(&:header_section?)
end
end
@sections[champ.parent || (champ.public? ? :public : :private)]
end
def auto_numbering_section_headers_for?(champ)
sections_for(champ)&.none?(&:libelle_with_section_index?)
end
def index_for_section_header(champ)
champs = champ.private? ? champs_private : champs_public
index = 1
champs.each do |c|
return index if c.stable_id == champ.stable_id
next unless c.visible?
index += 1 if c.type_de_champ.header_section?
end
end
end
end

View file

@ -48,8 +48,9 @@
class Dossier < ApplicationRecord
self.ignored_columns = [:en_construction_conservation_extension]
include DossierFilteringConcern
include DossierRebaseConcern
include DossierPrefillableConcern
include DossierRebaseConcern
include DossierSectionsConcern
enum state: {
brouillon: 'brouillon',
@ -1237,20 +1238,6 @@ class Dossier < ApplicationRecord
termine_expired_to_delete.find_each(&:purge_discarded)
end
def sections_for(champ)
@sections = Hash.new do |hash, parent|
case parent
when :public
hash[parent] = champs_public.filter(&:header_section?)
when :private
hash[parent] = champs_private.filter(&:header_section?)
else
hash[parent] = parent.champs.filter(&:header_section?)
end
end
@sections[champ.parent || (champ.public? ? :public : :private)]
end
def clone
dossier_attributes = [:autorisation_donnees, :user_id, :revision_id, :groupe_instructeur_id]
relationships = [:individual, :etablissement]
@ -1291,22 +1278,6 @@ class Dossier < ApplicationRecord
false
end
def auto_numbering_section_headers_for?(champ)
sections_for(champ)&.none?(&:libelle_with_section_index?)
end
def index_for_section_header(champ)
champs = champ.private? ? champs_private : champs_public
index = 1
champs.each do |c|
return index if c.stable_id == champ.stable_id
next unless c.visible?
index += 1 if c.type_de_champ.header_section?
end
end
private
def create_missing_traitemets

View file

@ -0,0 +1,69 @@
describe DossierSectionsConcern do
describe '#auto_numbering_section_headers_for?' do
let(:public_libelle) { "Infos" }
let(:private_libelle) { "Infos Private" }
let(:types_de_champ_public) { [{ type: :header_section, libelle: public_libelle }, { type: :header_section, libelle: "Details" }] }
let(:types_de_champ_private) { [{ type: :header_section, libelle: private_libelle }, { type: :header_section, libelle: "Details Private" }] }
let(:procedure) { create(:procedure, :for_individual, types_de_champ_public:, types_de_champ_private:) }
let(:dossier) { create(:dossier, procedure: procedure) }
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) }
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) }
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) }
end
end
describe '#index_for_section_header' do
include Logic
let(:number_stable_id) { 99 }
let(:types_de_champ) {
[
{ type: :header_section, libelle: "Infos" }, { type: :integer_number, stable_id: number_stable_id },
{ type: :header_section, libelle: "Details", condition: ds_eq(champ_value(99), constant(5)) }, { type: :header_section, libelle: "Conclusion" }
]
}
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(:number_value) { nil }
before do
dossier.champs_public.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.index_for_section_header(headers[2])).to eq(2)
end
end
context "when all headers are visible" 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.index_for_section_header(headers[1])).to eq(2)
expect(dossier.index_for_section_header(headers[2])).to eq(3)
end
end
end
end

View file

@ -2089,74 +2089,6 @@ describe Dossier do
end
end
describe '#auto_numbering_section_headers_for?' do
let(:public_libelle) { "Infos" }
let(:private_libelle) { "Infos Private" }
let(:types_de_champ_public) { [{ type: :header_section, libelle: public_libelle }, { type: :header_section, libelle: "Details" }] }
let(:types_de_champ_private) { [{ type: :header_section, libelle: private_libelle }, { type: :header_section, libelle: "Details Private" }] }
let(:procedure) { create(:procedure, :for_individual, types_de_champ_public:, types_de_champ_private:) }
let(:dossier) { create(:dossier, procedure: procedure) }
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) }
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) }
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) }
end
end
describe '#index_for_section_header' do
include Logic
let(:number_stable_id) { 99 }
let(:types_de_champ) {
[
{ type: :header_section, libelle: "Infos" }, { type: :integer_number, stable_id: number_stable_id },
{ type: :header_section, libelle: "Details", condition: ds_eq(champ_value(99), constant(5)) }, { type: :header_section, libelle: "Conclusion" }
]
}
let(:procedure) { create(:procedure, :for_individual, types_de_champ_public: types_de_champ) }
let(:dossier) { create(:dossier, procedure: procedure) }
let(:headers) { dossier.champs_public.select { _1.header_section? } }
let(:number_value) { nil }
before do
dossier.champs_public.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.index_for_section_header(headers[2])).to eq(2)
end
end
context "when all headers are visible" 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.index_for_section_header(headers[1])).to eq(2)
expect(dossier.index_for_section_header(headers[2])).to eq(3)
end
end
end
private
def count_for_month(processed_by_month, month)