test(factory): add a new way to build/create types_de_champ

This commit is contained in:
Paul Chavard 2022-07-23 12:29:18 +02:00
parent 2487cb47c0
commit ee9851091f
2 changed files with 117 additions and 10 deletions

View file

@ -21,6 +21,7 @@ FactoryBot.define do
administrateur {}
instructeurs { [] }
types_de_champ { [] }
types_de_champ_public { [] }
types_de_champ_private { [] }
updated_at { nil }
attestation_template { nil }
@ -29,8 +30,18 @@ FactoryBot.define do
after(:build) do |procedure, evaluator|
initial_revision = build(:procedure_revision, procedure: procedure, attestation_template: evaluator.attestation_template, dossier_submitted_message: evaluator.dossier_submitted_message)
if evaluator.types_de_champ_public.present?
build_types_de_champ(evaluator.types_de_champ_public, revision: initial_revision, scope: :public)
end
add_types_de_champs(evaluator.types_de_champ, to: initial_revision, scope: :public)
add_types_de_champs(evaluator.types_de_champ_private, to: initial_revision, scope: :private)
if evaluator.types_de_champ_private.present?
if evaluator.types_de_champ_private.first.is_a?(Hash)
build_types_de_champ(evaluator.types_de_champ_private, revision: initial_revision, scope: :private)
else
add_types_de_champs(evaluator.types_de_champ_private, to: initial_revision, scope: :private)
end
end
if procedure.brouillon?
procedure.draft_revision = initial_revision
@ -41,6 +52,14 @@ FactoryBot.define do
end
end
before(:create) do |procedure, _evaluator|
procedure.revisions.each do |revision|
revision.association(:types_de_champ).reset
revision.association(:types_de_champ_public).reset
revision.association(:types_de_champ_private).reset
end
end
after(:create) do |procedure, evaluator|
evaluator.instructeurs.each { |i| i.assign_to_procedure(procedure) }
@ -349,6 +368,75 @@ FactoryBot.define do
end
end
def build_types_de_champ(types_de_champ, revision:, scope: :public, parent: nil)
types_de_champ.deep_dup.each.with_index do |type_de_champ_attributes, i|
type = TypeDeChamp.type_champs.fetch(type_de_champ_attributes.delete(:type) || :text)
position = type_de_champ_attributes.delete(:position) || i
children = type_de_champ_attributes.delete(:children)
options = type_de_champ_attributes.delete(:options)
layers = type_de_champ_attributes.delete(:layers)
if options.present?
if type == :drop_down_list
type_de_champ_attributes[:drop_down_other] = options.delete(:other).present?
end
if type.in?([:drop_down_list, :multiple_drop_down_list, :linked_drop_down_list])
type_de_champ_attributes[:drop_down_options] = options
end
end
if type == :linked_drop_down_list
type_de_champ_attributes[:drop_down_secondary_libelle] = type_de_champ_attributes.delete(:secondary_libelle)
type_de_champ_attributes[:drop_down_secondary_description] = type_de_champ_attributes.delete(:secondary_description)
end
if type == :carte && layers.present?
type_de_champ_attributes[:editable_options] = layers.index_with { '1' }
end
type_de_champ = if scope == :private
build(:"type_de_champ_#{type}", :private, no_coordinate: true, **type_de_champ_attributes)
else
build(:"type_de_champ_#{type}", no_coordinate: true, **type_de_champ_attributes)
end
coordinate = build(:procedure_revision_type_de_champ,
revision: revision,
type_de_champ: type_de_champ,
position: position,
parent: parent)
revision.association(:revision_types_de_champ).target << coordinate
if parent.present?
parent.association(:revision_types_de_champ).target << coordinate
end
if type_de_champ.repetition? && children.present?
build_types_de_champ(children, revision: revision, scope: scope, parent: coordinate)
end
end
if parent.present?
parent.association(:revision_types_de_champ).target.sort_by!(&:position)
else
revision_types_de_champ_private, revision_types_de_champ_public = revision.revision_types_de_champ.partition(&:private?)
root_revision_types_de_champ_public, child_revision_types_de_champ_public = revision_types_de_champ_public.partition { |coordinate| coordinate.parent.nil? }
root_revision_types_de_champ_private, child_revision_types_de_champ_private = revision_types_de_champ_private.partition { |coordinate| coordinate.parent.nil? }
revision.association(:revision_types_de_champ_public).target = root_revision_types_de_champ_public.sort_by(&:position)
revision.association(:revision_types_de_champ_private).target = root_revision_types_de_champ_private.sort_by(&:position)
revision.association(:revision_types_de_champ).target = revision.revision_types_de_champ_public +
revision.revision_types_de_champ_private +
child_revision_types_de_champ_public.sort_by(&:parent).sort_by(&:position) +
child_revision_types_de_champ_private.sort_by(&:parent).sort_by(&:position)
revision.association(:types_de_champ).target = revision.revision_types_de_champ.map(&:type_de_champ)
revision.association(:types_de_champ_public).target = revision.revision_types_de_champ_public.map(&:type_de_champ)
revision.association(:types_de_champ_private).target = revision.revision_types_de_champ_private.map(&:type_de_champ)
end
end
def add_types_de_champs(types_de_champ, to: nil, scope: :public)
revision = to
association_name = scope == :private ? :revision_types_de_champ_private : :revision_types_de_champ_public

View file

@ -6,17 +6,36 @@ FactoryBot.define do
after(:build) do |revision, evaluator|
if evaluator.from_original
original = evaluator.from_original
from_revision = evaluator.from_original
revision.procedure = original.procedure
revision.attestation_template_id = original.attestation_template_id
revision.dossier_submitted_message_id = original.dossier_submitted_message_id
original.revision_types_de_champ_public.each do |r_tdc|
revision.revision_types_de_champ_public << build(:procedure_revision_type_de_champ, from_original: r_tdc)
end
original.revision_types_de_champ_private.each do |r_tdc|
revision.revision_types_de_champ_private << build(:procedure_revision_type_de_champ, from_original: r_tdc)
revision.procedure = from_revision.procedure
revision.attestation_template_id = from_revision.attestation_template_id
revision.dossier_submitted_message_id = from_revision.dossier_submitted_message_id
coordinate_map = {}
revision.association(:revision_types_de_champ).target = from_revision.revision_types_de_champ.map do |from_coordinate|
parent = from_coordinate.parent.present? ? coordinate_map[from_coordinate.parent] : nil
coordinate = build(:procedure_revision_type_de_champ,
revision: revision,
type_de_champ: from_coordinate.type_de_champ,
position: from_coordinate.position,
parent: parent)
if parent.present?
parent.association(:revision_types_de_champ).target << coordinate
elsif from_coordinate.private?
revision.association(:revision_types_de_champ_private).target << coordinate
else
revision.association(:revision_types_de_champ_public).target << coordinate
end
coordinate_map[from_coordinate] = coordinate
coordinate
end
revision.association(:types_de_champ).target = revision.revision_types_de_champ.map(&:type_de_champ)
revision.association(:types_de_champ_public).target = revision.revision_types_de_champ_public.map(&:type_de_champ)
revision.association(:types_de_champ_private).target = revision.revision_types_de_champ_private.map(&:type_de_champ)
end
end
end