feat(routing): server side: prevent changing tdc type if used for routing
This commit is contained in:
parent
9ea75a5bd1
commit
8b832396a3
2 changed files with 30 additions and 5 deletions
|
@ -20,7 +20,12 @@ module Administrateurs
|
|||
def update
|
||||
type_de_champ = draft.find_and_ensure_exclusive_use(params[:stable_id])
|
||||
|
||||
if type_de_champ.update(type_de_champ_update_params)
|
||||
if type_de_champ.revision_type_de_champ.used_by_routing_rules? && changing_of_type?(type_de_champ)
|
||||
coordinate = draft.coordinate_for(type_de_champ)
|
||||
errors = "« #{type_de_champ.libelle} » est utilisé pour le routage, vous ne pouvez pas modifier son type."
|
||||
@morphed = [champ_component_from(coordinate, focused: false, errors:)]
|
||||
flash.alert = errors
|
||||
elsif type_de_champ.update(type_de_champ_update_params)
|
||||
@coordinate = draft.coordinate_for(type_de_champ)
|
||||
@morphed = champ_components_starting_at(@coordinate)
|
||||
|
||||
|
@ -86,6 +91,10 @@ module Administrateurs
|
|||
|
||||
private
|
||||
|
||||
def changing_of_type?(type_de_champ)
|
||||
type_de_champ_update_params['type_champ'].present? && (type_de_champ_update_params['type_champ'] != type_de_champ.type_champ)
|
||||
end
|
||||
|
||||
def champ_components_starting_at(coordinate, offset = 0)
|
||||
coordinate
|
||||
.siblings_starting_at(offset)
|
||||
|
@ -93,11 +102,12 @@ module Administrateurs
|
|||
.map { |c| champ_component_from(c) }
|
||||
end
|
||||
|
||||
def champ_component_from(coordinate, focused: false)
|
||||
def champ_component_from(coordinate, focused: false, errors: '')
|
||||
TypesDeChampEditor::ChampComponent.new(
|
||||
coordinate: coordinate,
|
||||
coordinate:,
|
||||
upper_coordinates: coordinate.upper_siblings,
|
||||
focused: focused
|
||||
focused: focused,
|
||||
errors:
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ describe Administrateurs::TypesDeChampController, type: :controller do
|
|||
create(:procedure).tap do |p|
|
||||
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'l1')
|
||||
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'l2')
|
||||
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'l3')
|
||||
p.draft_revision.add_type_de_champ(type_champ: :drop_down_list, libelle: 'l3')
|
||||
p.draft_revision.add_type_de_champ(type_champ: :yes_no, libelle: 'bon dossier', private: true)
|
||||
end
|
||||
end
|
||||
|
@ -97,6 +97,21 @@ describe Administrateurs::TypesDeChampController, type: :controller do
|
|||
expect(flash.alert).to eq(["Le champ « Libelle » doit être rempli"])
|
||||
end
|
||||
end
|
||||
|
||||
context 'rejected if type changed and routing involved' do
|
||||
let(:params) do
|
||||
default_params.deep_merge(type_de_champ: { type_champ: 'text', 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
|
||||
|
||||
# l1, l2, l3 => l1, l3, l2
|
||||
|
|
Loading…
Reference in a new issue