Move a service to the gestionnaire model

This commit is contained in:
gregoirenovel 2018-02-19 11:20:57 +01:00
parent deeda0e540
commit 1b1460c191
6 changed files with 16 additions and 57 deletions

View file

@ -46,9 +46,12 @@ class Admin::AccompagnateursController < AdminController
procedure = Procedure.find(params[:procedure_id])
to = params[:to]
accompagnateur_service = AccompagnateurService.new gestionnaire, procedure, to
accompagnateur_service.change_assignement!
case to
when 'assign'
gestionnaire.assign_to_procedure(procedure)
when 'not_assign'
gestionnaire.remove_from_procedure(procedure)
end
flash.notice = "Assignement effectué"
redirect_to admin_procedure_accompagnateurs_path, procedure_id: params[:procedure_id]

View file

@ -47,6 +47,14 @@ class Gestionnaire < ActiveRecord::Base
procedures.find_by(id: procedure_id).present?
end
def assign_to_procedure(procedure)
AssignTo.create(gestionnaire: self, procedure: procedure)
end
def remove_from_procedure(procedure)
AssignTo.where(gestionnaire: self, procedure: procedure).delete_all
end
def last_week_overview
start_date = DateTime.now.beginning_of_week

View file

@ -1,19 +0,0 @@
class AccompagnateurService
ASSIGN = 'assign'
NOT_ASSIGN = 'not_assign'
def initialize accompagnateur, procedure, to
@accompagnateur = accompagnateur
@procedure = procedure
@to = to
end
def change_assignement!
case @to
when ASSIGN
AssignTo.create(gestionnaire: @accompagnateur, procedure: @procedure)
when NOT_ASSIGN
AssignTo.where(gestionnaire: @accompagnateur, procedure: @procedure).delete_all
end
end
end

View file

@ -9,7 +9,7 @@
- @accompagnateurs_assign.each do |accompagnateur|
%tr
%td.col-md-1.col-lg-1.col-sm-1.col-xs-1.col-sm-1.col-xs-1.center
%a.btn.btn-primary{ href: "#{admin_procedure_accompagnateurs_path(procedure_id: @procedure.id, accompagnateur_id: accompagnateur.id, to: AccompagnateurService::NOT_ASSIGN)}", 'data-method' => 'put' }
%a.btn.btn-primary{ href: "#{admin_procedure_accompagnateurs_path(procedure_id: @procedure.id, accompagnateur_id: accompagnateur.id, to: 'not_assign')}", 'data-method' => 'put' }
.fa.fa-arrow-left
%td{ style: 'padding-top: 11px; font-size: 15px; text-align: right;' }= accompagnateur.email

View file

@ -16,7 +16,7 @@
%tr
%td.col-xs-11{ style: 'padding-top: 11px; font-size: 15px;' }= accompagnateur.email
%td.center
%a.btn.btn-success.gestionnaire-affectation{ href: "#{admin_procedure_accompagnateurs_path(procedure_id: @procedure.id, accompagnateur_id: accompagnateur.id, to: AccompagnateurService::ASSIGN)}", 'data-method' => 'put' }
%a.btn.btn-success.gestionnaire-affectation{ href: "#{admin_procedure_accompagnateurs_path(procedure_id: @procedure.id, accompagnateur_id: accompagnateur.id, to: 'assign')}", 'data-method' => 'put' }
.fa.fa-arrow-right

View file

@ -1,33 +0,0 @@
require 'spec_helper'
describe AccompagnateurService do
let(:procedure) { create :procedure, :published }
let(:accompagnateur) { create :gestionnaire }
let(:accompagnateur_service) { AccompagnateurService.new accompagnateur, procedure, to }
describe '#change_assignement!' do
subject { accompagnateur_service.change_assignement! }
context 'when accompagnateur is not assign at the procedure' do
let(:to) { AccompagnateurService::ASSIGN }
before do
subject
end
it { expect(accompagnateur.procedures).to include procedure }
end
context 'when accompagnateur is assign at the procedure' do
let(:to) { AccompagnateurService::NOT_ASSIGN }
before do
create :assign_to, gestionnaire: accompagnateur, procedure: procedure
subject
end
it { expect(accompagnateur.procedures).not_to include procedure }
end
end
end