Update factories to use revisions

This commit is contained in:
Paul Chavard 2020-08-27 19:55:37 +02:00
parent ec72fdd164
commit d1fb6c559e
6 changed files with 116 additions and 65 deletions

View file

@ -161,23 +161,20 @@ FactoryBot.define do
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,
order_place: 0,
procedure: champ_repetition.dossier.procedure,
position: 0,
parent: champ_repetition.type_de_champ,
libelle: 'Nom'
)
types_de_champ << type_de_champ_text
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,
order_place: 1,
procedure: champ_repetition.dossier.procedure,
position: 1,
parent: champ_repetition.type_de_champ,
libelle: 'Age'
)
types_de_champ << type_de_champ_number
champ_repetition.type_de_champ.types_de_champ << [type_de_champ_text, type_de_champ_number]
champ_repetition.champs << [
build(:champ_text, dossier: champ_repetition.dossier, row: 0, type_de_champ: type_de_champ_text, parent: champ_repetition),
build(:champ_number, dossier: champ_repetition.dossier, row: 0, type_de_champ: type_de_champ_number, parent: champ_repetition),
@ -198,13 +195,11 @@ FactoryBot.define do
after(:build) do |champ_repetition, _evaluator|
type_de_champ_pj0 = build(:type_de_champ_piece_justificative,
procedure: champ_repetition.dossier.procedure,
order_place: 0,
position: 0,
parent: champ_repetition.type_de_champ,
libelle: 'Justificatif de domicile')
type_de_champ_pj1 = build(:type_de_champ_piece_justificative,
procedure: champ_repetition.dossier.procedure,
order_place: 1,
position: 1,
parent: champ_repetition.type_de_champ,
libelle: 'Carte d\'identité')

View file

@ -15,6 +15,8 @@ FactoryBot.define do
procedure = create(:procedure, :published, :with_type_de_champ, :with_type_de_champ_private)
end
dossier.revision = procedure.active_revision
# Assign the procedure to the dossier through the groupe_instructeur
if dossier.groupe_instructeur.nil?
dossier.groupe_instructeur = procedure.defaut_groupe_instructeur
@ -71,7 +73,7 @@ FactoryBot.define do
linked_dossier = create(:dossier, :en_construction)
# find first type de champ dossier_link
type_de_champ = dossier.procedure.types_de_champ.find do |t|
type_de_champ = dossier.types_de_champ.find do |t|
t.type_champ == TypeDeChamp.type_champs.fetch(:dossier_link)
end
@ -202,7 +204,7 @@ FactoryBot.define do
trait :with_all_champs do
after(:create) do |dossier, _evaluator|
dossier.champs = dossier.procedure.types_de_champ.map do |type_de_champ|
dossier.champs = dossier.types_de_champ.map do |type_de_champ|
build(:"champ_#{type_de_champ.type_champ}", dossier: dossier, type_de_champ: type_de_champ)
end
dossier.save!
@ -211,7 +213,7 @@ FactoryBot.define do
trait :with_all_annotations do
after(:create) do |dossier, _evaluator|
dossier.champs = dossier.procedure.types_de_champ.map do |type_de_champ|
dossier.champs = dossier.types_de_champ.map do |type_de_champ|
build(:"champ_#{type_de_champ.type_champ}", dossier: dossier, type_de_champ: type_de_champ)
end
dossier.save!

View file

@ -16,6 +16,9 @@ FactoryBot.define do
transient do
administrateur { }
instructeurs { [] }
types_de_champ { [] }
types_de_champ_private { [] }
updated_at { nil }
end
after(:build) do |procedure, evaluator|
@ -24,10 +27,35 @@ FactoryBot.define do
elsif procedure.administrateurs.empty?
procedure.administrateurs = [create(:administrateur)]
end
procedure.draft_revision = build(:procedure_revision, procedure: procedure)
evaluator.types_de_champ.each do |type_de_champ|
type_de_champ.revision = procedure.draft_revision
type_de_champ.private = false
type_de_champ.revision.revision_types_de_champ << build(:procedure_revision_type_de_champ,
revision: procedure.draft_revision,
position: type_de_champ.order_place,
type_de_champ: type_de_champ)
end
evaluator.types_de_champ_private.each do |type_de_champ|
type_de_champ.revision = procedure.draft_revision
type_de_champ.private = true
type_de_champ.revision.revision_types_de_champ_private << build(:procedure_revision_type_de_champ,
revision: procedure.draft_revision,
position: type_de_champ.order_place,
type_de_champ: type_de_champ)
end
end
after(:create) do |procedure, evaluator|
evaluator.instructeurs.each { |i| i.assign_to_procedure(procedure) }
if evaluator.updated_at
procedure.update_column(:updated_at, evaluator.updated_at)
end
procedure.reload
end
factory :procedure_with_dossiers do
@ -38,9 +66,7 @@ FactoryBot.define do
after(:create) do |procedure, evaluator|
user = create(:user)
evaluator.dossiers_count.times do
dossier = procedure.new_dossier
dossier.user = user
dossier.save!
create(:dossier, procedure: procedure, user: user)
end
end
end
@ -48,7 +74,7 @@ FactoryBot.define do
factory :simple_procedure do
after(:build) do |procedure, _evaluator|
procedure.for_individual = true
procedure.types_de_champ << build(:type_de_champ, libelle: 'Texte obligatoire', mandatory: true)
build(:type_de_champ, libelle: 'Texte obligatoire', mandatory: true, procedure: procedure)
procedure.path = generate(:published_path)
procedure.publish!
end
@ -96,10 +122,8 @@ FactoryBot.define do
end
after(:build) do |procedure, evaluator|
evaluator.types_de_champ_count.times do
type_de_champ = build(:type_de_champ)
procedure.types_de_champ << type_de_champ
evaluator.types_de_champ_count.times do |position|
build(:type_de_champ, procedure: procedure, position: position)
end
end
end
@ -110,68 +134,51 @@ FactoryBot.define do
end
after(:build) do |procedure, evaluator|
evaluator.types_de_champ_private_count.times do
type_de_champ = build(:type_de_champ, :private)
procedure.types_de_champ_private << type_de_champ
evaluator.types_de_champ_private_count.times do |position|
build(:type_de_champ, :private, procedure: procedure, position: position)
end
end
end
trait :with_type_de_champ_mandatory do
after(:build) do |procedure, _evaluator|
type_de_champ = build(:type_de_champ, mandatory: true)
procedure.types_de_champ << type_de_champ
build(:type_de_champ, mandatory: true, procedure: procedure)
end
end
trait :with_datetime do
after(:build) do |procedure, _evaluator|
type_de_champ = build(:type_de_champ_datetime, mandatory: true)
procedure.types_de_champ << type_de_champ
build(:type_de_champ_datetime, mandatory: true, procedure: procedure)
end
end
trait :with_dossier_link do
after(:build) do |procedure, _evaluator|
type_de_champ = build(:type_de_champ_dossier_link)
procedure.types_de_champ << type_de_champ
build(:type_de_champ_dossier_link, procedure: procedure)
end
end
trait :with_yes_no do
after(:build) do |procedure, _evaluator|
type_de_champ = build(:type_de_champ_yes_no)
procedure.types_de_champ << type_de_champ
build(:type_de_champ_yes_no, procedure: procedure)
end
end
trait :with_piece_justificative do
after(:build) do |procedure, _evaluator|
type_de_champ = build(:type_de_champ_piece_justificative)
procedure.types_de_champ << type_de_champ
build(:type_de_champ_piece_justificative, procedure: procedure)
end
end
trait :with_repetition do
after(:build) do |procedure, _evaluator|
type_de_champ = build(:type_de_champ_repetition)
procedure.types_de_champ << type_de_champ
type_de_champ.types_de_champ << build(:type_de_champ, libelle: 'sub type de champ')
build(:type_de_champ_repetition, :with_types_de_champ, procedure: procedure)
end
end
trait :with_number do
after(:build) do |procedure, _evaluator|
type_de_champ = build(:type_de_champ_number)
procedure.types_de_champ << type_de_champ
build(:type_de_champ_number, procedure: procedure)
end
end
@ -228,35 +235,35 @@ FactoryBot.define do
trait :with_all_champs_mandatory do
after(:build) do |procedure, _evaluator|
procedure.types_de_champ = TypeDeChamp.type_champs.map.with_index do |(libelle, type_champ), index|
TypeDeChamp.type_champs.map.with_index do |(libelle, type_champ), index|
if libelle == 'drop_down_list'
libelle = 'simple_drop_down_list'
end
build(:"type_de_champ_#{type_champ}", procedure: procedure, mandatory: true, libelle: libelle, order_place: index)
build(:"type_de_champ_#{type_champ}", procedure: procedure, mandatory: true, libelle: libelle, position: index)
end
procedure.types_de_champ << build(:type_de_champ_drop_down_list, :long, procedure: procedure, mandatory: true, libelle: 'simple_choice_drop_down_list_long')
procedure.types_de_champ << build(:type_de_champ_multiple_drop_down_list, :long, procedure: procedure, mandatory: true, libelle: 'multiple_choice_drop_down_list_long')
build(:type_de_champ_drop_down_list, :long, procedure: procedure, mandatory: true, libelle: 'simple_choice_drop_down_list_long', position: TypeDeChamp.type_champs.size)
build(:type_de_champ_multiple_drop_down_list, :long, procedure: procedure, mandatory: true, libelle: 'multiple_choice_drop_down_list_long', position: TypeDeChamp.type_champs.size + 1)
end
end
trait :with_all_champs do
after(:build) do |procedure, _evaluator|
procedure.types_de_champ = TypeDeChamp.type_champs.map.with_index do |(libelle, type_champ), index|
TypeDeChamp.type_champs.map.with_index do |(libelle, type_champ), index|
if libelle == 'drop_down_list'
libelle = 'simple_drop_down_list'
end
build(:"type_de_champ_#{type_champ}", procedure: procedure, libelle: libelle, order_place: index)
build(:"type_de_champ_#{type_champ}", procedure: procedure, libelle: libelle, position: index)
end
end
end
trait :with_all_annotations do
after(:build) do |procedure, _evaluator|
procedure.types_de_champ_private = TypeDeChamp.type_champs.map.with_index do |(libelle, type_champ), index|
TypeDeChamp.type_champs.map.with_index do |(libelle, type_champ), index|
if libelle == 'drop_down_list'
libelle = 'simple_drop_down_list'
end
build(:"type_de_champ_#{type_champ}", procedure: procedure, private: true, libelle: libelle, order_place: index)
build(:"type_de_champ_#{type_champ}", procedure: procedure, private: true, libelle: libelle, position: index)
end
end
end

View file

@ -0,0 +1,4 @@
FactoryBot.define do
factory :procedure_revision do
end
end

View file

@ -0,0 +1,4 @@
FactoryBot.define do
factory :procedure_revision_type_de_champ do
end
end

View file

@ -7,7 +7,40 @@ FactoryBot.define do
mandatory { false }
add_attribute(:private) { false }
association :procedure
transient do
procedure { nil }
position { nil }
parent { nil }
end
after(:build) do |type_de_champ, evaluator|
if evaluator.procedure
type_de_champ.revision = evaluator.procedure.active_revision
build(:procedure_revision_type_de_champ,
position: evaluator.position,
revision: evaluator.procedure.active_revision,
type_de_champ: type_de_champ)
if type_de_champ.private?
type_de_champ.revision.types_de_champ_private << type_de_champ
else
type_de_champ.revision.types_de_champ << type_de_champ
end
elsif evaluator.parent
type_de_champ.revision = evaluator.parent.revision
type_de_champ.order_place = evaluator.position || evaluator.parent.types_de_champ.size
evaluator.parent.types_de_champ << type_de_champ
else
type_de_champ.order_place = evaluator.position
end
end
trait :private do
add_attribute(:private) { true }
sequence(:libelle) { |n| "Libelle champ privé #{n}" }
sequence(:description) { |n| "description du champ privé #{n}" }
end
factory :type_de_champ_text do
type_champ { TypeDeChamp.type_champs.fetch(:text) }
@ -96,8 +129,8 @@ FactoryBot.define do
factory :type_de_champ_piece_justificative do
type_champ { TypeDeChamp.type_champs.fetch(:piece_justificative) }
after(:build) do |tc, _evaluator|
tc.piece_justificative_template.attach(io: StringIO.new("toto"), filename: "toto.txt", content_type: "text/plain")
after(:build) do |type_de_champ, _evaluator|
type_de_champ.piece_justificative_template.attach(io: StringIO.new("toto"), filename: "toto.txt", content_type: "text/plain")
end
end
factory :type_de_champ_siret do
@ -109,17 +142,23 @@ FactoryBot.define do
factory :type_de_champ_repetition do
type_champ { TypeDeChamp.type_champs.fetch(:repetition) }
transient do
types_de_champ { [] }
end
after(:build) do |type_de_champ_repetition, evaluator|
evaluator.types_de_champ.each do |type_de_champ|
type_de_champ.revision = type_de_champ_repetition.revision
type_de_champ.order_place = type_de_champ_repetition.types_de_champ.size
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|
type_de_champ.types_de_champ << build(:type_de_champ, procedure: type_de_champ.procedure, libelle: 'sub type de champ')
build(:type_de_champ, libelle: 'sub type de champ', parent: type_de_champ)
end
end
end
trait :private do
add_attribute(:private) { true }
sequence(:libelle) { |n| "Libelle champ privé #{n}" }
sequence(:description) { |n| "description du champ privé #{n}" }
end
end
end