add function "next_step" into dossier

This commit is contained in:
Xavier J 2015-09-24 10:27:14 +02:00
parent a9109f8b2b
commit ded5a6a28f
2 changed files with 332 additions and 26 deletions

View file

@ -1,11 +1,11 @@
class Dossier < ActiveRecord::Base
enum state: { draft: 'draft',
submitted: 'submitted',
reply: 'reply',
updated: 'updated',
confirmed: 'confirmed',
filed: 'filed',
processed: 'processed' }
proposed: 'proposed',
reply: 'reply',
updated: 'updated',
confirmed: 'confirmed',
deposited: 'deposited',
processed: 'processed' }
has_one :etablissement
has_one :entreprise
@ -29,28 +29,81 @@ class Dossier < ActiveRecord::Base
validates :date_previsionnelle, presence: true, allow_blank: false, unless: Proc.new { description.nil? }
def retrieve_piece_justificative_by_type(type)
pieces_justificatives.where(type_de_piece_justificative_id: type).last
end
def retrieve_piece_justificative_by_type(type)
pieces_justificatives.where(type_de_piece_justificative_id: type).last
end
def build_default_pieces_justificatives
procedure.types_de_piece_justificative.each do |type_de_piece_justificative|
PieceJustificative.create(type_de_piece_justificative_id: type_de_piece_justificative.id, dossier_id: id)
end
end
def build_default_pieces_justificatives
procedure.types_de_piece_justificative.each do |type_de_piece_justificative|
PieceJustificative.create(type_de_piece_justificative_id: type_de_piece_justificative.id, dossier_id: id)
end
end
def sous_domaine
if Rails.env.production?
'tps'
else
'tps-dev'
end
end
def sous_domaine
if Rails.env.production?
'tps'
else
'tps-dev'
end
end
private
def next_step! role, action
unless ['propose', 'reply', 'update', 'comment', 'confirme', 'depose', 'process'].include?(action)
fail 'action is not valid'
end
def build_default_cerfa
build_cerfa
true
end
unless ['user', 'gestionnaire'].include?(role)
fail 'role is not valid'
end
if role == 'user'
case action
when 'propose'
if draft?
proposed!
end
when 'depose'
if confirmed?
deposited!
end
when 'update'
if reply?
updated!
end
when 'comment'
if reply?
updated!
end
end
elsif role == 'gestionnaire'
case action
when 'comment'
if updated?
reply!
elsif proposed?
reply!
end
when 'confirme'
if updated?
confirmed!
elsif reply?
confirmed!
elsif proposed?
confirmed!
end
when 'process'
if deposited?
processed!
end
end
end
state
end
private
def build_default_cerfa
build_cerfa
true
end
end

View file

@ -115,5 +115,258 @@ describe Dossier do
end
end
end
#TODO revoir le nommage
describe '#next_step' do
let(:dossier) { create(:dossier) }
let(:role) { 'user' }
let(:action) { 'propose' }
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 draft' do
before do
dossier.draft!
end
context 'when user is connected' do
let(:role) { 'user' }
context 'when he updates dossier informations' do
let(:action) {'update'}
it { is_expected.to eq('draft') }
end
context 'when he posts a comment' do
let(:action) {'comment'}
it { is_expected.to eq('draft') }
end
context 'when he proposes a dossier' do
let(:action) { 'propose' }
it { is_expected.to eq('proposed') }
end
end
end
context 'when dossier is at state proposed' do
before do
dossier.proposed!
end
context 'when user is connect' do
let(:role) { 'user' }
context 'when is update dossier informations' do
let(:action) { 'update' }
it {is_expected.to eq('proposed')}
end
context 'when is post a comment' do
let(:action) { 'comment' }
it {is_expected.to eq('proposed')}
end
end
context 'when gestionnaire is connect' do
let(:role) { 'gestionnaire' }
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('reply')}
end
context 'when is confirmed the dossier' do
let(:action) { 'confirme' }
it {is_expected.to eq('confirmed')}
end
end
end
context 'when dossier is at state reply' do
before do
dossier.reply!
end
context 'when user is connect' do
let(:role) { 'user' }
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('updated') }
end
context 'when is updated dossier informations' do
let(:action) { 'update' }
it {
is_expected.to eq('updated')
}
end
end
context 'when gestionnaire is connect' do
let(:role) { 'gestionnaire' }
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('reply')}
end
context 'when is confirmed the dossier' do
let(:action) { 'confirme' }
it {is_expected.to eq('confirmed')}
end
end
end
context 'when dossier is at state updated' do
before do
dossier.updated!
end
context 'when user is connect' do
let(:role) { 'user' }
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('updated')}
end
context 'when is updated dossier informations' do
let(:action) { 'update' }
it { is_expected.to eq('updated')}
end
end
context 'when gestionnaire is connect' do
let(:role) { 'gestionnaire' }
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('reply')}
end
context 'when is confirmed the dossier' do
let(:action) { 'confirme' }
it {is_expected.to eq('confirmed')}
end
end
end
context 'when dossier is at state confirmed' do
before do
dossier.confirmed!
end
context 'when user is connect' do
let(:role) { 'user' }
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('confirmed') }
end
context 'when is deposed the dossier' do
let(:action) { 'depose' }
it { is_expected.to eq('deposited') }
end
end
context 'when gestionnaire is connect' do
let(:role) { 'gestionnaire' }
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('confirmed')}
end
end
end
context 'when dossier is at state deposited' do
before do
dossier.deposited!
end
context 'when user is connect' do
let(:role) { 'user' }
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('deposited') }
end
end
context 'when gestionnaire is connect' do
let(:role) { 'gestionnaire' }
context 'when is post a comment' do
let(:action) { 'comment' }
it {is_expected.to eq('deposited')}
end
context 'when is processed the dossier' do
let(:action) { 'process' }
it {is_expected.to eq('processed')}
end
end
end
context 'when dossier is at state processed' do
before do
dossier.processed!
end
context 'when user is connect' do
let(:role) { 'user' }
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('processed')}
end
end
context 'when gestionnaire is connect' do
let(:role) { 'gestionnaire' }
context 'when is post a comment' do
let(:action) { 'comment' }
it { is_expected.to eq('processed')}
end
end
end
end
end
end