Clarify procedure path availability code

This commit is contained in:
Paul Chavard 2018-10-30 15:59:27 +01:00
parent 6782643426
commit e391fe6bec
8 changed files with 62 additions and 33 deletions

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,8 +70,7 @@ 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
@ -89,6 +86,10 @@ class Admin::ProceduresController < AdminController
if !@procedure.update(procedure_params)
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
@ -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

@ -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

@ -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')