Refactor assignement-related methods

This commit is contained in:
gregoirenovel 2018-02-20 11:24:32 +01:00
parent ac6ba40598
commit 3d10dbf0e4
3 changed files with 65 additions and 6 deletions

View file

@ -51,11 +51,17 @@ class Admin::AccompagnateursController < AdminController
case to
when ASSIGN
gestionnaire.assign_to_procedure(procedure)
flash.notice = "L'accompagnateur a bien été affecté"
if gestionnaire.assign_to_procedure(procedure)
flash.notice = "L'accompagnateur a bien été affecté"
else
flash.alert = "L'accompagnateur a déjà été affecté"
end
when NOT_ASSIGN
gestionnaire.remove_from_procedure(procedure)
flash.notice = "L'accompagnateur a bien été désaffecté"
if gestionnaire.remove_from_procedure(procedure)
flash.notice = "L'accompagnateur a bien été désaffecté"
else
flash.alert = "L'accompagnateur a déjà été désaffecté"
end
end
redirect_to admin_procedure_accompagnateurs_path, procedure_id: params[:procedure_id]

View file

@ -52,11 +52,16 @@ class Gestionnaire < ActiveRecord::Base
end
def assign_to_procedure(procedure)
AssignTo.create(gestionnaire: self, procedure: procedure)
begin
procedures << procedure
true
rescue ActiveRecord::RecordNotUnique
false
end
end
def remove_from_procedure(procedure)
AssignTo.where(gestionnaire: self, procedure: procedure).delete_all
!!(procedure.in?(procedures) && procedures.destroy(procedure))
end
def last_week_overview

View file

@ -64,6 +64,54 @@ describe Gestionnaire, type: :model do
end
end
describe "#assign_to_procedure" do
subject { gestionnaire.assign_to_procedure(procedure_to_assign) }
context "with a procedure not already assigned" do
let(:procedure_to_assign) { procedure_3 }
it { is_expected.to be_truthy }
it { expect{ subject }.to change(gestionnaire.procedures, :count) }
end
context "with an already assigned procedure" do
let(:procedure_to_assign) { procedure }
it { is_expected.to be_falsey }
it { expect{ subject }.not_to change(gestionnaire.procedures, :count) }
end
end
describe "#remove_from_procedure" do
subject { gestionnaire.remove_from_procedure(procedure_to_remove) }
context "with an assigned procedure" do
let(:procedure_to_remove) { procedure }
let!(:procedure_presentation) { procedure_assign.procedure_presentation }
it { is_expected.to be_truthy }
describe "consequences" do
before do
procedure_assign.build_procedure_presentation
procedure_assign.save
subject
end
it "removes the assign_to and procedure_presentation" do
expect(AssignTo.where(id: procedure_assign).count).to eq(0)
expect(ProcedurePresentation.where(assign_to_id: procedure_assign.id).count).to eq(0)
end
end
end
context "with an already unassigned procedure" do
let(:procedure_to_remove) { procedure_3 }
it { is_expected.to be_falsey }
end
end
context 'unified login' do
it 'syncs credentials to associated user' do
gestionnaire = create(:gestionnaire)