Procedure: clone the deliberation

This commit is contained in:
simon lehericey 2018-05-31 11:00:22 +02:00
parent fa14db1cbf
commit 0e690c90cc
3 changed files with 25 additions and 10 deletions

View file

@ -200,15 +200,7 @@ class Procedure < ApplicationRecord
procedure.logo_secure_token = nil
procedure.remote_logo_url = self.logo_url
if notice.attached?
response = Typhoeus.get(notice.service_url, timeout: 5)
if response.success?
procedure.notice.attach(
io: StringIO.new(response.body),
filename: notice.filename
)
end
end
%i(notice deliberation).each { |attachment| clone_attachment(procedure, attachment) }
procedure.administrateur = admin
procedure.initiated_mail = initiated_mail&.dup
@ -351,6 +343,19 @@ class Procedure < ApplicationRecord
private
def clone_attachment(cloned_procedure, attachment_symbol)
attachment = send(attachment_symbol)
if attachment.attached?
response = Typhoeus.get(attachment.service_url, timeout: 5)
if response.success?
cloned_procedure.send(attachment_symbol).attach(
io: StringIO.new(response.body),
filename: attachment.filename
)
end
end
end
def check_juridique
if cadre_juridique.blank? && !deliberation.attached?
errors.add(:cadre_juridique, " : veuillez remplir le texte de loi ou la délibération")

View file

@ -473,7 +473,7 @@ describe Admin::ProceduresController, type: :controller do
end
describe 'PUT #clone' do
let!(:procedure) { create(:procedure, :with_notice, administrateur: admin) }
let!(:procedure) { create(:procedure, :with_notice, :with_deliberation, administrateur: admin) }
let(:params) { { procedure_id: procedure.id } }
subject { put :clone, params: params }
@ -491,6 +491,7 @@ describe Admin::ProceduresController, type: :controller do
expect(response).to redirect_to edit_admin_procedure_path(id: Procedure.last.id)
expect(Procedure.last.cloned_from_library).to be_falsey
expect(Procedure.last.notice.attached?).to be_truthy
expect(Procedure.last.deliberation.attached?).to be_truthy
expect(flash[:notice]).to have_content 'Procédure clonée'
end

View file

@ -135,6 +135,15 @@ FactoryBot.define do
end
end
trait :with_deliberation do
after(:create) do |procedure, _evaluator|
procedure.deliberation.attach(
io: StringIO.new('Hello World'),
filename: 'hello.txt'
)
end
end
trait :with_all_champs_mandatory do
after(:build) do |procedure, _evaluator|
tdcs = []