models: explicitely save procedure's new revision

Deep-cloned objects have all their relationships stale. Thus, for a
newly deep-cloned revision, `revision.types_de_champs` returns `[]`,
even when it actually has associated types de champ.

This causes consecutive champs creations and re-ordering to fail in
subtle ways, like:

```
procedure.draft_revision.add_type_de_champ(…)
procedure.publish_revision!
procedure.draft_revision.add_type_de_champ(…)
procedure.draft_revision.move_type_de_champ(…) # this will fail
```

As `publish_revision!` created a new stale revision, moving the type
de champ fails because not all existing champs are found until the
object is refreshed.

We don't hit this path in production, because usually only a single
operation is made in a request.

To fix this, save the new revision before associating it as the draft
procedure.

(Another option would be to `reload` the revision after creation, but
this seems better contained and matches the name of the method.)
This commit is contained in:
Pierre de La Morinerie 2021-11-23 13:27:28 +01:00
parent 73faa569e8
commit e5f5440663
2 changed files with 47 additions and 1 deletions

View file

@ -678,7 +678,9 @@ class Procedure < ApplicationRecord
end
def create_new_revision
draft_revision.deep_clone(include: [:revision_types_de_champ, :revision_types_de_champ_private])
draft_revision
.deep_clone(include: [:revision_types_de_champ, :revision_types_de_champ_private])
.tap(&:save!)
end
def average_dossier_weight