Merge pull request #2926 from tchak/path-availability

Clarify procedure path availability code
This commit is contained in:
gregoirenovel 2018-10-30 16:21:07 +01:00 committed by GitHub
commit 9a3daf0b79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 78 additions and 52 deletions

View file

@ -64,10 +64,7 @@ function validatePath(path) {
}
function path_type_init() {
$(PROCEDURE_PATH_SELECTOR).bind('autocomplete:select', function(
ev,
suggestion
) {
togglePathMessage(true, suggestion['mine']);
$(PROCEDURE_PATH_SELECTOR).on('autocomplete:select', function(event) {
togglePathMessage(true, event.detail['mine']);
});
}

View file

@ -40,8 +40,7 @@ class Admin::ProceduresController < AdminController
def edit
@path = @procedure.path || @procedure.default_path
@available = @procedure.path_available?(@path)
@mine = @procedure.path_mine?(@path)
@availability = @procedure.path_availability(@path)
end
def destroy
@ -60,8 +59,7 @@ class Admin::ProceduresController < AdminController
def new
@procedure ||= Procedure.new
@procedure.module_api_carto ||= ModuleAPICarto.new
@available = true
@mine = true
@availability = Procedure::PATH_AVAILABLE
end
def create
@ -72,32 +70,35 @@ class Admin::ProceduresController < AdminController
end
@path = @procedure.path
@available = !Procedure.exists?(path: @path)
@mine = Procedure.path_mine?(current_administrateur, @path)
@availability = Procedure.path_availability(current_administrateur, @procedure.path)
if !@procedure.save
flash.now.alert = @procedure.errors.full_messages
return render 'new'
render 'new'
else
flash.notice = 'Démarche enregistrée.'
redirect_to admin_procedure_types_de_champ_path(procedure_id: @procedure.id)
end
redirect_to admin_procedure_types_de_champ_path(procedure_id: @procedure.id)
end
def update
@procedure = current_administrateur.procedures.find(params[:id])
if !@procedure.update(procedure_params)
flash.alert = @procedure.errors.full_messages
flash.now.alert = @procedure.errors.full_messages
@path = procedure_params[:path]
if @path.present?
@availability = @procedure.path_availability(@path)
end
render 'edit'
elsif Flipflop.publish_draft? && @procedure.brouillon?
reset_procedure
flash.notice = 'Démarche modifiée. Tous les dossiers de cette démarche ont été supprimés.'
redirect_to edit_admin_procedure_path(id: @procedure.id)
else
flash.notice = 'Démarche modifiée.'
redirect_to edit_admin_procedure_path(id: @procedure.id)
end
redirect_to edit_admin_procedure_path(id: @procedure.id)
end
def publish
@ -225,11 +226,9 @@ class Admin::ProceduresController < AdminController
if procedure_id.present?
procedure = current_administrateur.procedures.find(procedure_id)
@available = procedure.path_available?(path)
@mine = procedure.path_mine?(path)
@availability = procedure.path_availability(path)
else
@available = !Procedure.exists?(path: path)
@mine = Procedure.path_mine?(current_administrateur, path)
@availability = Procedure.path_availability(current_administrateur, path)
end
end

View file

@ -306,16 +306,34 @@ class Procedure < ApplicationRecord
mean_time(:en_instruction_at, :processed_at)
end
def path_available?(path)
!Procedure.where.not(id: id).exists?(path: path)
PATH_AVAILABLE = :available
PATH_AVAILABLE_PUBLIEE = :available_publiee
PATH_NOT_AVAILABLE = :not_available
PATH_NOT_AVAILABLE_BROUILLON = :not_available_brouillon
PATH_CAN_PUBLISH = [PATH_AVAILABLE, PATH_AVAILABLE_PUBLIEE]
def path_availability(path)
Procedure.path_availability(administrateur, path, id)
end
def path_mine?(path)
Procedure.path_mine?(administrateur, path)
end
def self.path_availability(administrateur, path, exclude_id = nil)
if exclude_id.present?
procedure = where.not(id: exclude_id).find_by(path: path)
else
procedure = find_by(path: path)
end
def self.path_mine?(administrateur, path)
where(administrateur: administrateur).exists?(path: path)
if procedure.blank?
PATH_AVAILABLE
elsif administrateur.owns?(procedure)
if procedure.brouillon?
PATH_NOT_AVAILABLE_BROUILLON
else
PATH_AVAILABLE_PUBLIEE
end
else
PATH_NOT_AVAILABLE
end
end
def self.find_with_path(path)
@ -335,7 +353,7 @@ class Procedure < ApplicationRecord
end
def can_publish?(path)
path_available?(path) || path_mine?(path)
path_availability(path).in?(PATH_CAN_PUBLISH)
end
def after_publish(path)

View file

@ -19,8 +19,8 @@
= commencer_test_url(path: '')
= 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) }
%p.unavailable-path-message.text-warning
- if !@available
= render partial: 'unavailable', locals: { mine: @mine }
- if @availability != Procedure::PATH_AVAILABLE
= render partial: 'unavailable', locals: { availability: @availability }
%p.help-block
%i.fa.fa-info-circle
Afin de faciliter laccès à la démarche, vous êtes invité à personnaliser ladresse d'accès public. C'est ce lien qu'il va falloir communiquer aux usagers souhaitant faire la démarche.

View file

@ -41,7 +41,8 @@
%br
Vous ne pouvez pas lutiliser car il appartient à un autre administrateur.
#path_is_invalid.text-danger.center.message
= t('activerecord.errors.models.procedure.attributes.path.format')
Ce lien
= t('activerecord.errors.models.procedure.attributes.path.invalid')
.modal-footer
= submit_tag procedure_modal_text(@procedure, :submit), class: %w(btn btn btn-success), disabled: :disabled, id: 'publish'
= button_tag "Annuler", class: %w(btn btn btn-default), data: { dismiss: :modal }, id: 'cancel'

View file

@ -1,8 +1,11 @@
- if mine
- case availability
- when Procedure::PATH_AVAILABLE_PUBLIEE
Ce lien est déjà utilisé par une de vos démarche.
%br
Si vous voulez lutiliser, lancienne démarche sera archivée lors de la publication de la démarche (plus accessible du public).
- else
Si vous voulez lutiliser, lancienne démarche sera archivée lors de la publication de la démarche (plus accessible du public).
- when Procedure::PATH_NOT_AVAILABLE_BROUILLON
Un brouillon de démarche existe déjà avec ce lien.
- when Procedure::PATH_NOT_AVAILABLE
Ce lien est déjà utilisé par une démarche.
%br
Vous ne pouvez pas lutiliser car il appartient à un autre administrateur.

View file

@ -1,11 +1,11 @@
<% if @available %>
<% if @availability == Procedure::PATH_AVAILABLE %>
<%= remove_element('.unavailable-path-message', inner: true) %>
<% else %>
<%= render_to_element('.unavailable-path-message', partial: 'unavailable', locals: { availability: @availability }) %>
<% end %>
<% if @availability.in?(Procedure::PATH_CAN_PUBLISH) %>
<%= enable_element('button[type=submit]') %>
<% else %>
<%= render_to_element('.unavailable-path-message', partial: 'unavailable', locals: { mine: @mine }) %>
<% if @mine %>
<%= enable_element('button[type=submit]') %>
<% else %>
<%= disable_element('button[type=submit]') %>
<% end %>
<%= disable_element('button[type=submit]') %>
<% end %>

View file

@ -3,7 +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
- if !Flipflop.publish_draft? || (@available || @mine)
- if !Flipflop.publish_draft? || @availability.in?(Procedure::PATH_CAN_PUBLISH)
= f.button 'Enregistrer', class: 'btn btn-success'
- else
= f.button 'Enregistrer', class: 'btn btn-success', disabled: true

View file

@ -6,7 +6,7 @@
= form_for @procedure, url: { controller: 'admin/procedures', action: :create }, multipart: true do |f|
= render partial: 'informations', locals: { f: f }
.text-right
- if !Flipflop.publish_draft? || (@available || @mine)
- if !Flipflop.publish_draft? || @availability.in?(Procedure::PATH_CAN_PUBLISH)
= f.button 'Valider', class: 'btn btn-info', id: 'save-procedure'
- else
= f.button 'Valider', class: 'btn btn-info', id: 'save-procedure', disabled: true

View file

@ -0,0 +1 @@
= render partial: 'layouts/left_panels/left_panel_admin_procedurescontroller_navbar', locals: { active: 'Description' }

View file

@ -156,6 +156,10 @@ fr:
taken: déjà utilisé
password:
too_short: 'est trop court'
procedure:
attributes:
path:
invalid: 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.
errors:
messages:

View file

@ -2,13 +2,7 @@ fr:
activerecord:
attributes:
procedure:
path: Lien
path: Lien public
organisation: Organisme
duree_conservation_dossiers_dans_ds: Durée de conservation des dossiers sur demarches-simplifiees.fr
duree_conservation_dossiers_hors_ds: Durée de conservation des dossiers hors demarches-simplifiees.fr
errors:
models:
procedure:
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.

View file

@ -735,10 +735,19 @@ describe Admin::ProceduresController, type: :controller do
it { expect(response.body).to include("innerHTML = ''") }
end
context 'my path' do
context 'my path (brouillon)' do
let(:procedure_owned) { create(:procedure, :with_path, administrateur: admin) }
let(:path) { procedure_owned.path }
it {
expect(response.body).to include('Un brouillon de démarche existe déjà avec ce lien.')
}
end
context 'my path' do
let(:procedure_owned) { create(:procedure, :published, administrateur: admin) }
let(:path) { procedure_owned.path }
it {
expect(response.body).to include('Ce lien est déjà utilisé par une de vos démarche.')
expect(response.body).to include('Si vous voulez lutiliser, lancienne démarche sera archivée')