refactor(dossier): fix champ repetition factories

This commit is contained in:
Paul Chavard 2022-05-18 10:52:25 +02:00
parent 6c2a3bafa4
commit cdd597db19
3 changed files with 63 additions and 31 deletions

View file

@ -215,24 +215,40 @@ FactoryBot.define do
end
after(:build) do |champ_repetition, evaluator|
types_de_champ = champ_repetition.type_de_champ.types_de_champ
existing_type_de_champ_text = types_de_champ.find { |tdc| tdc.libelle == 'Nom' }
type_de_champ_text = existing_type_de_champ_text || build(
:type_de_champ_text,
position: 0,
parent: champ_repetition.type_de_champ,
libelle: 'Nom'
)
revision = champ_repetition.type_de_champ.procedure&.active_revision || build(:procedure_revision)
parent = revision.revision_types_de_champ.find { |rtdc| rtdc.type_de_champ == champ_repetition.type_de_champ }
types_de_champ = revision.revision_types_de_champ.filter { |rtdc| rtdc.parent == parent }.map(&:type_de_champ)
existing_type_de_champ_number = types_de_champ.find { |tdc| tdc.libelle == 'Age' }
type_de_champ_number = existing_type_de_champ_number || build(
:type_de_champ_number,
position: 1,
parent: champ_repetition.type_de_champ,
libelle: 'Age'
)
type_de_champ_text = types_de_champ.find { |tdc| tdc.libelle == 'Nom' }
if !type_de_champ_text
type_de_champ_text = build(:type_de_champ_text,
position: 0,
parent: champ_repetition.type_de_champ,
libelle: 'Nom')
revision.revision_types_de_champ << build(:procedure_revision_type_de_champ,
revision: revision,
type_de_champ: type_de_champ_text,
parent: parent,
position: 0)
champ_repetition.type_de_champ.types_de_champ << type_de_champ_text
end
type_de_champ_number = types_de_champ.find { |tdc| tdc.libelle == 'Age' }
if !type_de_champ_number
type_de_champ_number = build(:type_de_champ_number,
position: 1,
parent: champ_repetition.type_de_champ,
libelle: 'Age')
revision.revision_types_de_champ << build(:procedure_revision_type_de_champ,
revision: revision,
type_de_champ: type_de_champ_number,
parent: parent,
position: 1)
champ_repetition.type_de_champ.types_de_champ << type_de_champ_number
end
champ_repetition.type_de_champ.types_de_champ << [type_de_champ_text, type_de_champ_number]
evaluator.rows.times do |row|
champ_repetition.champs << [
build(:champ_text, dossier: champ_repetition.dossier, row: row, type_de_champ: type_de_champ_text, parent: champ_repetition),

View file

@ -176,24 +176,37 @@ FactoryBot.define do
end
after(:build) do |type_de_champ_repetition, evaluator|
evaluator.procedure&.save!
evaluator.types_de_champ.each do |type_de_champ|
type_de_champ.order_place = type_de_champ_repetition.types_de_champ.size
revision = evaluator.procedure&.active_revision || build(:procedure_revision)
parent = revision.revision_types_de_champ.find { |rtdc| rtdc.type_de_champ == type_de_champ_repetition }
types_de_champ = revision.revision_types_de_champ.filter { |rtdc| rtdc.parent == parent }
position = types_de_champ.size
revision.revision_types_de_champ << build(:procedure_revision_type_de_champ,
revision: revision,
type_de_champ: type_de_champ,
parent: parent,
position: position)
# old system
type_de_champ.order_place = position
type_de_champ_repetition.types_de_champ << type_de_champ
end
end
trait :with_types_de_champ do
after(:build) do |type_de_champ, evaluator|
tdc = build(:type_de_champ, libelle: 'sub type de champ', parent: type_de_champ)
after(:build) do |type_de_champ_repetition, evaluator|
type_de_champ = build(:type_de_champ, libelle: 'sub type de champ', parent: type_de_champ_repetition)
revision = evaluator.procedure.active_revision
parent = revision.revision_types_de_champ.find { |rtdc| rtdc.type_de_champ == type_de_champ_repetition }
evaluator.procedure.save
ProcedureRevisionTypeDeChamp.create!(
revision_id: evaluator.procedure.active_revision.id,
type_de_champ_id: tdc.id,
parent_id: tdc.parent.revision_type_de_champ.id,
position: 0
)
evaluator.procedure.save!
revision.revision_types_de_champ << build(:procedure_revision_type_de_champ,
revision: revision,
type_de_champ: type_de_champ,
parent: parent,
position: 0)
end
end
end