Merge pull request #4029 from tchak/dossier-asm

Add aasm to dossiers state
This commit is contained in:
Paul Chavard 2019-07-03 14:12:15 +02:00 committed by GitHub
commit 87902a05d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 93 additions and 47 deletions

View file

@ -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?)

View file

@ -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) }