From f6a6d8f4577f5f2ac564a0d8bbd73ab4c6bdaffe Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 19 Sep 2018 17:12:06 +0200 Subject: [PATCH 1/2] Fix destroy dossiers on procedure reset --- app/models/procedure.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 2f2c10855..a51759245 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -114,7 +114,7 @@ class Procedure < ApplicationRecord if locked? raise "Can not reset a locked procedure." else - dossiers.delete_all + dossiers.destroy_all end end From f94a24d7ce21dc9aedb1eeeb5db0af50120583cd Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 19 Sep 2018 17:12:12 +0200 Subject: [PATCH 2/2] Add path field to procedure information form --- .../admin/procedures_controller.rb | 32 ++++++++- app/models/procedure.rb | 4 +- .../admin/procedures/_informations.html.haml | 8 +++ .../admin/procedures/_modal_publish.html.haml | 2 +- app/views/admin/procedures/edit.html.haml | 5 +- app/views/admin/procedures/new.html.haml | 6 +- config/locales/models/procedure_path/fr.yml | 4 ++ .../admin/procedures_controller_spec.rb | 2 + spec/features/admin/procedure_cloning_spec.rb | 3 + .../features/admin/procedure_creation_spec.rb | 72 +++++++++++++------ spec/features/admin/procedure_spec_helper.rb | 7 +- spec/features/admin/procedure_update_spec.rb | 63 ++++++++++++++++ 12 files changed, 176 insertions(+), 32 deletions(-) create mode 100644 spec/features/admin/procedure_update_spec.rb diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index c9c9137bd..cd6671683 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -39,6 +39,9 @@ class Admin::ProceduresController < AdminController end def edit + @path = @procedure.path || @procedure.default_path + @available = @procedure.path_available?(@path) + @mine = @procedure.path_is_mine?(@path) end def hide @@ -63,29 +66,52 @@ class Admin::ProceduresController < AdminController def new @procedure ||= Procedure.new @procedure.module_api_carto ||= ModuleAPICarto.new + @available = true + @mine = true end def create @procedure = Procedure.new(procedure_params) @procedure.module_api_carto = ModuleAPICarto.new(create_module_api_carto_params) if @procedure.valid? + @path = params.require(:procedure).permit(:path)[:path] + @available = !ProcedurePath.exists?(path: @path) + @mine = ProcedurePath.mine?(current_administrateur, @path) - if !@procedure.save + if !@procedure.validate flash.now.alert = @procedure.errors.full_messages return render 'new' + elsif Flipflop.publish_draft? && !ProcedurePath.valid?(Procedure.last, @path) + # FIXME: The code abow is a horrible hack that we need until we migrated path directly on procedure model + flash.now.alert = 'Lien de la démarche invalide.' + return render 'new' + else + @procedure.save! + if Flipflop.publish_draft? + @procedure.publish_with_path!(@path) + end + flash.notice = 'Démarche enregistrée.' end - flash.notice = 'Démarche enregistrée' redirect_to admin_procedure_types_de_champ_path(procedure_id: @procedure.id) end def update @procedure = current_administrateur.procedures.find(params[:id]) + path = params.require(:procedure).permit(:path)[:path] if !@procedure.update(procedure_params) flash.alert = @procedure.errors.full_messages + elsif Flipflop.publish_draft? && @procedure.brouillon? + if ProcedurePath.valid?(@procedure, path) + @procedure.publish_with_path!(path) + reset_procedure + flash.notice = 'Démarche modifiée. Tous les brouillons de cette démarche ont été supprimés.' + else + flash.alert = 'Lien de la démarche invalide.' + end else reset_procedure - flash.notice = 'Démarche modifiée' + flash.notice = 'Démarche modifiée. Tous les brouillons de cette démarche ont été supprimés.' end redirect_to edit_admin_procedure_path(id: @procedure.id) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index a51759245..80e5629e7 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -9,7 +9,7 @@ class Procedure < ApplicationRecord has_one :module_api_carto, dependent: :destroy has_one :attestation_template, dependent: :destroy - has_one :procedure_path + has_one :procedure_path, dependent: :destroy belongs_to :administrateur belongs_to :parent_procedure, class_name: 'Procedure' @@ -158,7 +158,7 @@ class Procedure < ApplicationRecord end def default_path - libelle.parameterize.first(50) + libelle&.parameterize&.first(50) end def organisation_name diff --git a/app/views/admin/procedures/_informations.html.haml b/app/views/admin/procedures/_informations.html.haml index 212442f00..a7cd61033 100644 --- a/app/views/admin/procedures/_informations.html.haml +++ b/app/views/admin/procedures/_informations.html.haml @@ -11,6 +11,14 @@ = f.text_area :description, rows: '6', placeholder: 'Description du projet', class: 'form-control' - if !@procedure.locked? + - if Flipflop.publish_draft? + .form-group + %h4 Lien* + = f.text_field :path, value: @path, class: 'form-control', data: { remote: true, debounce: true, url: admin_procedures_available_path, params: { id: @procedure.id }.to_query(:procedure) } + .unavailable-path-message + - if !@available + = render partial: 'unavailable', locals: { mine: @mine } + .form-group %h4 Conservation des données = f.label :duree_conservation_dossiers_dans_ds, "Sur demarches-simplifiees.fr* (durée en mois après le début de l’instruction)" diff --git a/app/views/admin/procedures/_modal_publish.html.haml b/app/views/admin/procedures/_modal_publish.html.haml index c89e4b9f2..03a3faff8 100644 --- a/app/views/admin/procedures/_modal_publish.html.haml +++ b/app/views/admin/procedures/_modal_publish.html.haml @@ -21,7 +21,7 @@ %h4 Lien de la démarche %p.center = commencer_url(procedure_path: '') - = text_field_tag('procedure_path', @procedure.default_path, + = text_field_tag('procedure_path', @procedure.path || @procedure.default_path, id: 'procedure_path', placeholder: 'Chemin vers la démarche', data: { autocomplete: 'path' }, diff --git a/app/views/admin/procedures/edit.html.haml b/app/views/admin/procedures/edit.html.haml index 5efaea830..17bb396b8 100644 --- a/app/views/admin/procedures/edit.html.haml +++ b/app/views/admin/procedures/edit.html.haml @@ -3,4 +3,7 @@ = form_for @procedure, url: url_for({ controller: 'admin/procedures', action: :update, id: @procedure.id }), multipart: true do |f| = render partial: 'informations', locals: { f: f } .text-right - = f.button 'Enregistrer', class: 'btn btn-success' + - if !Flipflop.publish_draft? || (@available || @mine) + = f.button 'Enregistrer', class: 'btn btn-success' + - else + = f.button 'Enregistrer', class: 'btn btn-success', disabled: true diff --git a/app/views/admin/procedures/new.html.haml b/app/views/admin/procedures/new.html.haml index 1b203b146..bb05ef17a 100644 --- a/app/views/admin/procedures/new.html.haml +++ b/app/views/admin/procedures/new.html.haml @@ -5,4 +5,8 @@ #procedure_new.section.section-label = form_for @procedure, url: { controller: 'admin/procedures', action: :create }, multipart: true do |f| = render partial: 'informations', locals: { f: f } - = f.submit 'Valider', class: 'btn btn-info', id: 'save-procedure', style: 'float: right;' + .text-right + - if !Flipflop.publish_draft? || (@available || @mine) + = f.button 'Valider', class: 'btn btn-info', id: 'save-procedure' + - else + = f.button 'Valider', class: 'btn btn-info', id: 'save-procedure', disabled: true diff --git a/config/locales/models/procedure_path/fr.yml b/config/locales/models/procedure_path/fr.yml index a17306b90..bd1e29b41 100644 --- a/config/locales/models/procedure_path/fr.yml +++ b/config/locales/models/procedure_path/fr.yml @@ -1,8 +1,12 @@ fr: activerecord: + attributes: + procedure_path: + path: Lien 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. + taken: est déjà utilisé par une procédure. diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index 3536c64d1..301e93094 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -6,6 +6,7 @@ describe Admin::ProceduresController, type: :controller do let(:bad_procedure_id) { 100000 } + let(:path) { 'ma-jolie-demarche' } let(:libelle) { 'Démarche de test' } let(:description) { 'Description de test' } let(:organisation) { 'Organisation de test' } @@ -20,6 +21,7 @@ describe Admin::ProceduresController, type: :controller do let(:procedure_params) { { + path: path, libelle: libelle, description: description, organisation: organisation, diff --git a/spec/features/admin/procedure_cloning_spec.rb b/spec/features/admin/procedure_cloning_spec.rb index d085edd19..1315a0b35 100644 --- a/spec/features/admin/procedure_cloning_spec.rb +++ b/spec/features/admin/procedure_cloning_spec.rb @@ -7,6 +7,9 @@ feature 'As an administrateur I wanna clone a procedure', js: true do let(:administrateur) { create(:administrateur) } before do + # FIXME: needed to make procedure_path validation work + create(:procedure) + Flipflop::FeatureSet.current.test!.switch!(:publish_draft, true) login_as administrateur, scope: :administrateur visit root_path end diff --git a/spec/features/admin/procedure_creation_spec.rb b/spec/features/admin/procedure_creation_spec.rb index e9919d721..9bc37d4fc 100644 --- a/spec/features/admin/procedure_creation_spec.rb +++ b/spec/features/admin/procedure_creation_spec.rb @@ -7,6 +7,9 @@ feature 'As an administrateur I wanna create a new procedure', js: true do let(:administrateur) { create(:administrateur) } before do + # FIXME: needed to make procedure_path validation work + create(:procedure) + Flipflop::FeatureSet.current.test!.switch!(:publish_draft, true) login_as administrateur, scope: :administrateur visit root_path end @@ -29,21 +32,46 @@ feature 'As an administrateur I wanna create a new procedure', js: true do end context 'Creating a new procedure' do - scenario 'Finding save button for new procedure, libelle, description and cadre_juridique required' do - expect(page).to have_selector('#new-procedure') - find('#new-procedure').click - click_on 'from-scratch' + context "when publish_draft enabled" do + scenario 'Finding save button for new procedure, libelle, description and cadre_juridique required' do + expect(page).to have_selector('#new-procedure') + find('#new-procedure').click + click_on 'from-scratch' - expect(page).to have_current_path(new_admin_procedure_path) - fill_in 'procedure_duree_conservation_dossiers_dans_ds', with: '3' - fill_in 'procedure_duree_conservation_dossiers_hors_ds', with: '6' - click_on 'save-procedure' + expect(page).to have_current_path(new_admin_procedure_path) + fill_in 'procedure_duree_conservation_dossiers_dans_ds', with: '3' + fill_in 'procedure_duree_conservation_dossiers_hors_ds', with: '6' + click_on 'save-procedure' - expect(page).to have_text('Libelle doit être rempli') - fill_in_dummy_procedure_details - click_on 'save-procedure' + expect(page).to have_text('Libelle doit être rempli') + fill_in_dummy_procedure_details + click_on 'save-procedure' - expect(page).to have_current_path(admin_procedure_types_de_champ_path(Procedure.first)) + expect(page).to have_current_path(admin_procedure_types_de_champ_path(Procedure.last)) + end + end + + context "when publish_draft disabled" do + before do + Flipflop::FeatureSet.current.test!.switch!(:publish_draft, false) + end + + scenario 'Finding save button for new procedure, libelle, description and cadre_juridique required' do + expect(page).to have_selector('#new-procedure') + find('#new-procedure').click + click_on 'from-scratch' + + expect(page).to have_current_path(new_admin_procedure_path) + fill_in 'procedure_duree_conservation_dossiers_dans_ds', with: '3' + fill_in 'procedure_duree_conservation_dossiers_hors_ds', with: '6' + click_on 'save-procedure' + + expect(page).to have_text('Libelle doit être rempli') + fill_in_dummy_procedure_details(fill_path: false) + click_on 'save-procedure' + + expect(page).to have_current_path(admin_procedure_types_de_champ_path(Procedure.last)) + end end end @@ -64,20 +92,20 @@ feature 'As an administrateur I wanna create a new procedure', js: true do scenario 'Add champ, add file, visualize them in procedure preview' do fill_in 'procedure_types_de_champ_attributes_0_libelle', with: 'libelle de champ' click_on 'add_type_de_champ' - expect(page).to have_current_path(admin_procedure_types_de_champ_path(Procedure.first)) + expect(page).to have_current_path(admin_procedure_types_de_champ_path(Procedure.last)) expect(page).to have_selector('#procedure_types_de_champ_attributes_1_libelle') - expect(Procedure.first.types_de_champ.first.libelle).to eq('libelle de champ') + expect(Procedure.last.types_de_champ.first.libelle).to eq('libelle de champ') click_on 'onglet-pieces' - expect(page).to have_current_path(admin_procedure_pieces_justificatives_path(Procedure.first)) + expect(page).to have_current_path(admin_procedure_pieces_justificatives_path(Procedure.last)) fill_in 'procedure_types_de_piece_justificative_attributes_0_libelle', with: 'libelle de piece' click_on 'add_piece_justificative' - expect(page).to have_current_path(admin_procedure_pieces_justificatives_path(Procedure.first)) + expect(page).to have_current_path(admin_procedure_pieces_justificatives_path(Procedure.last)) expect(page).to have_selector('#procedure_types_de_piece_justificative_attributes_1_libelle') preview_window = window_opened_by { click_on 'onglet-preview' } within_window(preview_window) do - expect(page).to have_current_path(apercu_procedure_path(Procedure.first)) + expect(page).to have_current_path(apercu_procedure_path(Procedure.last)) expect(page).to have_field('libelle de champ') expect(page).to have_field('libelle de piece') end @@ -88,28 +116,28 @@ feature 'As an administrateur I wanna create a new procedure', js: true do click_on 'add_type_de_champ' click_on 'onglet-pieces' - expect(page).to have_current_path(admin_procedure_pieces_justificatives_path(Procedure.first)) + expect(page).to have_current_path(admin_procedure_pieces_justificatives_path(Procedure.last)) fill_in 'procedure_types_de_piece_justificative_attributes_0_libelle', with: 'libelle de piece' click_on 'add_piece_justificative' click_on 'onglet-infos' - expect(page).to have_current_path(admin_procedure_path(Procedure.first)) + expect(page).to have_current_path(admin_procedure_path(Procedure.last)) expect(page).to have_selector('#disabled-publish-procedure') expect(page.find_by_id('disabled-publish-procedure')[:disabled]).to eq('true') click_on 'onglet-instructeurs' - expect(page).to have_current_path(admin_procedure_instructeurs_path(Procedure.first)) + expect(page).to have_current_path(admin_procedure_instructeurs_path(Procedure.last)) fill_in 'gestionnaire_email', with: 'gestionnaire@apientreprise.fr' click_on 'add-gestionnaire-email' page.first('.gestionnaire-affectation').click click_on 'onglet-infos' - expect(page).to have_current_path(admin_procedure_path(Procedure.first)) + expect(page).to have_current_path(admin_procedure_path(Procedure.last)) expect(page).to have_selector('#publish-procedure', visible: true) find('#publish-procedure').click within '#publish-modal' do - expect(page).to have_field('procedure_path', with: 'libelle-de-la-procedure') + expect(page).to have_field('procedure_path', with: 'lien-de-la-procedure') click_on 'publish' end diff --git a/spec/features/admin/procedure_spec_helper.rb b/spec/features/admin/procedure_spec_helper.rb index 78b9035f5..0154e9609 100644 --- a/spec/features/admin/procedure_spec_helper.rb +++ b/spec/features/admin/procedure_spec_helper.rb @@ -1,7 +1,10 @@ module ProcedureSpecHelper - def fill_in_dummy_procedure_details + def fill_in_dummy_procedure_details(fill_path: true) fill_in 'procedure_libelle', with: 'libelle de la procedure' - page.execute_script("$('#procedure_description').val('description de la procedure')") + fill_in 'procedure_description', with: 'description de la procedure' + if fill_path + fill_in 'procedure_path', with: 'lien-de-la-procedure' + end fill_in 'procedure_cadre_juridique', with: 'cadre juridique' fill_in 'procedure_duree_conservation_dossiers_dans_ds', with: '3' fill_in 'procedure_duree_conservation_dossiers_hors_ds', with: '6' diff --git a/spec/features/admin/procedure_update_spec.rb b/spec/features/admin/procedure_update_spec.rb new file mode 100644 index 000000000..68f9b3df4 --- /dev/null +++ b/spec/features/admin/procedure_update_spec.rb @@ -0,0 +1,63 @@ +require 'spec_helper' +require 'features/admin/procedure_spec_helper' + +feature 'Administrateurs can edit procedures', js: true do + include ProcedureSpecHelper + + let(:administrateur) { create(:administrateur) } + let!(:procedure) do + create(:procedure_with_dossiers, + :published, + :with_path, + :with_type_de_champ, + administrateur: administrateur) + end + + before do + Flipflop::FeatureSet.current.test!.switch!(:publish_draft, true) + login_as administrateur, scope: :administrateur + end + + context 'when the procedure is in draft' do + let!(:procedure) do + create(:procedure_with_dossiers, + :with_path, + :with_type_de_champ, + administrateur: administrateur) + end + + scenario 'the administrator can edit the libelle and the path' do + visit admin_procedures_draft_path + click_on procedure.libelle + click_on 'Description' + + expect(page).to have_field('procedure_libelle', with: procedure.libelle) + expect(page).to have_field('procedure_path', with: procedure.path) + + fill_in('procedure_libelle', with: 'Ma petite démarche') + fill_in('procedure_path', with: 'nouveau-lien-demarche') + + click_on 'Enregistrer' + + expect(page).to have_field('procedure_libelle', with: 'Ma petite démarche') + expect(page).to have_field('procedure_path', with: 'nouveau-lien-demarche') + end + end + + context 'when the procedure is published' do + scenario 'the administrator can edit the libellé, but can‘t change the path' do + visit root_path + click_on procedure.libelle + click_on 'Description' + + expect(page).to have_field('procedure_libelle', with: procedure.libelle) + expect(page).not_to have_field('procedure_path') + + fill_in('procedure_libelle', with: 'Ma petite démarche') + + click_on 'Enregistrer' + + expect(page).to have_field('procedure_libelle', with: 'Ma petite démarche') + end + end +end