Merge pull request #1447 from betagouv/remove-service

Remove service
This commit is contained in:
gregoirenovel 2018-02-23 17:18:32 +01:00 committed by GitHub
commit e8c2e8a723
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 95 additions and 64 deletions

View file

@ -4,6 +4,9 @@ class Admin::AccompagnateursController < AdminController
before_action :retrieve_procedure
ASSIGN = 'assign'
NOT_ASSIGN = 'not_assign'
def show
assign_scope = @procedure.gestionnaires
@ -46,11 +49,21 @@ class Admin::AccompagnateursController < AdminController
procedure = Procedure.find(params[:procedure_id])
to = params[:to]
accompagnateur_service = AccompagnateurService.new gestionnaire, procedure, to
case to
when ASSIGN
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
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
accompagnateur_service.change_assignement!
flash.notice = "Assignement effectué"
redirect_to admin_procedure_accompagnateurs_path, procedure_id: params[:procedure_id]
end
end

View file

@ -6,7 +6,7 @@ module NewGestionnaire
ITEMS_PER_PAGE = 25
def index
@procedures = current_gestionnaire.procedures.order(archived_at: :desc, published_at: :desc)
@procedures = current_gestionnaire.visible_procedures.order(archived_at: :desc, published_at: :desc)
dossiers = current_gestionnaire.dossiers
@dossiers_count_per_procedure = dossiers.all_state.group(:procedure_id).reorder(nil).count
@ -202,7 +202,7 @@ module NewGestionnaire
end
def redirect_to_avis_if_needed
if current_gestionnaire.procedures.count == 0 && current_gestionnaire.avis.count > 0
if current_gestionnaire.visible_procedures.count == 0 && current_gestionnaire.avis.count > 0
redirect_to gestionnaire_avis_index_path
end
end

View file

@ -5,7 +5,7 @@ class Gestionnaire < ActiveRecord::Base
has_and_belongs_to_many :administrateurs
has_many :assign_to, dependent: :destroy
has_many :procedures, -> { publiees_ou_archivees }, through: :assign_to
has_many :procedures, through: :assign_to
has_many :dossiers, -> { state_not_brouillon }, through: :procedures
has_many :follows
has_many :followed_dossiers, through: :follows, source: :dossier
@ -14,6 +14,10 @@ class Gestionnaire < ActiveRecord::Base
include CredentialsSyncableConcern
def visible_procedures
procedures.publiees_ou_archivees
end
def procedure_filter
procedure_id = self[:procedure_filter]
if procedures.find_by(id: procedure_id).present?
@ -47,6 +51,19 @@ class Gestionnaire < ActiveRecord::Base
procedures.find_by(id: procedure_id).present?
end
def assign_to_procedure(procedure)
begin
procedures << procedure
true
rescue ActiveRecord::RecordNotUnique
false
end
end
def remove_from_procedure(procedure)
!!(procedure.in?(procedures) && procedures.destroy(procedure))
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' }
= link_to "#{admin_procedure_accompagnateurs_path(procedure_id: @procedure.id, accompagnateur_id: accompagnateur.id, to: Admin::AccompagnateursController::NOT_ASSIGN)}", class: "btn btn-primary", 'data-method' => 'put' do
.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' }
= link_to "#{admin_procedure_accompagnateurs_path(procedure_id: @procedure.id, accompagnateur_id: accompagnateur.id, to: Admin::AccompagnateursController::ASSIGN)}", class: "btn btn-success gestionnaire-affectation", 'data-method' => 'put' do
.fa.fa-arrow-right

View file

@ -10,7 +10,7 @@
- if nav_bar_profile == :gestionnaire && gestionnaire_signed_in?
- current_url = request.path_info
%ul.header-tabs
- if current_gestionnaire.procedures.count > 0
- if current_gestionnaire.visible_procedures.count > 0
%li
= link_to "Procédures", gestionnaire_procedures_path, class: (controller_name != 'avis') ? "tab-link active" : 'tab-link'
- if current_gestionnaire.avis.count > 0

View file

@ -0,0 +1,5 @@
class AddUniqueIndexOnAssignTosGestionnaireIdProcedureId < ActiveRecord::Migration[5.2]
def change
add_index :assign_tos, [:gestionnaire_id, :procedure_id], unique: true
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_02_09_133452) do
ActiveRecord::Schema.define(version: 2018_02_19_170553) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -84,6 +84,7 @@ ActiveRecord::Schema.define(version: 2018_02_09_133452) do
create_table "assign_tos", id: :serial, force: :cascade do |t|
t.integer "gestionnaire_id"
t.integer "procedure_id"
t.index ["gestionnaire_id", "procedure_id"], name: "index_assign_tos_on_gestionnaire_id_and_procedure_id", unique: true
t.index ["gestionnaire_id"], name: "index_assign_tos_on_gestionnaire_id"
t.index ["procedure_id"], name: "index_assign_tos_on_procedure_id"
end

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)
@ -188,7 +236,6 @@ describe Gestionnaire, type: :model do
end
describe "procedure_presentation_for_procedure_id" do
let!(:procedure_assign_2) { create :assign_to, gestionnaire: gestionnaire, procedure: procedure_2 }
let!(:pp) { ProcedurePresentation.create(assign_to: procedure_assign) }
it { expect(gestionnaire.procedure_presentation_for_procedure_id(procedure.id)).to eq(pp) }

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