From e582ff729c72a1d36e7bd928035ac378f041daa0 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 2 Jul 2019 15:38:23 +0200 Subject: [PATCH] Add aasm to dossiers state --- app/models/dossier.rb | 130 ++++++++++++++++++++++++------------ spec/models/dossier_spec.rb | 10 +-- 2 files changed, 93 insertions(+), 47 deletions(-) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 965c39554..021f750d9 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -40,6 +40,54 @@ class Dossier < ApplicationRecord accepts_nested_attributes_for :champs accepts_nested_attributes_for :champs_private + include AASM + + aasm whiny_persistence: true, column: :state, enum: true do + state :brouillon, initial: true + state :en_construction + state :en_instruction + state :accepte + state :refuse + state :sans_suite + + event :passer_en_construction, after: :after_passer_en_construction do + transitions from: :brouillon, to: :en_construction + end + + event :passer_en_instruction, after: :after_passer_en_instruction do + transitions from: :en_construction, to: :en_instruction + end + + event :passer_automatiquement_en_instruction, after: :after_passer_automatiquement_en_instruction do + transitions from: :en_construction, to: :en_instruction + end + + event :repasser_en_construction, after: :after_repasser_en_construction do + transitions from: :en_instruction, to: :en_construction + end + + event :accepter, after: :after_accepter do + transitions from: :en_instruction, to: :accepte + end + + event :accepter_automatiquement, after: :after_accepter_automatiquement do + transitions from: :en_construction, to: :accepte + end + + event :refuser, after: :after_refuser do + transitions from: :en_instruction, to: :refuse + end + + event :classer_sans_suite, after: :after_classer_sans_suite do + transitions from: :en_instruction, to: :sans_suite + end + + event :repasser_en_instruction, after: :after_repasser_en_instruction do + transitions from: :refuse, to: :en_instruction + transitions from: :sans_suite, to: :en_instruction + end + end + default_scope { where(hidden_at: nil) } scope :state_brouillon, -> { where(state: states.fetch(:brouillon)) } scope :state_not_brouillon, -> { where.not(state: states.fetch(:brouillon)) } @@ -282,63 +330,85 @@ class Dossier < ApplicationRecord DossierMailer.notify_deletion_to_user(deleted_dossier, user.email).deliver_later end - def passer_en_instruction!(gestionnaire) - en_instruction! + def after_passer_en_instruction(gestionnaire) gestionnaire.follow(self) log_dossier_operation(gestionnaire, :passer_en_instruction) end - def passer_automatiquement_en_instruction! - en_instruction! - + def after_passer_automatiquement_en_instruction log_automatic_dossier_operation(:passer_en_instruction) end - def repasser_en_construction!(gestionnaire) + def after_repasser_en_construction(gestionnaire) self.en_instruction_at = nil - en_construction! + save! log_dossier_operation(gestionnaire, :repasser_en_construction) end - def repasser_en_instruction!(gestionnaire) - update(state: Dossier.states.fetch(:en_instruction), processed_at: nil, motivation: nil) + def after_repasser_en_instruction(gestionnaire) + self.processed_at = nil + self.motivation = nil attestation&.destroy - DossierMailer.notify_revert_to_instruction(self).deliver_later + save! + DossierMailer.notify_revert_to_instruction(self).deliver_later log_dossier_operation(gestionnaire, :repasser_en_instruction) end - def accepter!(gestionnaire, motivation, justificatif = nil) + def after_accepter(gestionnaire, motivation, justificatif = nil) self.motivation = motivation - self.en_instruction_at ||= Time.zone.now + if justificatif self.justificatif_motivation.attach(justificatif) end - accepte! if attestation.nil? - update(attestation: build_attestation) + self.attestation = build_attestation end + save! NotificationMailer.send_closed_notification(self).deliver_later log_dossier_operation(gestionnaire, :accepter, self) end - def accepter_automatiquement! + def after_accepter_automatiquement self.en_instruction_at ||= Time.zone.now - accepte! - if attestation.nil? - update(attestation: build_attestation) + self.attestation = build_attestation end + save! NotificationMailer.send_closed_notification(self).deliver_later log_automatic_dossier_operation(:accepter, self) end + def after_refuser(gestionnaire, motivation, justificatif = nil) + self.motivation = motivation + + if justificatif + self.justificatif_motivation.attach(justificatif) + end + + save! + NotificationMailer.send_refused_notification(self).deliver_later + log_dossier_operation(gestionnaire, :refuser, self) + end + + def after_classer_sans_suite(gestionnaire, motivation, justificatif = nil) + self.motivation = motivation + + if justificatif + self.justificatif_motivation.attach(justificatif) + end + + save! + NotificationMailer.send_without_continuation_notification(self).deliver_later + log_dossier_operation(gestionnaire, :classer_sans_suite, self) + end + def hide!(administration) update(hidden_at: Time.zone.now) @@ -346,30 +416,6 @@ class Dossier < ApplicationRecord log_dossier_operation(administration, :supprimer, self) end - def refuser!(gestionnaire, motivation, justificatif = nil) - self.motivation = motivation - self.en_instruction_at ||= Time.zone.now - if justificatif - self.justificatif_motivation.attach(justificatif) - end - refuse! - - NotificationMailer.send_refused_notification(self).deliver_later - log_dossier_operation(gestionnaire, :refuser, self) - end - - def classer_sans_suite!(gestionnaire, motivation, justificatif = nil) - self.motivation = motivation - self.en_instruction_at ||= Time.zone.now - if justificatif - self.justificatif_motivation.attach(justificatif) - end - sans_suite! - - NotificationMailer.send_without_continuation_notification(self).deliver_later - log_dossier_operation(gestionnaire, :classer_sans_suite, self) - end - def check_mandatory_champs (champs + champs.select(&:repetition?).flat_map(&:champs)) .select(&:mandatory_and_blank?) diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index e9a5b49a8..b6a96f225 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -794,7 +794,7 @@ describe Dossier do end describe '#accepter!' do - let(:dossier) { create(:dossier) } + let(:dossier) { create(:dossier, :en_instruction) } let(:last_operation) { dossier.dossier_operation_logs.last } let(:operation_serialized) { JSON.parse(last_operation.serialized.download) } let!(:gestionnaire) { create(:gestionnaire) } @@ -813,7 +813,7 @@ describe Dossier do after { Timecop.return } it { expect(dossier.motivation).to eq('motivation') } - it { expect(dossier.en_instruction_at).to eq(now) } + it { expect(dossier.en_instruction_at).to eq(dossier.en_instruction_at) } it { expect(dossier.processed_at).to eq(now) } it { expect(dossier.state).to eq('accepte') } it { expect(last_operation.operation).to eq('accepter') } @@ -826,7 +826,7 @@ describe Dossier do end describe '#accepter_automatiquement!' do - let(:dossier) { create(:dossier) } + let(:dossier) { create(:dossier, :en_construction) } let(:last_operation) { dossier.dossier_operation_logs.last } let!(:now) { Time.zone.parse('01/01/2100') } let(:attestation) { Attestation.new } @@ -853,7 +853,7 @@ describe Dossier do end describe '#passer_en_instruction!' do - let(:dossier) { create(:dossier) } + let(:dossier) { create(:dossier, :en_construction) } let(:last_operation) { dossier.dossier_operation_logs.last } let(:operation_serialized) { JSON.parse(last_operation.serialized.download) } let(:gestionnaire) { create(:gestionnaire) } @@ -870,7 +870,7 @@ describe Dossier do end describe '#passer_automatiquement_en_instruction!' do - let(:dossier) { create(:dossier) } + let(:dossier) { create(:dossier, :en_construction) } let(:last_operation) { dossier.dossier_operation_logs.last } let(:operation_serialized) { JSON.parse(last_operation.serialized.download) } let(:gestionnaire) { create(:gestionnaire) }