From 9c354e18eba11a1184186705d55b601c17354ac9 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Tue, 5 Dec 2017 17:43:32 +0100 Subject: [PATCH] [fix 363] Dossier: remove next_step! --- .../backoffice/dossiers_controller.rb | 13 ++++--- .../new_gestionnaire/dossiers_controller.rb | 13 ++++--- app/models/dossier.rb | 35 ++---------------- spec/models/dossier_spec.rb | 36 +------------------ 4 files changed, 21 insertions(+), 76 deletions(-) diff --git a/app/controllers/backoffice/dossiers_controller.rb b/app/controllers/backoffice/dossiers_controller.rb index d9582eeda..3273384ee 100644 --- a/app/controllers/backoffice/dossiers_controller.rb +++ b/app/controllers/backoffice/dossiers_controller.rb @@ -115,20 +115,25 @@ class Backoffice::DossiersController < Backoffice::DossiersListController case params[:process_action] when "refuse" - next_step = "refuse" + dossier.refuse! notice = "Dossier considéré comme refusé." template = dossier.procedure.refused_mail_template when "without_continuation" - next_step = "without_continuation" + dossier.sans_suite! notice = "Dossier considéré comme sans suite." template = dossier.procedure.without_continuation_mail_template when "close" - next_step = "close" + dossier.attestation = dossier.build_attestation + dossier.accepte! notice = "Dossier traité avec succès." template = dossier.procedure.closed_mail_template end - dossier.next_step! 'gestionnaire', next_step, motivation + if motivation + dossier.motivation = motivation + end + + dossier.save # needed to force Carrierwave to provide dossier.attestation.pdf.read # when the Feature.remote_storage is true, otherwise pdf.read is a closed stream. diff --git a/app/controllers/new_gestionnaire/dossiers_controller.rb b/app/controllers/new_gestionnaire/dossiers_controller.rb index 904b04a37..1041bc2e4 100644 --- a/app/controllers/new_gestionnaire/dossiers_controller.rb +++ b/app/controllers/new_gestionnaire/dossiers_controller.rb @@ -81,20 +81,25 @@ module NewGestionnaire case params[:process_action] when "refuser" - next_step = "refuse" + dossier.refuse! notice = "Dossier considéré comme refusé." template = procedure.refused_mail_template when "classer_sans_suite" - next_step = "without_continuation" + dossier.sans_suite! notice = "Dossier considéré comme sans suite." template = procedure.without_continuation_mail_template when "accepter" - next_step = "close" + dossier.attestation = dossier.build_attestation + dossier.accepte! notice = "Dossier traité avec succès." template = procedure.closed_mail_template end - dossier.next_step!('gestionnaire', next_step, motivation) + if motivation + dossier.motivation = motivation + end + + dossier.save # needed to force Carrierwave to provide dossier.attestation.pdf.read # when the Feature.remote_storage is true, otherwise pdf.read is a closed stream. diff --git a/app/models/dossier.rb b/app/models/dossier.rb index ff37d71d7..e3936ae4d 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -144,37 +144,6 @@ class Dossier < ActiveRecord::Base champs.joins(', types_de_piece_justificative').where("pieces_justificatives.type_de_piece_justificative_id = types_de_piece_justificative.id AND types_de_piece_justificative.procedure_id = #{procedure.id}").order('order_place ASC') end - def next_step!(action, motivation = nil) - unless %w(receive refuse without_continuation close).include?(action) - fail 'action is not valid' - end - - case action - when 'close' - if en_instruction? - self.attestation = build_attestation - save - - accepte! - end - when 'refuse' - if en_instruction? - refuse! - end - when 'without_continuation' - if en_instruction? - sans_suite! - end - end - - if motivation - self.motivation = motivation - save - end - - state - end - def brouillon? BROUILLON.include?(state) end @@ -355,14 +324,14 @@ class Dossier < ActiveRecord::Base end end - private - def build_attestation if procedure.attestation_template.present? && procedure.attestation_template.activated? procedure.attestation_template.attestation_for(self) end end + private + def update_state_dates if en_construction? && !self.en_construction_at self.en_construction_at = DateTime.now diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 5b1b886a4..c0b468df5 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -116,40 +116,6 @@ describe Dossier do end end end - - describe '#next_step' do - let(:dossier) { create(:dossier) } - let(:role) { 'user' } - let(:action) { 'receive' } - - subject { dossier.next_step! role, action } - - context 'when action is not valid' do - let(:action) { 'test' } - it { expect { subject }.to raise_error('action is not valid') } - end - - context 'when role is not valid' do - let(:role) { 'test' } - it { expect { subject }.to raise_error('role is not valid') } - end - - context 'when dossier is at state en_instruction' do - before do - dossier.en_instruction! - end - - context 'when gestionnaire is connect' do - let(:role) { 'gestionnaire' } - - context 'when he closes the dossier' do - let(:action) { 'close' } - - it { is_expected.to eq('accepte') } - end - end - end - end end describe '#cerfa_available?' do @@ -658,7 +624,7 @@ describe Dossier do let(:procedure) { create(:procedure, attestation_template: attestation_template) } before :each do - dossier.next_step!('gestionnaire', 'close') + dossier.attestation = dossier.build_attestation dossier.reload end