Add Procedure path suggestion
This commit is contained in:
parent
8806eab8d6
commit
bd1e0aba38
4 changed files with 71 additions and 7 deletions
|
@ -40,6 +40,7 @@ class Admin::ProceduresController < AdminController
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
@suggested_path = @procedure.suggested_path(current_administrateur)
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
|
|
|
@ -167,8 +167,22 @@ class Procedure < ApplicationRecord
|
||||||
types_de_champ_private.map(&:build_champ)
|
types_de_champ_private.map(&:build_champ)
|
||||||
end
|
end
|
||||||
|
|
||||||
def default_path
|
def path_customized?
|
||||||
libelle&.parameterize&.first(50)
|
!path.match?(/[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}/)
|
||||||
|
end
|
||||||
|
|
||||||
|
def suggested_path(administrateur)
|
||||||
|
if path_customized?
|
||||||
|
return path
|
||||||
|
end
|
||||||
|
slug = libelle&.parameterize&.first(50)
|
||||||
|
suggestion = slug
|
||||||
|
counter = 1
|
||||||
|
while !path_available?(administrateur, suggestion)
|
||||||
|
counter = counter + 1
|
||||||
|
suggestion = "#{slug}-#{counter}"
|
||||||
|
end
|
||||||
|
suggestion
|
||||||
end
|
end
|
||||||
|
|
||||||
def organisation_name
|
def organisation_name
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
%h4 Lien de la démarche
|
%h4 Lien de la démarche
|
||||||
%p.center
|
%p.center
|
||||||
= commencer_url(path: '')
|
= commencer_url(path: '')
|
||||||
= text_field_tag(:path, @procedure.path || @procedure.default_path,
|
= text_field_tag(:path, @suggested_path,
|
||||||
id: 'procedure_path',
|
id: 'procedure_path',
|
||||||
placeholder: 'Chemin vers la démarche',
|
placeholder: 'Chemin vers la démarche',
|
||||||
data: { autocomplete: 'path' },
|
data: { autocomplete: 'path' },
|
||||||
|
|
|
@ -616,6 +616,22 @@ describe Procedure do
|
||||||
it { expect(procedure.archived_at).to eq(now) }
|
it { expect(procedure.archived_at).to eq(now) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'path_customized?' do
|
||||||
|
let(:procedure) { create :procedure }
|
||||||
|
|
||||||
|
subject { procedure.path_customized? }
|
||||||
|
|
||||||
|
context 'when the path is still the default' do
|
||||||
|
it { is_expected.to be_falsey }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the path has been changed' do
|
||||||
|
before { procedure.path = 'custom_path' }
|
||||||
|
|
||||||
|
it { is_expected.to be_truthy }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe 'total_dossier' do
|
describe 'total_dossier' do
|
||||||
let(:procedure) { create :procedure }
|
let(:procedure) { create :procedure }
|
||||||
|
|
||||||
|
@ -630,12 +646,45 @@ describe Procedure do
|
||||||
it { is_expected.to eq 2 }
|
it { is_expected.to eq 2 }
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#default_path' do
|
describe 'suggested_path' do
|
||||||
let(:procedure) { create(:procedure, libelle: 'A long libelle with àccênts, blabla coucou hello un deux trois voila') }
|
let(:procedure) { create :procedure, aasm_state: :publiee, libelle: 'Inscription au Collège' }
|
||||||
|
|
||||||
subject { procedure.default_path }
|
subject { procedure.suggested_path(procedure.administrateurs.first) }
|
||||||
|
|
||||||
it { is_expected.to eq('a-long-libelle-with-accents-blabla-coucou-hello-un') }
|
context 'when the path has been customized' do
|
||||||
|
before { procedure.path = 'custom_path' }
|
||||||
|
|
||||||
|
it { is_expected.to eq 'custom_path' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the suggestion does not conflict' do
|
||||||
|
it { is_expected.to eq 'inscription-au-college' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the suggestion conflicts with one procedure' do
|
||||||
|
before do
|
||||||
|
create :procedure, aasm_state: :publiee, path: 'inscription-au-college'
|
||||||
|
end
|
||||||
|
|
||||||
|
it { is_expected.to eq 'inscription-au-college-2' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the suggestion conflicts with several procedures' do
|
||||||
|
before do
|
||||||
|
create :procedure, aasm_state: :publiee, path: 'inscription-au-college'
|
||||||
|
create :procedure, aasm_state: :publiee, path: 'inscription-au-college-2'
|
||||||
|
end
|
||||||
|
|
||||||
|
it { is_expected.to eq 'inscription-au-college-3' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the suggestion conflicts with another procedure of the same admin' do
|
||||||
|
before do
|
||||||
|
create :procedure, aasm_state: :publiee, path: 'inscription-au-college', administrateurs: procedure.administrateurs
|
||||||
|
end
|
||||||
|
|
||||||
|
it { is_expected.to eq 'inscription-au-college' }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".default_scope" do
|
describe ".default_scope" do
|
||||||
|
|
Loading…
Add table
Reference in a new issue