Implement check_availability route

This commit is contained in:
Paul Chavard 2018-09-04 13:53:18 +01:00
parent 14bf543976
commit 3f6cc668a7
7 changed files with 95 additions and 0 deletions

View file

@ -205,6 +205,20 @@ class Admin::ProceduresController < AdminController
render json: json_path_list
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
procedure = Procedure.find(params[:id])

View file

@ -120,6 +120,14 @@ class Procedure < ApplicationRecord
publiee_ou_archivee?
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?.
def brouillon_avec_lien?
Flipflop.publish_draft? && brouillon? && procedure_path.present?

View file

@ -11,6 +11,11 @@ class ProcedurePath < ApplicationRecord
.find_or_initialize_by(path: path).validate
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)
joins(:procedure)
.where.not(procedures: { aasm_state: :archivee })

View file

@ -0,0 +1,8 @@
- if mine
Ce lien est déjà utilisé par une de vos procédure.
%br
Si vous voulez lutiliser, lancienne 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 lutiliser car il appartient à un autre administrateur.

View 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 %>

View file

@ -179,6 +179,7 @@ Rails.application.routes.draw do
get 'procedures/archived' => 'procedures#archived'
get 'procedures/draft' => 'procedures#draft'
get 'procedures/path_list' => 'procedures#path_list'
get 'procedures/available' => 'procedures#check_availability'
get 'profile' => 'profile#show', as: :profile
post 'renew_api_token' => 'profile#renew_api_token', as: :renew_api_token

View file

@ -721,4 +721,52 @@ describe Admin::ProceduresController, type: :controller do
it { expect(procedure.deliberation.attached?).to eq(false) }
it { expect(response).to redirect_to(edit_admin_procedure_path(procedure)) }
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 lutiliser, lancienne 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 lutiliser car il appartient à un autre administrateur.')
}
end
end
end