Do not attempt to archive already archived procedures

This commit is contained in:
Paul Chavard 2018-05-29 11:44:44 +02:00
parent a9b4ba2958
commit 86b2cb211e
4 changed files with 30 additions and 14 deletions

View file

@ -79,10 +79,7 @@ class Procedure < ApplicationRecord
procedure_path = ProcedurePath.find_by(path: path)
if procedure_path.present?
if procedure_path.procedure != self
procedure_path.procedure.archive!
procedure_path.update(procedure: self)
end
procedure_path.publish!(self)
else
ProcedurePath.create(procedure: self, administrateur: administrateur, path: path)
end

View file

@ -11,15 +11,24 @@ class ProcedurePath < ApplicationRecord
where(procedure: procedure).or(where(test_procedure: procedure)).last
end
def hide!(procedure)
if self.procedure == procedure
def hide!(new_procedure)
if procedure == new_procedure
update(procedure: nil)
end
if self.test_procedure == procedure
if test_procedure == new_procedure
update(test_procedure: nil)
end
if procedure.nil? && test_procedure.nil?
destroy
end
end
def publish!(new_procedure)
if procedure != new_procedure
if procedure&.publiee?
procedure.archive!
end
update(procedure: new_procedure)
end
end
end

View file

@ -365,6 +365,22 @@ describe Admin::ProceduresController, type: :controller do
end
end
context 'procedure path exists and has archived procedure' do
let(:procedure_path) { procedure2.path }
let(:procedure2) { create(:procedure, :archived, administrateur: admin) }
it 'publish the given procedure' do
expect(procedure.publiee?).to be_truthy
expect(procedure.path).to eq(procedure_path)
expect(response.status).to eq 302
expect(flash[:notice]).to have_content 'Procédure publiée'
end
it 'archive previous procedure' do
expect(procedure2.archivee?).to be_truthy
end
end
context 'procedure path exists and is not owned by current administrator' do
let(:procedure_path) { procedure3.path }
@ -605,7 +621,7 @@ describe Admin::ProceduresController, type: :controller do
end
it { expect(procedure.hidden_at).not_to be_nil }
it { expect(procedure.procedure_path.procedure).to be_nil }
it { expect(procedure.procedure_path).to be_nil }
end
context "when procedure has no path" do

View file

@ -164,12 +164,6 @@ describe Users::DossiersController, type: :controller do
it { expect(subject.status).to eq 200 }
end
context 'when procedure is hidden' do
let(:procedure) { create(:procedure, :hidden) }
it { expect(subject).to redirect_to(root_path) }
end
context 'when procedure path dose not exist' do
let(:path) { 'hello' }