refactor(dossier): create only fillable champs on new dossiers

This commit is contained in:
Paul Chavard 2024-11-26 22:07:38 +01:00
parent 7ee90b6d85
commit 5f10486a51
No known key found for this signature in database
3 changed files with 35 additions and 48 deletions

View file

@ -1058,13 +1058,13 @@ class Dossier < ApplicationRecord
end
def build_default_champs_for(types_de_champ)
self.champs << types_de_champ.flat_map do |type_de_champ|
champ = type_de_champ.build_champ(dossier: self)
if type_de_champ.repetition? && (type_de_champ.private? || type_de_champ.mandatory?)
row_id = ULID.generate
[champ] + revision.children_of(type_de_champ).map { _1.build_champ(dossier: self, row_id:) }
self.champs << types_de_champ.filter(&:fillable?).filter_map do |type_de_champ|
if type_de_champ.repetition?
if type_de_champ.private? || type_de_champ.mandatory?
type_de_champ.build_champ(dossier: self, row_id: ULID.generate)
end
else
champ
type_de_champ.build_champ(dossier: self)
end
end
end

View file

@ -94,14 +94,6 @@ FactoryBot.define do
external_id { '200071991' }
end
factory :champ_do_not_use_header_section, class: 'Champs::HeaderSectionChamp' do
value { 'une section' }
end
factory :champ_do_not_use_explication, class: 'Champs::ExplicationChamp' do
value { '' }
end
factory :champ_do_not_use_dossier_link, class: 'Champs::DossierLinkChamp' do
value { create(:dossier, :en_construction).id }
end
@ -188,25 +180,5 @@ FactoryBot.define do
factory :champ_do_not_use_expression_reguliere, class: 'Champs::ExpressionReguliereChamp' do
end
factory :champ_do_not_use_repetition, class: 'Champs::RepetitionChamp' do
transient do
rows { 2 }
end
after(:build) do |champ_repetition, evaluator|
revision = champ_repetition.procedure.active_revision
parent = revision.revision_types_de_champ.find { _1.type_de_champ == champ_repetition.type_de_champ }
types_de_champ = revision.revision_types_de_champ.filter { _1.parent == parent }.map(&:type_de_champ)
evaluator.rows.times do
row_id = ULID.generate
champ_repetition.dossier.champs << types_de_champ.map do |type_de_champ|
attrs = { dossier: champ_repetition.dossier, private: champ_repetition.private?, stable_id: type_de_champ.stable_id, row_id: }
build(:"champ_do_not_use_#{type_de_champ.type_champ}", **attrs)
end
end
end
end
end
end

View file

@ -24,25 +24,13 @@ FactoryBot.define do
after(:create) do |dossier, evaluator|
if evaluator.populate_champs
dossier.revision.types_de_champ_public.each do |type_de_champ|
value = if type_de_champ.drop_down_list?
type_de_champ.drop_down_options.first
elsif type_de_champ.multiple_drop_down_list?
type_de_champ.drop_down_options.first(2).to_json
end
attrs = { stable_id: type_de_champ.stable_id, dossier:, value: }.compact
create(:"champ_do_not_use_#{type_de_champ.type_champ}", **attrs)
dossier_factory_create_champ_or_repetition(type_de_champ, dossier)
end
end
if evaluator.populate_annotations
dossier.revision.types_de_champ_private.each do |type_de_champ|
value = if type_de_champ.drop_down_list?
type_de_champ.drop_down_options.first
elsif type_de_champ.multiple_drop_down_list?
type_de_champ.drop_down_options.first(2).to_json
end
attrs = { stable_id: type_de_champ.stable_id, dossier:, private: true, value: }.compact
create(:"champ_do_not_use_#{type_de_champ.type_champ}", **attrs)
dossier_factory_create_champ_or_repetition(type_de_champ, dossier)
end
end
@ -301,3 +289,30 @@ FactoryBot.define do
end
end
end
def dossier_factory_create_champ_or_repetition(type_de_champ, dossier)
if type_de_champ.repetition?
types_de_champ = dossier.revision.children_of(type_de_champ)
2.times do
row_id = ULID.generate
type_de_champ.build_champ(dossier:, row_id:).save!
types_de_champ.each do |type_de_champ|
dossier_factory_create_champ(type_de_champ, dossier, row_id:)
end
end
else
dossier_factory_create_champ(type_de_champ, dossier)
end
end
def dossier_factory_create_champ(type_de_champ, dossier, row_id: nil)
return unless type_de_champ.fillable?
value = if type_de_champ.drop_down_list?
type_de_champ.drop_down_options.first
elsif type_de_champ.multiple_drop_down_list?
type_de_champ.drop_down_options.first(2).to_json
end
attrs = { stable_id: type_de_champ.stable_id, private: type_de_champ.private?, row_id:, dossier:, value: }.compact
create(:"champ_do_not_use_#{type_de_champ.type_champ}", **attrs)
end