Skip to content

Commit

Permalink
poc(spec): renumber only at one point
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin committed Jan 18, 2024
1 parent 832fc2b commit 88bbd6f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 7 additions & 3 deletions app/models/procedure_revision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def find_and_ensure_exclusive_use(stable_id)
end
end

# []
def move_type_de_champ(stable_id, position)
coordinate, _ = coordinate_and_tdc(stable_id)
siblings = coordinate.siblings
Expand All @@ -89,6 +88,12 @@ def move_type_de_champ(stable_id, position)
coordinate
end

def renumber(siblings)
siblings.to_a.compact.each.with_index do |sibling, position|
sibling.update_column(:position, position)
end
end

def remove_type_de_champ(stable_id)
coordinate, tdc = coordinate_and_tdc(stable_id)

Expand All @@ -101,12 +106,11 @@ def remove_type_de_champ(stable_id)
children.each(&:destroy_if_orphan)
tdc.destroy_if_orphan

coordinate.siblings.where("position >= ?", coordinate.position).update_all("position = position - 1")

# they are not aware of the removal
types_de_champ_public.reset
types_de_champ_private.reset

coordinate.siblings.where("position >= ?", coordinate.position).update_all(position: "position - 1")
coordinate
end

Expand Down
3 changes: 2 additions & 1 deletion spec/models/dossier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1931,7 +1931,8 @@
procedure.publish!
dossier
procedure.draft_revision.remove_type_de_champ(text_type_de_champ.stable_id)
procedure.draft_revision.add_type_de_champ(type_champ: TypeDeChamp.type_champs.fetch(:text), libelle: 'New text field')
coordinate = procedure.draft_revision.add_type_de_champ(type_champ: TypeDeChamp.type_champs.fetch(:text), libelle: 'New text field')
procedure.draft_revision.renumber(coordinate.revision_type_de_champ.siblings)
procedure.draft_revision.find_and_ensure_exclusive_use(yes_no_type_de_champ.stable_id).update(libelle: 'Updated yes/no')
procedure.draft_revision.find_and_ensure_exclusive_use(commune_type_de_champ.stable_id).update(libelle: 'Commune de naissance')
procedure.draft_revision.find_and_ensure_exclusive_use(repetition_type_de_champ.stable_id).update(libelle: 'Repetition')
Expand Down

0 comments on commit 88bbd6f

Please sign in to comment.