feat(routing): server side: prevent tdc deletion if used for routing
This commit is contained in:
parent
3f55fddcef
commit
c1f67499aa
3 changed files with 59 additions and 36 deletions
|
@ -67,12 +67,20 @@ module Administrateurs
|
|||
end
|
||||
|
||||
def destroy
|
||||
@coordinate = draft.remove_type_de_champ(params[:stable_id])
|
||||
flash.notice = "Formulaire enregistré"
|
||||
coordinate, type_de_champ = draft.coordinate_and_tdc(params[:stable_id])
|
||||
|
||||
if @coordinate.present?
|
||||
@destroyed = @coordinate
|
||||
@morphed = champ_components_starting_at(@coordinate)
|
||||
if coordinate.used_by_routing_rules?
|
||||
errors = "« #{type_de_champ.libelle} » est utilisé pour le routage, vous ne pouvez pas le supprimer."
|
||||
@morphed = [champ_component_from(coordinate, focused: false, errors:)]
|
||||
flash.alert = errors
|
||||
else
|
||||
@coordinate = draft.remove_type_de_champ(params[:stable_id])
|
||||
flash.notice = "Formulaire enregistré"
|
||||
|
||||
if @coordinate.present?
|
||||
@destroyed = @coordinate
|
||||
@morphed = champ_components_starting_at(@coordinate)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -101,27 +109,27 @@ module Administrateurs
|
|||
|
||||
def type_de_champ_update_params
|
||||
params.required(:type_de_champ).permit(:type_champ,
|
||||
:libelle,
|
||||
:description,
|
||||
:mandatory,
|
||||
:drop_down_list_value,
|
||||
:drop_down_other,
|
||||
:drop_down_secondary_libelle,
|
||||
:drop_down_secondary_description,
|
||||
:collapsible_explanation_enabled,
|
||||
:collapsible_explanation_text,
|
||||
editable_options: [
|
||||
:cadastres,
|
||||
:unesco,
|
||||
:arretes_protection,
|
||||
:conservatoire_littoral,
|
||||
:reserves_chasse_faune_sauvage,
|
||||
:reserves_biologiques,
|
||||
:reserves_naturelles,
|
||||
:natura_2000,
|
||||
:zones_humides,
|
||||
:znieff
|
||||
])
|
||||
:libelle,
|
||||
:description,
|
||||
:mandatory,
|
||||
:drop_down_list_value,
|
||||
:drop_down_other,
|
||||
:drop_down_secondary_libelle,
|
||||
:drop_down_secondary_description,
|
||||
:collapsible_explanation_enabled,
|
||||
:collapsible_explanation_text,
|
||||
editable_options: [
|
||||
:cadastres,
|
||||
:unesco,
|
||||
:arretes_protection,
|
||||
:conservatoire_littoral,
|
||||
:reserves_chasse_faune_sauvage,
|
||||
:reserves_biologiques,
|
||||
:reserves_naturelles,
|
||||
:natura_2000,
|
||||
:zones_humides,
|
||||
:znieff
|
||||
])
|
||||
end
|
||||
|
||||
def draft
|
||||
|
|
|
@ -225,6 +225,16 @@ class ProcedureRevision < ApplicationRecord
|
|||
types_de_champ_public.any?(&:carte?)
|
||||
end
|
||||
|
||||
def coordinate_and_tdc(stable_id)
|
||||
return [nil, nil] if stable_id.blank?
|
||||
|
||||
coordinate = revision_types_de_champ
|
||||
.joins(:type_de_champ)
|
||||
.find_by(type_de_champ: { stable_id: stable_id })
|
||||
|
||||
[coordinate, coordinate&.type_de_champ]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def compute_estimated_fill_duration
|
||||
|
@ -245,16 +255,6 @@ class ProcedureRevision < ApplicationRecord
|
|||
end
|
||||
end
|
||||
|
||||
def coordinate_and_tdc(stable_id)
|
||||
return [nil, nil] if stable_id.blank?
|
||||
|
||||
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 renumber(siblings)
|
||||
siblings.to_a.compact.each.with_index do |sibling, position|
|
||||
sibling.update_column(:position, position)
|
||||
|
|
|
@ -157,5 +157,20 @@ describe Administrateurs::TypesDeChampController, type: :controller do
|
|||
expect(assigns(:destroyed).libelle).to eq('l2')
|
||||
expect(morpheds).to eq([['l3', ['l1']]])
|
||||
end
|
||||
|
||||
context 'rejected if type changed and routing involved' do
|
||||
let(:params) do
|
||||
{ procedure_id: procedure.id, stable_id: third_coordinate.stable_id }
|
||||
end
|
||||
|
||||
before do
|
||||
allow_any_instance_of(ProcedureRevisionTypeDeChamp).to receive(:used_by_routing_rules?).and_return(true)
|
||||
end
|
||||
|
||||
it do
|
||||
is_expected.to have_http_status(:ok)
|
||||
expect(flash.alert).to include("utilisé pour le routage")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue