models: remove invalid Dossier ↔︎ Champ inverse relationship

`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.
This commit is contained in:
Pierre de La Morinerie 2021-04-01 15:22:47 +00:00
parent 6eadcf614d
commit 3499f5af9a
3 changed files with 4 additions and 4 deletions

View file

@ -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

View file

@ -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

View file

@ -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