Merge pull request #2543 from tchak/check-availability-endpoint
Check availability endpoint
This commit is contained in:
commit
b6f4a99c14
8 changed files with 107 additions and 0 deletions
|
@ -205,6 +205,20 @@ class Admin::ProceduresController < AdminController
|
||||||
render json: json_path_list
|
render json: json_path_list
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def check_availability
|
||||||
|
path = params[:procedure][:path]
|
||||||
|
procedure_id = params[:procedure][:id]
|
||||||
|
|
||||||
|
if procedure_id.present?
|
||||||
|
procedure = current_administrateur.procedures.find(procedure_id)
|
||||||
|
@available = procedure.path_available?(path)
|
||||||
|
@mine = procedure.path_is_mine?(path)
|
||||||
|
else
|
||||||
|
@available = !ProcedurePath.exists?(path: path)
|
||||||
|
@mine = ProcedurePath.mine?(current_administrateur, path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def delete_deliberation
|
def delete_deliberation
|
||||||
procedure = Procedure.find(params[:id])
|
procedure = Procedure.find(params[:id])
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,18 @@ module ApplicationHelper
|
||||||
# rubocop:enable Rails/OutputSafety
|
# rubocop:enable Rails/OutputSafety
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def disable_element(selector)
|
||||||
|
# rubocop:disable Rails/OutputSafety
|
||||||
|
raw("document.querySelector('#{selector}').disabled = true;")
|
||||||
|
# rubocop:enable Rails/OutputSafety
|
||||||
|
end
|
||||||
|
|
||||||
|
def enable_element(selector)
|
||||||
|
# rubocop:disable Rails/OutputSafety
|
||||||
|
raw("document.querySelector('#{selector}').disabled = false;")
|
||||||
|
# rubocop:enable Rails/OutputSafety
|
||||||
|
end
|
||||||
|
|
||||||
def current_email
|
def current_email
|
||||||
current_user&.email ||
|
current_user&.email ||
|
||||||
current_gestionnaire&.email ||
|
current_gestionnaire&.email ||
|
||||||
|
|
|
@ -120,6 +120,14 @@ class Procedure < ApplicationRecord
|
||||||
publiee_ou_archivee?
|
publiee_ou_archivee?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def path_available?(path)
|
||||||
|
!ProcedurePath.where.not(procedure: self).exists?(path: path)
|
||||||
|
end
|
||||||
|
|
||||||
|
def path_is_mine?(path)
|
||||||
|
ProcedurePath.where.not(procedure: self).mine?(administrateur, path)
|
||||||
|
end
|
||||||
|
|
||||||
# This method is needed for transition. Eventually this will be the same as brouillon?.
|
# This method is needed for transition. Eventually this will be the same as brouillon?.
|
||||||
def brouillon_avec_lien?
|
def brouillon_avec_lien?
|
||||||
Flipflop.publish_draft? && brouillon? && procedure_path.present?
|
Flipflop.publish_draft? && brouillon? && procedure_path.present?
|
||||||
|
|
|
@ -11,6 +11,11 @@ class ProcedurePath < ApplicationRecord
|
||||||
.find_or_initialize_by(path: path).validate
|
.find_or_initialize_by(path: path).validate
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.mine?(administrateur, path)
|
||||||
|
procedure_path = find_by(path: path)
|
||||||
|
procedure_path && administrateur.owns?(procedure_path)
|
||||||
|
end
|
||||||
|
|
||||||
def self.find_with_path(path)
|
def self.find_with_path(path)
|
||||||
joins(:procedure)
|
joins(:procedure)
|
||||||
.where.not(procedures: { aasm_state: :archivee })
|
.where.not(procedures: { aasm_state: :archivee })
|
||||||
|
|
8
app/views/admin/procedures/_unavailable.html.haml
Normal file
8
app/views/admin/procedures/_unavailable.html.haml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
- if mine
|
||||||
|
Ce lien est déjà utilisé par une de vos procédure.
|
||||||
|
%br
|
||||||
|
Si vous voulez l’utiliser, l’ancienne procédure sera archivée (plus accessible du public).
|
||||||
|
- else
|
||||||
|
Ce lien est déjà utilisé par une procédure.
|
||||||
|
%br
|
||||||
|
Vous ne pouvez pas l’utiliser car il appartient à un autre administrateur.
|
11
app/views/admin/procedures/check_availability.js.erb
Normal file
11
app/views/admin/procedures/check_availability.js.erb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<% if @available %>
|
||||||
|
<%= remove_element('.unavailable-path-message', inner: true) %>
|
||||||
|
<%= 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 %>
|
||||||
|
<% end %>
|
|
@ -179,6 +179,7 @@ Rails.application.routes.draw do
|
||||||
get 'procedures/archived' => 'procedures#archived'
|
get 'procedures/archived' => 'procedures#archived'
|
||||||
get 'procedures/draft' => 'procedures#draft'
|
get 'procedures/draft' => 'procedures#draft'
|
||||||
get 'procedures/path_list' => 'procedures#path_list'
|
get 'procedures/path_list' => 'procedures#path_list'
|
||||||
|
get 'procedures/available' => 'procedures#check_availability'
|
||||||
get 'profile' => 'profile#show', as: :profile
|
get 'profile' => 'profile#show', as: :profile
|
||||||
post 'renew_api_token' => 'profile#renew_api_token', as: :renew_api_token
|
post 'renew_api_token' => 'profile#renew_api_token', as: :renew_api_token
|
||||||
|
|
||||||
|
|
|
@ -721,4 +721,52 @@ describe Admin::ProceduresController, type: :controller do
|
||||||
it { expect(procedure.deliberation.attached?).to eq(false) }
|
it { expect(procedure.deliberation.attached?).to eq(false) }
|
||||||
it { expect(response).to redirect_to(edit_admin_procedure_path(procedure)) }
|
it { expect(response).to redirect_to(edit_admin_procedure_path(procedure)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "GET #check_availability" do
|
||||||
|
render_views
|
||||||
|
let(:procedure) { create(:procedure, :with_path, administrateur: admin) }
|
||||||
|
let(:params) {
|
||||||
|
{
|
||||||
|
procedure: {
|
||||||
|
path: path,
|
||||||
|
id: procedure.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let(:path) { generate(:published_path) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
get :check_availability, params: params, format: 'js'
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'self path' do
|
||||||
|
let(:path) { procedure.path }
|
||||||
|
|
||||||
|
it { expect(response.body).to include("innerHTML = ''") }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'available path' do
|
||||||
|
it { expect(response.body).to include("innerHTML = ''") }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'my path' do
|
||||||
|
let(:procedure_owned) { create(:procedure, :with_path, administrateur: admin) }
|
||||||
|
let(:path) { procedure_owned.path }
|
||||||
|
|
||||||
|
it {
|
||||||
|
expect(response.body).to include('Ce lien est déjà utilisé par une de vos procédure.')
|
||||||
|
expect(response.body).to include('Si vous voulez l’utiliser, l’ancienne procédure sera archivée')
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'unavailable path' do
|
||||||
|
let(:procedure_not_owned) { create(:procedure, :with_path, administrateur: create(:administrateur)) }
|
||||||
|
let(:path) { procedure_not_owned.path }
|
||||||
|
|
||||||
|
it {
|
||||||
|
expect(response.body).to include('Ce lien est déjà utilisé par une procédure.')
|
||||||
|
expect(response.body).to include('Vous ne pouvez pas l’utiliser car il appartient à un autre administrateur.')
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue