From 887512c26c294df7bf5e962c23b2d161e9a980d5 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Fri, 14 Apr 2017 11:34:53 +0200 Subject: [PATCH] Procedure path can contain dashes and can be longer --- .../javascripts/admin_procedures_modal.js | 2 +- app/models/procedure.rb | 2 +- app/models/procedure_path.rb | 2 +- .../admin/procedures/_modal_publish.html.haml | 4 ++-- config/locales/models/procedure_path/fr.yml | 8 ++++++++ ...1_remove_field_limit_for_procedure_paths.rb | 5 +++++ db/schema.rb | 4 ++-- spec/features/admin/procedure_creation_spec.rb | 2 +- spec/models/procedure_path_spec.rb | 18 +++++++++++++++--- spec/models/procedure_spec.rb | 8 ++++++++ 10 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 config/locales/models/procedure_path/fr.yml create mode 100644 db/migrate/20170414095411_remove_field_limit_for_procedure_paths.rb diff --git a/app/assets/javascripts/admin_procedures_modal.js b/app/assets/javascripts/admin_procedures_modal.js index 342cc64f5..edbb6f538 100644 --- a/app/assets/javascripts/admin_procedures_modal.js +++ b/app/assets/javascripts/admin_procedures_modal.js @@ -52,7 +52,7 @@ function path_validation(el) { } function validatePath(path) { - var re = /^[a-z0-9_]{3,30}$/; + var re = /^[a-z0-9_\-]{3,50}$/; return re.test(path); } diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 3d4edf63f..7d3624dca 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -53,7 +53,7 @@ class Procedure < ActiveRecord::Base end def default_path - libelle.downcase.gsub(/[^a-z0-9\-_]/, "_").gsub(/_*$/, '').gsub(/_+/, '_') + libelle.parameterize.first(50) end def types_de_champ_ordered diff --git a/app/models/procedure_path.rb b/app/models/procedure_path.rb index e68942ff1..98fd4e3d1 100644 --- a/app/models/procedure_path.rb +++ b/app/models/procedure_path.rb @@ -1,5 +1,5 @@ class ProcedurePath < ActiveRecord::Base - validates :path, procedure_path_format: true, presence: true, allow_blank: false, allow_nil: false + validates :path, format: { with: /\A[a-z0-9_\-]{3,50}\z/ }, presence: true, allow_blank: false, allow_nil: false validates :administrateur_id, presence: true, allow_blank: false, allow_nil: false validates :procedure_id, presence: true, allow_blank: false, allow_nil: false diff --git a/app/views/admin/procedures/_modal_publish.html.haml b/app/views/admin/procedures/_modal_publish.html.haml index b5c282d34..14fb1c234 100644 --- a/app/views/admin/procedures/_modal_publish.html.haml +++ b/app/views/admin/procedures/_modal_publish.html.haml @@ -28,7 +28,7 @@ id: 'procedure_path', placeholder: 'Chemin vers la procédure', class: 'form-control', - maxlength: 30, + maxlength: 50, style: 'width: 300px; display: inline;') #path-messages #path_is_mine.text-warning.center.message @@ -40,7 +40,7 @@ %br Vous ne pouvez pas l'utiliser car il appartient à un autre administrateur. #path_is_invalid.text-danger.center.message - Ce lien n'est pas valide. Il doit comporter au moins 3 caractères et seuls les caractères a-z, 0-9 et '_' sont autorisés. + = t('activerecord.errors.models.procedure_path.attributes.path.format') .modal-footer = submit_tag "#{@procedure.archived? ? 'Réactiver' : 'Publier'}", class: %w(btn btn btn-success), id: 'publish', diff --git a/config/locales/models/procedure_path/fr.yml b/config/locales/models/procedure_path/fr.yml new file mode 100644 index 000000000..a17306b90 --- /dev/null +++ b/config/locales/models/procedure_path/fr.yml @@ -0,0 +1,8 @@ +fr: + activerecord: + errors: + models: + procedure_path: + attributes: + path: + format: Ce lien n'est pas valide. Il doit comporter au moins 3 caractères, au plus 50 caractères et seuls les caractères a-z, 0-9, '_' et '-' sont autorisés. diff --git a/db/migrate/20170414095411_remove_field_limit_for_procedure_paths.rb b/db/migrate/20170414095411_remove_field_limit_for_procedure_paths.rb new file mode 100644 index 000000000..9de4f3a0b --- /dev/null +++ b/db/migrate/20170414095411_remove_field_limit_for_procedure_paths.rb @@ -0,0 +1,5 @@ +class RemoveFieldLimitForProcedurePaths < ActiveRecord::Migration[5.0] + def change + change_column :procedure_paths, :path, :string, limit: nil, null: true, unique: true, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index e390453da..140dccaf6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170328142700) do +ActiveRecord::Schema.define(version: 20170414095411) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -319,7 +319,7 @@ ActiveRecord::Schema.define(version: 20170328142700) do end create_table "procedure_paths", force: :cascade do |t| - t.string "path", limit: 30 + t.string "path" t.integer "procedure_id" t.integer "administrateur_id" t.index ["path"], name: "index_procedure_paths_on_path", using: :btree diff --git a/spec/features/admin/procedure_creation_spec.rb b/spec/features/admin/procedure_creation_spec.rb index fbf76005a..8e14bd43a 100644 --- a/spec/features/admin/procedure_creation_spec.rb +++ b/spec/features/admin/procedure_creation_spec.rb @@ -92,7 +92,7 @@ feature 'As an administrateur I wanna create a new procedure', js: true do expect(page).to have_selector('#publish-procedure', visible: true) page.find_by_id('publish-procedure').click - expect(page.find_by_id('procedure_path')['value']).to eq('libelle_de_la_procedure') + expect(page.find_by_id('procedure_path')['value']).to eq('libelle-de-la-procedure') page.find_by_id('publish').click expect(page).to have_selector('.procedure-lien') end diff --git a/spec/models/procedure_path_spec.rb b/spec/models/procedure_path_spec.rb index fa7ceb7d4..dfb268291 100644 --- a/spec/models/procedure_path_spec.rb +++ b/spec/models/procedure_path_spec.rb @@ -24,14 +24,26 @@ describe ProcedurePath do let(:path) { '' } it { expect{procedure_path}.to raise_error ActiveRecord::RecordInvalid } end - context 'when path is invalid' do + context 'when path contains spaces' do let(:path) { 'Demande de subvention' } it { expect{procedure_path}.to raise_error ActiveRecord::RecordInvalid } end - context 'when path is valid' do - let(:path) { 'ma_super_procedure' } + context 'when path contains alphanumerics and underscores' do + let(:path) { 'ma_super_procedure_1' } it { expect{procedure_path}.not_to raise_error } end + context 'when path contains dashes' do + let(:path) { 'ma-super-procedure' } + it { expect{procedure_path}.not_to raise_error } + end + context 'when path is too long' do + let(:path) { 'ma-super-procedure-12345678901234567890123456789012345678901234567890' } + it { expect{procedure_path}.to raise_error ActiveRecord::RecordInvalid } + end + context 'when path is too short' do + let(:path) { 'pr' } + it { expect{procedure_path}.to raise_error ActiveRecord::RecordInvalid } + end end end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 6c4b16ba6..7a5717d99 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -284,4 +284,12 @@ describe Procedure do it_behaves_like "export is empty" end end + + describe '#default_path' do + let(:procedure){ create(:procedure, libelle: 'A long libelle with àccênts, blabla coucou hello un deux trois voila') } + + subject { procedure.default_path } + + it { is_expected.to eq('a-long-libelle-with-accents-blabla-coucou-hello-un') } + end end