diff --git a/app/controllers/administrateurs/conditions_controller.rb b/app/controllers/administrateurs/conditions_controller.rb index e517b40bb..ba1e77cfa 100644 --- a/app/controllers/administrateurs/conditions_controller.rb +++ b/app/controllers/administrateurs/conditions_controller.rb @@ -55,9 +55,7 @@ module Administrateurs def retrieve_coordinate_and_uppers @tdc = draft_revision.find_and_ensure_exclusive_use(params[:stable_id]) @coordinate = draft_revision.coordinate_for(@tdc) - @upper_tdcs = draft_revision - .upper_coordinates(@coordinate.position) - .map(&:type_de_champ) + @upper_tdcs = @coordinate.upper_siblings.map(&:type_de_champ) end def draft_revision diff --git a/app/controllers/administrateurs/types_de_champ_controller.rb b/app/controllers/administrateurs/types_de_champ_controller.rb index dc7501e81..25373dc9e 100644 --- a/app/controllers/administrateurs/types_de_champ_controller.rb +++ b/app/controllers/administrateurs/types_de_champ_controller.rb @@ -8,7 +8,7 @@ module Administrateurs if type_de_champ.valid? @coordinate = draft.coordinate_for(type_de_champ) @created = champ_component_from(@coordinate, focused: true) - @morphed = champ_components_starting_at(@coordinate.position + 1) + @morphed = champ_components_starting_at(@coordinate, 1) reset_procedure flash.notice = "Formulaire enregistré" @@ -22,7 +22,7 @@ module Administrateurs if type_de_champ.update(type_de_champ_update_params) @coordinate = draft.coordinate_for(type_de_champ) - @morphed = champ_components_starting_at(@coordinate.position) + @morphed = champ_components_starting_at(@coordinate) reset_procedure flash.notice = "Formulaire enregistré" @@ -42,7 +42,7 @@ module Administrateurs @destroyed = @coordinate @created = champ_component_from(@coordinate) # update the one component below - @morphed = champ_components_starting_at(@coordinate.position + 1).take(1) + @morphed = champ_components_starting_at(@coordinate, 1).take(1) end def move_down @@ -51,7 +51,7 @@ module Administrateurs @destroyed = @coordinate @created = champ_component_from(@coordinate) # update the one component above - @morphed = champ_components_starting_at(@coordinate.position - 1).take(1) + @morphed = champ_components_starting_at(@coordinate, - 1).take(1) end def destroy @@ -60,14 +60,14 @@ module Administrateurs flash.notice = "Formulaire enregistré" @destroyed = @coordinate - @morphed = champ_components_starting_at(@coordinate.position) + @morphed = champ_components_starting_at(@coordinate) end private - def champ_components_starting_at(position) - draft - .coordinates_starting_at(position) + def champ_components_starting_at(coordinate, offset = 0) + coordinate + .siblings_starting_at(offset) .lazy .map { |c| champ_component_from(c) } end @@ -75,7 +75,7 @@ module Administrateurs def champ_component_from(coordinate, focused: false) TypesDeChampEditor::ChampComponent.new( coordinate: coordinate, - upper_coordinates: draft.upper_coordinates(coordinate.position), + upper_coordinates: coordinate.upper_siblings, focused: focused ) end diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 59070bcca..c9c9c254f 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -197,14 +197,6 @@ class ProcedureRevision < ApplicationRecord revision_types_de_champ.find_by!(type_de_champ: tdc) end - def upper_coordinates(position) - revision_types_de_champ_public.filter { |c| c.position < position } - end - - def coordinates_starting_at(position) - revision_types_de_champ_public.reload.filter { |c| position <= c.position } - end - private def compute_estimated_fill_duration diff --git a/app/models/procedure_revision_type_de_champ.rb b/app/models/procedure_revision_type_de_champ.rb index 58f522929..7ebe63d97 100644 --- a/app/models/procedure_revision_type_de_champ.rb +++ b/app/models/procedure_revision_type_de_champ.rb @@ -47,6 +47,14 @@ class ProcedureRevisionTypeDeChamp < ApplicationRecord end end + def upper_siblings + siblings.filter { |s| s.position < position } + end + + def siblings_starting_at(offset) + siblings.filter { |s| (position + offset) <= s.position } + end + def previous_sibling index = siblings.index(self) if index > 0