Fix bug and change the implementation to create new preference list column when new gestionnarire is affect on a procedure

This commit is contained in:
Xavier J 2016-10-07 12:10:58 +02:00
parent 52bac4f3f9
commit 7c2bcd0b64
7 changed files with 192 additions and 23 deletions

View file

@ -27,9 +27,10 @@ class Admin::AccompagnateursController < AdminController
procedure = Procedure.find(params[:procedure_id])
to = params[:to]
accompagnateur_service = AccompagnateurService.new gestionnaire, procedure, to
AccompagnateurService.change_assignement! gestionnaire, procedure, to
AccompagnateurService.build_default_column gestionnaire, procedure, to
accompagnateur_service.change_assignement!
accompagnateur_service.build_default_column
flash.notice = "Assignement effectué"
redirect_to admin_procedure_accompagnateurs_path, procedure_id: params[:procedure_id]

View file

@ -33,9 +33,9 @@ class Gestionnaire < ActiveRecord::Base
Follow.where(gestionnaire_id: id, dossier_id: dossier_id).any?
end
def build_default_preferences_list_dossier
def build_default_preferences_list_dossier procedure_id=nil
PreferenceListDossier.available_columns_for.each do |table|
PreferenceListDossier.available_columns_for(procedure_id).each do |table|
table.second.each do |column|
if valid_couple_table_attr? table.first, column.first
@ -47,6 +47,7 @@ class Gestionnaire < ActiveRecord::Base
bootstrap_lg: column.second[:bootstrap_lg],
order: nil,
filter: nil,
procedure_id: procedure_id,
gestionnaire: self
)
end

View file

@ -2,23 +2,24 @@ class AccompagnateurService
ASSIGN = 'assign'
NOT_ASSIGN = 'not_assign'
def self.change_assignement! accompagnateur, procedure, to
if to == ASSIGN
AssignTo.create(gestionnaire: accompagnateur, procedure: procedure)
elsif to == NOT_ASSIGN
AssignTo.delete_all(gestionnaire: accompagnateur, procedure: procedure)
def initialize accompagnateur, procedure, to
@accompagnateur = accompagnateur
@procedure = procedure
@to = to
end
def change_assignement!
if @to == ASSIGN
AssignTo.create(gestionnaire: @accompagnateur, procedure: @procedure)
elsif @to == NOT_ASSIGN
AssignTo.delete_all(gestionnaire: @accompagnateur, procedure: @procedure)
end
end
def self.build_default_column accompagnateur, procedure, to
return unless to == ASSIGN
return unless PreferenceListDossier.where(gestionnaire: accompagnateur, procedure: procedure).empty?
def build_default_column
return unless @to == ASSIGN
return unless PreferenceListDossier.where(gestionnaire: @accompagnateur, procedure: @procedure).empty?
accompagnateur.preference_list_dossiers.each do |pref|
clone = pref.dup
clone.procedure = procedure
clone.save
end
@accompagnateur.build_default_preferences_list_dossier @procedure.id
end
end

View file

@ -0,0 +1,163 @@
class ResetAllPreferenceListDossier < ActiveRecord::Migration
class PreferenceListDossier < ActiveRecord::Base
belongs_to :gestionnaire
belongs_to :procedure
def self.available_columns_for procedure_id = nil
columns = {
dossier: columns_dossier,
procedure: columns_procedure,
entreprise: columns_entreprise,
etablissement: columns_etablissement,
user: columns_user,
france_connect: columns_france_connect
}
columns
end
private
def self.columns_dossier
table = nil
{
dossier_id: create_column('ID', table, 'id', 'id', 1),
created_at: create_column('Créé le', table, 'created_at', 'first_creation', 2),
updated_at: create_column('Mise à jour le', table, 'updated_at', 'last_update', 2),
state: create_column('Statut', table, 'state', 'display_state', 1)
}
end
def self.columns_procedure
table = 'procedure'
{
libelle: create_column('Libellé procédure', table, 'libelle', 'libelle', 4),
organisation: create_column('Organisation', table, 'organisation', 'organisation', 3),
direction: create_column('Direction', table, 'direction', 'direction', 3)
}
end
def self.columns_entreprise
table = 'entreprise'
{
siren: create_column('SIREN', table, 'siren', 'siren', 2),
forme_juridique: create_column('Forme juridique', table, 'forme_juridique', 'forme_juridique', 3),
nom_commercial: create_column('Nom commercial', table, 'nom_commercial', 'nom_commercial', 3),
raison_sociale: create_column('Raison sociale', table, 'raison_sociale', 'raison_sociale', 3),
siret_siege_social: create_column('SIRET siège social', table, 'siret_siege_social', 'siret_siege_social', 2),
date_creation: create_column('Date de création', table, 'date_creation', 'date_creation', 2),
}
end
def self.columns_etablissement
table = 'etablissement'
{
siret: create_column('SIRET', table, 'siret', 'siret', 2),
libelle: create_column('Nom établissement', table, 'libelle_naf', 'libelle_naf', 3),
code_postal: create_column('Code postal', table, 'code_postal', 'code_postal', 1)
}
end
def self.columns_user
table = 'user'
{
email: create_column('Email', table, 'email', 'email', 2)
}
end
def self.columns_france_connect
table = 'france_connect_information'
{
gender: create_column('Civilité (FC)', table, 'gender', 'gender_fr', 1),
given_name: create_column('Prénom (FC)', table, 'given_name', 'given_name', 2),
family_name: create_column('Nom (FC)', table, 'family_name', 'family_name', 2)
}
end
def self.create_column libelle, table, attr, attr_decorate, bootstrap_lg
{
libelle: libelle,
table: table,
attr: attr,
attr_decorate: attr_decorate,
bootstrap_lg: bootstrap_lg,
order: nil,
filter: nil
}
end
end
class Gestionnaire < ActiveRecord::Base
has_many :assign_to, dependent: :destroy
has_many :procedures, through: :assign_to
def build_default_preferences_list_dossier procedure_id=nil
PreferenceListDossier.available_columns_for(procedure_id).each do |table|
table.second.each do |column|
if valid_couple_table_attr? table.first, column.first
PreferenceListDossier.create(
libelle: column.second[:libelle],
table: column.second[:table],
attr: column.second[:attr],
attr_decorate: column.second[:attr_decorate],
bootstrap_lg: column.second[:bootstrap_lg],
order: nil,
filter: nil,
procedure_id: procedure_id,
gestionnaire: self
)
end
end
end
end
private
def valid_couple_table_attr? table, column
couples = [{
table: :dossier,
column: :dossier_id
}, {
table: :procedure,
column: :libelle
}, {
table: :etablissement,
column: :siret
}, {
table: :entreprise,
column: :raison_sociale
}, {
table: :dossier,
column: :state
}]
couples.include?({table: table, column: column})
end
end
class Procedure < ActiveRecord::Base
has_many :assign_to, dependent: :destroy
has_many :gestionnaires, through: :assign_to
end
def change
PreferenceListDossier.delete_all
Procedure.all.each do |procedure|
procedure.gestionnaires.each do |gestionnaire|
gestionnaire.build_default_preferences_list_dossier procedure.id
end
end
Gestionnaire.all.each do |gestionnaire|
gestionnaire.build_default_preferences_list_dossier
end
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161006085422) do
ActiveRecord::Schema.define(version: 20161007095443) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

View file

@ -5,9 +5,11 @@ describe AccompagnateurService do
let(:procedure) { create :procedure }
let(:accompagnateur) { create :gestionnaire }
let(:accompagnateur_service) { AccompagnateurService.new accompagnateur, procedure, to}
describe '#change_assignement!' do
subject { AccompagnateurService.change_assignement! accompagnateur, procedure, to }
subject { accompagnateur_service.change_assignement! }
context 'when accompagnateur is not assign at the procedure' do
let(:to) { AccompagnateurService::ASSIGN }
@ -33,7 +35,7 @@ describe AccompagnateurService do
describe '#build_default_column' do
subject { AccompagnateurService.build_default_column accompagnateur, procedure, to }
subject { accompagnateur_service.build_default_column }
context 'when to is not assign' do
let(:to) { AccompagnateurService::NOT_ASSIGN }

View file

@ -4,13 +4,14 @@ describe DossiersListGestionnaireService do
let(:gestionnaire) { create :gestionnaire }
let(:liste) { 'a_traiter' }
let(:dossier) { create :dossier }
let(:accompagnateur_service) { AccompagnateurService.new gestionnaire, procedure, 'assign'}
describe '#default_sort' do
let(:procedure) { dossier.procedure }
before do
AccompagnateurService.change_assignement! gestionnaire, procedure, 'assign'
AccompagnateurService.build_default_column gestionnaire, procedure, 'assign'
accompagnateur_service.change_assignement!
accompagnateur_service.build_default_column
gestionnaire.reload
end