demarches-normaliennes/spec/factories/procedure_revision.rb
Pierre de La Morinerie 970c3e4b2b specs: avoid executing business logic in Procedure factory
Calling business logic in a factory is a code-smell, because it
usually requires the object to be saved into database, and may have
unintended consequences when the business logic is changed.

Also, this allows to just build a published procedure, without saving it
to the database.
2021-11-23 08:07:07 +01:00

21 lines
676 B
Ruby

FactoryBot.define do
factory :procedure_revision do
transient do
from_original { nil }
end
after(:build) do |revision, evaluator|
if evaluator.from_original
original = evaluator.from_original
revision.procedure = original.procedure
original.revision_types_de_champ.each do |r_tdc|
revision.revision_types_de_champ << 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)
end
end
end
end
end