demarches-normaliennes/spec/factories/procedure_revision.rb

43 lines
1.7 KiB
Ruby
Raw Normal View History

2020-08-27 19:55:37 +02:00
FactoryBot.define do
factory :procedure_revision do
transient do
from_original { nil }
end
after(:build) do |revision, evaluator|
if evaluator.from_original
from_revision = evaluator.from_original
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
2020-08-27 19:55:37 +02:00
end
end