From be33c0ce7bf47dcb521fb809e10b8351abb2b108 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 17 May 2022 22:04:53 +0200 Subject: [PATCH] extract coordinate_and_tdc --- app/models/procedure_revision.rb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 9445d5068..be1b03bf2 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -45,8 +45,8 @@ class ProcedureRevision < ApplicationRecord if parent_stable_id.present? clone_parent_to_draft_revision(parent_stable_id) - parent = types_de_champ.find_by(stable_id: parent_stable_id) - parent_coordinate = revision_types_de_champ.find_by(type_de_champ: parent) + + parent_coordinate, parent = coordinate_and_tdc(parent_stable_id) coordinate[:parent_id] = parent_coordinate.id coordinate[:position] = children_of(parent).count @@ -87,9 +87,7 @@ class ProcedureRevision < ApplicationRecord # Ensure that if this is a child, it's parent is cloned to the new revision clone_parent_to_draft_revision(stable_id) - coordinate = revision_types_de_champ - .joins(:type_de_champ) - .find_by(type_de_champ: { stable_id: stable_id }) + coordinate, _ = coordinate_and_tdc(stable_id) siblings = coordinate.siblings.to_a @@ -102,11 +100,7 @@ class ProcedureRevision < ApplicationRecord # Ensure that if this is a child, it's parent is cloned to the new revision clone_parent_to_draft_revision(stable_id) - coordinate = revision_types_de_champ - .joins(:type_de_champ) - .find_by(type_de_champ: { stable_id: stable_id }) - - tdc = coordinate.type_de_champ + coordinate, tdc = coordinate_and_tdc(stable_id) coordinate.destroy @@ -170,6 +164,14 @@ class ProcedureRevision < ApplicationRecord private + def coordinate_and_tdc(stable_id) + coordinate = revision_types_de_champ + .joins(:type_de_champ) + .find_by(type_de_champ: { stable_id: stable_id }) + + [coordinate, coordinate.type_de_champ] + end + def reorder(siblings) siblings.to_a.compact.each.with_index do |sibling, position| sibling.update(position: position)