From 3499f5af9ab7c70d0ca5860f7034f0bc1717ca02 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Thu, 1 Apr 2021 15:22:47 +0000 Subject: [PATCH] =?UTF-8?q?models:=20remove=20invalid=20Dossier=20?= =?UTF-8?q?=E2=86=94=EF=B8=8E=20Champ=20inverse=20relationship?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Dossier.champs` is not really an inverse of `Champs.dossier`: when a Champ record is created, it should not always be added to dossier.champs (for instance if the champ is private). NB: this breaks the workaround we added in #3907 to fix the parent dossier not being touched in some cases (the workaround was to add an inverse relationship, but we now have to remove it). The new workaround is to watch for `changed_for_autosave?` on champs. Unlike `changed?`, `changed_for_autosave?` also detects changes to attachments. This allows us to touch both `last_champ_updated_at` and `updated_at` in a single pass. --- app/controllers/users/dossiers_controller.rb | 2 +- app/models/champ.rb | 2 +- app/models/dossier.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 40fbe0f03..f03d28f19 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -365,7 +365,7 @@ module Users @dossier.champs.filter(&:repetition?).each do |champ| champ.champs = champ.champs.filter(&:persisted?) end - if @dossier.champs.any?(&:changed?) + if @dossier.champs.any?(&:changed_for_autosave?) @dossier.last_champ_updated_at = Time.zone.now end if !@dossier.save diff --git a/app/models/champ.rb b/app/models/champ.rb index 864ced9fb..8cb1d8804 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -18,7 +18,7 @@ # type_de_champ_id :integer # class Champ < ApplicationRecord - belongs_to :dossier, -> { with_discarded }, inverse_of: :champs, touch: true, optional: false + belongs_to :dossier, -> { with_discarded }, inverse_of: false, touch: true, optional: false belongs_to :type_de_champ, inverse_of: :champ, optional: false belongs_to :parent, class_name: 'Champ', optional: true has_many :commentaires diff --git a/app/models/dossier.rb b/app/models/dossier.rb index aaf93e14f..2441035f0 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -65,8 +65,8 @@ class Dossier < ApplicationRecord has_one_attached :justificatif_motivation has_one_attached :pdf_export_for_instructeur - has_many :champs, -> { root.public_ordered }, inverse_of: :dossier, dependent: :destroy - has_many :champs_private, -> { root.private_ordered }, class_name: 'Champ', inverse_of: :dossier, dependent: :destroy + has_many :champs, -> { root.public_ordered }, inverse_of: false, dependent: :destroy + has_many :champs_private, -> { root.private_ordered }, class_name: 'Champ', inverse_of: false, dependent: :destroy has_many :commentaires, inverse_of: :dossier, dependent: :destroy has_many :invites, dependent: :destroy has_many :follows, -> { active }, inverse_of: :dossier