fix(dossier): delete all champs starting with children

This commit is contained in:
Paul Chavard 2023-04-11 17:00:25 +02:00
parent 7054c2d9ef
commit 21b548d32b
4 changed files with 15 additions and 10 deletions

View file

@ -32,7 +32,7 @@ class Champ < ApplicationRecord
# here because otherwise we can't easily use includes in our queries.
has_many :geo_areas, -> { order(:created_at) }, dependent: :destroy, inverse_of: :champ
belongs_to :etablissement, optional: true, dependent: :destroy
has_many :champs, -> { ordered }, foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
has_many :champs, -> { ordered }, foreign_key: :parent_id, inverse_of: :parent
delegate :procedure, to: :dossier

View file

@ -86,8 +86,11 @@ class Dossier < ApplicationRecord
has_one_attached :justificatif_motivation
has_many :champs
has_many :champs_public, -> { root.public_ordered }, class_name: 'Champ', inverse_of: false, dependent: :destroy
has_many :champs_private, -> { root.private_ordered }, class_name: 'Champ', inverse_of: false, dependent: :destroy
# We have to remove champs in a particular order - champs with a reference to a parent have to be
# removed first, otherwise we get a foreign key constraint error.
has_many :champs_to_destroy, -> { order(:parent_id) }, class_name: 'Champ', inverse_of: false, dependent: :destroy
has_many :champs_public, -> { root.public_ordered }, class_name: 'Champ', inverse_of: false
has_many :champs_private, -> { root.private_ordered }, class_name: 'Champ', inverse_of: false
has_many :champs_public_all, -> { public_only }, class_name: 'Champ', inverse_of: false
has_many :champs_private_all, -> { private_only }, class_name: 'Champ', inverse_of: false
has_many :prefilled_champs_public, -> { root.public_only.prefilled }, class_name: 'Champ', inverse_of: false

View file

@ -241,19 +241,21 @@ FactoryBot.define do
trait :with_populated_champs do
after(:create) do |dossier, _evaluator|
dossier.champs_public = dossier.types_de_champ.map do |type_de_champ|
build(:"champ_#{type_de_champ.type_champ}", dossier: dossier, type_de_champ: type_de_champ)
dossier.champs_to_destroy.where(private: false).destroy_all
dossier.types_de_champ.each do |type_de_champ|
create(:"champ_#{type_de_champ.type_champ}", dossier:, type_de_champ:)
end
dossier.save!
dossier.reload
end
end
trait :with_populated_annotations do
after(:create) do |dossier, _evaluator|
dossier.champs_private = dossier.types_de_champ_private.map do |type_de_champ|
build(:"champ_#{type_de_champ.type_champ}", private: true, dossier: dossier, type_de_champ: type_de_champ)
dossier.champs_to_destroy.where(private: true).destroy_all
dossier.types_de_champ_private.each do |type_de_champ|
create(:"champ_#{type_de_champ.type_champ}", private: true, dossier:, type_de_champ:)
end
dossier.save!
dossier.reload
end
end

View file

@ -1,5 +1,5 @@
describe ProcedurePresentation do
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private) }
let(:procedure) { create(:procedure, :published, types_de_champ_public: [{}], types_de_champ_private: [{}]) }
let(:instructeur) { create(:instructeur) }
let(:assign_to) { create(:assign_to, procedure: procedure, instructeur: instructeur) }
let(:first_type_de_champ) { assign_to.procedure.active_revision.types_de_champ_public.first }