Add Procedure path suggestion

This commit is contained in:
Nicolas Bouilleaud 2019-07-05 14:38:20 +02:00
parent 8806eab8d6
commit bd1e0aba38
4 changed files with 71 additions and 7 deletions

View file

@ -40,6 +40,7 @@ class Admin::ProceduresController < AdminController
end
def show
@suggested_path = @procedure.suggested_path(current_administrateur)
end
def edit

View file

@ -167,8 +167,22 @@ class Procedure < ApplicationRecord
types_de_champ_private.map(&:build_champ)
end
def default_path
libelle&.parameterize&.first(50)
def path_customized?
!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
def organisation_name

View file

@ -21,7 +21,7 @@
%h4 Lien de la démarche
%p.center
= commencer_url(path: '')
= text_field_tag(:path, @procedure.path || @procedure.default_path,
= text_field_tag(:path, @suggested_path,
id: 'procedure_path',
placeholder: 'Chemin vers la démarche',
data: { autocomplete: 'path' },

View file

@ -616,6 +616,22 @@ describe Procedure do
it { expect(procedure.archived_at).to eq(now) }
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
let(:procedure) { create :procedure }
@ -630,12 +646,45 @@ describe Procedure do
it { is_expected.to eq 2 }
end
describe '#default_path' do
let(:procedure) { create(:procedure, libelle: 'A long libelle with àccênts, blabla coucou hello un deux trois voila') }
describe 'suggested_path' do
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
describe ".default_scope" do