refactor(dossier): filled champs

This commit is contained in:
Paul Chavard 2024-10-07 11:45:05 +02:00
parent d22ae1f76f
commit 8c8bb870fc
No known key found for this signature in database
5 changed files with 66 additions and 7 deletions

View file

@ -39,6 +39,34 @@ module DossierChampsConcern
revision.types_de_champ_private.map { project_champ(_1, nil) }
end
def filled_champs_public
project_champs_public.flat_map do |champ|
if champ.repetition?
champ.rows.flatten.filter { _1.persisted? && _1.fillable? }
elsif champ.persisted? && champ.fillable?
champ
else
[]
end
end
end
def filled_champs_private
project_champs_private.flat_map do |champ|
if champ.repetition?
champ.rows.flatten.filter { _1.persisted? && _1.fillable? }
elsif champ.persisted? && champ.fillable?
champ
else
[]
end
end
end
def filled_champs
filled_champs_public + filled_champs_private
end
def project_rows_for(type_de_champ)
return [] if !type_de_champ.repetition?

View file

@ -517,7 +517,7 @@ class Dossier < ApplicationRecord
def can_passer_en_construction?
return true if !revision.ineligibilite_enabled || !revision.ineligibilite_rules
!revision.ineligibilite_rules.compute(champs_for_revision(scope: :public))
!revision.ineligibilite_rules.compute(filled_champs_public)
end
def can_passer_en_instruction?
@ -567,7 +567,7 @@ class Dossier < ApplicationRecord
def any_etablissement_as_degraded_mode?
return true if etablissement&.as_degraded_mode?
return true if champs_for_revision(scope: :public).any? { _1.etablissement&.as_degraded_mode? }
return true if filled_champs_public.any? { _1.etablissement&.as_degraded_mode? }
false
end
@ -1031,7 +1031,7 @@ class Dossier < ApplicationRecord
end
def linked_dossiers_for(instructeur_or_expert)
dossier_ids = champs_for_revision.filter(&:dossier_link?).filter_map(&:value)
dossier_ids = filled_champs.filter(&:dossier_link?).filter_map(&:value)
instructeur_or_expert.dossiers.where(id: dossier_ids)
end
@ -1040,7 +1040,7 @@ class Dossier < ApplicationRecord
end
def geo_data?
GeoArea.exists?(champ_id: champs_for_revision)
GeoArea.exists?(champ_id: filled_champs)
end
def to_feature_collection
@ -1195,7 +1195,7 @@ class Dossier < ApplicationRecord
end
def geo_areas
champs_for_revision.flat_map(&:geo_areas)
filled_champs.flat_map(&:geo_areas)
end
def bounding_box

View file

@ -110,6 +110,7 @@ RSpec.describe DossierChampsConcern do
subject { dossier.project_champs_public }
it { expect(subject.size).to eq(4) }
it { expect(subject.find { _1.libelle == 'Nom' }).to be_falsey }
end
describe '#project_champs_private' do
@ -118,6 +119,36 @@ RSpec.describe DossierChampsConcern do
it { expect(subject.size).to eq(1) }
end
describe '#filled_champs_public' do
let(:types_de_champ_public) do
[
{ type: :header_section },
{ type: :text, libelle: "Un champ text" },
{ type: :text, libelle: "Un autre champ text" },
{ type: :yes_no, libelle: "Un champ yes no" },
{ type: :repetition, libelle: "Un champ répétable", mandatory: true, children: [{ type: :text, libelle: 'Nom' }] },
{ type: :explication }
]
end
subject { dossier.filled_champs_public }
it { expect(subject.size).to eq(4) }
it { expect(subject.find { _1.libelle == 'Nom' }).to be_truthy }
end
describe '#filled_champs_private' do
let(:types_de_champ_private) do
[
{ type: :header_section },
{ type: :text, libelle: "Une annotation" },
{ type: :explication }
]
end
subject { dossier.filled_champs_private }
it { expect(subject.size).to eq(1) }
end
describe '#repetition_row_ids' do
let(:type_de_champ_repetition) { dossier.find_type_de_champ_by_stable_id(993) }
subject { dossier.repetition_row_ids(type_de_champ_repetition) }

View file

@ -1852,7 +1852,7 @@ describe Dossier, type: :model do
let(:types_de_champ_public) { [{ type: :carte }, { type: :carte }, { type: :carte }] }
it do
dossier.champs_for_revision
dossier.filled_champs
count = 0

View file

@ -13,7 +13,7 @@ module Maintenance
before { expect(champ).to receive(:visible?).and_return(visible) }
context 'when piece_justificative' do
let(:champ) { dossier.champs_for_revision(scope: :public).find(&:piece_justificative?) }
let(:champ) { dossier.filled_champs_public.find(&:piece_justificative?) }
context 'when not visible' do
let(:visible) { false }