add function "next_step" into dossier model
This commit is contained in:
parent
888ac8b336
commit
4b25696e48
2 changed files with 308 additions and 2 deletions
|
@ -1,10 +1,10 @@
|
|||
class Dossier < ActiveRecord::Base
|
||||
enum state: { draft: 'draft',
|
||||
submitted: 'submitted',
|
||||
proposed: 'proposed',
|
||||
reply: 'reply',
|
||||
updated: 'updated',
|
||||
confirmed: 'confirmed',
|
||||
filed: 'filed',
|
||||
deposited: 'deposited',
|
||||
processed: 'processed' }
|
||||
|
||||
has_one :etablissement
|
||||
|
@ -47,6 +47,59 @@ class Dossier < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def next_step! role, action
|
||||
unless ['propose', 'reply', 'update', 'comment', 'confirme', 'depose', 'process'].include?(action)
|
||||
fail 'action is not valid'
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue