Merge pull request #8897 from tchak/fix-dossier-champs-destroy

fix(dossier): delete all champs starting with children
This commit is contained in:
Paul Chavard 2023-04-13 09:16:51 +00:00 committed by GitHub
commit 309b680adc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 12 deletions

View file

@ -1,7 +1,7 @@
module Administrateurs module Administrateurs
class TypesDeChampController < AdministrateurController class TypesDeChampController < AdministrateurController
before_action :retrieve_procedure before_action :retrieve_procedure
after_action :reset_procedure, only: [:create, :update, :destroy] after_action :reset_procedure, only: [:create, :update, :destroy, :piece_justificative_template]
def create def create
type_de_champ = draft.add_type_de_champ(type_de_champ_create_params) type_de_champ = draft.add_type_de_champ(type_de_champ_create_params)

View file

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

View file

@ -86,8 +86,11 @@ class Dossier < ApplicationRecord
has_one_attached :justificatif_motivation has_one_attached :justificatif_motivation
has_many :champs has_many :champs
has_many :champs_public, -> { root.public_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
has_many :champs_private, -> { root.private_ordered }, class_name: 'Champ', inverse_of: false, dependent: :destroy # 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_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 :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 has_many :prefilled_champs_public, -> { root.public_only.prefilled }, class_name: 'Champ', inverse_of: false

View file

@ -797,6 +797,7 @@ class Procedure < ApplicationRecord
end end
def publish_revision! def publish_revision!
reset!
transaction do transaction do
self.published_revision = draft_revision self.published_revision = draft_revision
self.draft_revision = create_new_revision self.draft_revision = create_new_revision
@ -810,8 +811,8 @@ class Procedure < ApplicationRecord
def reset_draft_revision! def reset_draft_revision!
if published_revision.present? && draft_changed? if published_revision.present? && draft_changed?
reset!
transaction do transaction do
reset!
draft_revision.types_de_champ.filter(&:only_present_on_draft?).each(&:destroy) draft_revision.types_de_champ.filter(&:only_present_on_draft?).each(&:destroy)
draft_revision.update(dossier_submitted_message: nil) draft_revision.update(dossier_submitted_message: nil)
draft_revision.destroy draft_revision.destroy

View file

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

View file

@ -1,5 +1,5 @@
describe ProcedurePresentation do 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(:instructeur) { create(:instructeur) }
let(:assign_to) { create(:assign_to, procedure: procedure, instructeur: 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 } let(:first_type_de_champ) { assign_to.procedure.active_revision.types_de_champ_public.first }