[Fix #2584] Move Field related code to a service

This commit is contained in:
Frederic Merizen 2018-09-14 11:15:59 +02:00
parent 36994ab697
commit 63fc0b3bc3
4 changed files with 153 additions and 145 deletions

View file

@ -67,10 +67,10 @@ module NewGestionnaire
@archived_dossiers
end
sorted_ids = sorted_ids(@dossiers)
sorted_ids = DossierFieldService.sorted_ids(@dossiers, procedure_presentation, current_gestionnaire)
if @current_filters.count > 0
filtered_ids = filtered_ids(@dossiers)
filtered_ids = DossierFieldService.filtered_ids(@dossiers, current_filters)
filtered_sorted_ids = sorted_ids.select { |id| filtered_ids.include?(id) }
else
filtered_sorted_ids = sorted_ids
@ -217,81 +217,6 @@ module NewGestionnaire
end
end
def filtered_ids(dossiers)
current_filters.map do |filter|
case filter['table']
when 'self'
dossiers.where("? ILIKE ?", filter['column'], "%#{filter['value']}%")
when 'france_connect_information'
dossiers
.includes(user: :france_connect_information)
.where("? ILIKE ?", "france_connect_informations.#{filter['column']}", "%#{filter['value']}%")
when 'type_de_champ', 'type_de_champ_private'
relation = filter['table'] == 'type_de_champ' ? :champs : :champs_private
dossiers
.includes(relation)
.where("champs.type_de_champ_id = ?", filter['column'].to_i)
.where("champs.value ILIKE ?", "%#{filter['value']}%")
when 'entreprise'
table = 'etablissement'
if filter['column'] == 'date_creation'
date = filter['value'].to_date rescue nil
dossiers
.includes(table)
.where("#{table.pluralize}.entreprise_#{filter['column']} = ?", date)
else
dossiers
.includes(table)
.where("#{table.pluralize}.entreprise_#{filter['column']} ILIKE ?", "%#{filter['value']}%")
end
when 'user', 'etablissement'
dossiers
.includes(filter['table'])
.where("#{filter['table'].pluralize}.#{filter['column']} ILIKE ?", "%#{filter['value']}%")
end.pluck(:id)
end.reduce(:&)
end
def sorted_ids(dossiers)
table = procedure_presentation.sort['table']
column = procedure_presentation.sort['column']
order = procedure_presentation.sort['order']
includes = ''
where = ''
sorted_ids = nil
case table
when 'notifications'
dossiers_id_with_notification = current_gestionnaire.notifications_for_procedure(procedure)
if order == 'desc'
sorted_ids = dossiers_id_with_notification + (dossiers.order('dossiers.updated_at desc').ids - dossiers_id_with_notification)
else
sorted_ids = (dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) + dossiers_id_with_notification
end
when 'self'
order = "dossiers.#{column} #{order}"
when 'france_connect_information'
includes = { user: :france_connect_information }
order = "france_connect_informations.#{column} #{order}"
when 'type_de_champ', 'type_de_champ_private'
includes = table == 'type_de_champ' ? :champs : :champs_private
where = "champs.type_de_champ_id = #{column.to_i}"
order = "champs.value #{order}"
else
includes = table
order = "#{table.pluralize}.#{column} #{order}"
end
if sorted_ids.nil?
sorted_ids = dossiers.includes(includes).where(where).order(Dossier.sanitize_for_order(order)).pluck(:id)
end
sorted_ids
end
def current_filters
@current_filters ||= procedure_presentation.filters[statut]
end

View file

@ -217,22 +217,7 @@ class Dossier < ApplicationRecord
end
def get_value(table, column)
case table
when 'self'
self.send(column)
when 'user'
self.user.send(column)
when 'france_connect_information'
self.user.france_connect_information&.send(column)
when 'entreprise'
self.etablissement&.send(:"entreprise_#{column}")
when 'etablissement'
self.etablissement&.send(column)
when 'type_de_champ'
self.champs.find { |c| c.type_de_champ_id == column.to_i }.value
when 'type_de_champ_private'
self.champs_private.find { |c| c.type_de_champ_id == column.to_i }.value
end
DossierFieldService.get_value(self, table, column)
end
def self.sanitize_for_order(order)

View file

@ -292,50 +292,7 @@ class Procedure < ApplicationRecord
end
def fields
fields = [
field_hash('Créé le', 'self', 'created_at'),
field_hash('Mis à jour le', 'self', 'updated_at'),
field_hash('Demandeur', 'user', 'email')
]
fields << [
field_hash('Civilité (FC)', 'france_connect_information', 'gender'),
field_hash('Prénom (FC)', 'france_connect_information', 'given_name'),
field_hash('Nom (FC)', 'france_connect_information', 'family_name')
]
if !for_individual || (for_individual && individual_with_siret)
fields << [
field_hash('SIREN', 'etablissement', 'entreprise_siren'),
field_hash('Forme juridique', 'etablissement', 'entreprise_forme_juridique'),
field_hash('Nom commercial', 'etablissement', 'entreprise_nom_commercial'),
field_hash('Raison sociale', 'etablissement', 'entreprise_raison_sociale'),
field_hash('SIRET siège social', 'etablissement', 'entreprise_siret_siege_social'),
field_hash('Date de création', 'etablissement', 'entreprise_date_creation')
]
fields << [
field_hash('SIRET', 'etablissement', 'siret'),
field_hash('Libellé NAF', 'etablissement', 'libelle_naf'),
field_hash('Code postal', 'etablissement', 'code_postal')
]
end
types_de_champ
.reject { |tdc| [TypeDeChamp.type_champs.fetch(:header_section), TypeDeChamp.type_champs.fetch(:explication)].include?(tdc.type_champ) }
.each do |type_de_champ|
fields << field_hash(type_de_champ.libelle, 'type_de_champ', type_de_champ.id.to_s)
end
types_de_champ_private
.reject { |tdc| [TypeDeChamp.type_champs.fetch(:header_section), TypeDeChamp.type_champs.fetch(:explication)].include?(tdc.type_champ) }
.each do |type_de_champ|
fields << field_hash(type_de_champ.libelle, 'type_de_champ_private', type_de_champ.id.to_s)
end
fields.flatten
DossierFieldService.fields(self)
end
def fields_for_select
@ -426,14 +383,6 @@ class Procedure < ApplicationRecord
end
end
def field_hash(label, table, column)
{
'label' => label,
'table' => table,
'column' => column
}
end
def update_durees_conservation_required
self.durees_conservation_required ||= duree_conservation_dossiers_hors_ds.present? && duree_conservation_dossiers_dans_ds.present?
true

View file

@ -0,0 +1,149 @@
class DossierFieldService
class << self
def fields(procedure)
fields = [
field_hash('Créé le', 'self', 'created_at'),
field_hash('Mis à jour le', 'self', 'updated_at'),
field_hash('Demandeur', 'user', 'email')
]
fields << [
field_hash('Civilité (FC)', 'france_connect_information', 'gender'),
field_hash('Prénom (FC)', 'france_connect_information', 'given_name'),
field_hash('Nom (FC)', 'france_connect_information', 'family_name')
]
if !procedure.for_individual || (procedure.for_individual && procedure.individual_with_siret)
fields << [
field_hash('SIREN', 'etablissement', 'entreprise_siren'),
field_hash('Forme juridique', 'etablissement', 'entreprise_forme_juridique'),
field_hash('Nom commercial', 'etablissement', 'entreprise_nom_commercial'),
field_hash('Raison sociale', 'etablissement', 'entreprise_raison_sociale'),
field_hash('SIRET siège social', 'etablissement', 'entreprise_siret_siege_social'),
field_hash('Date de création', 'etablissement', 'entreprise_date_creation')
]
fields << [
field_hash('SIRET', 'etablissement', 'siret'),
field_hash('Libellé NAF', 'etablissement', 'libelle_naf'),
field_hash('Code postal', 'etablissement', 'code_postal')
]
end
procedure.types_de_champ
.reject { |tdc| [TypeDeChamp.type_champs.fetch(:header_section), TypeDeChamp.type_champs.fetch(:explication)].include?(tdc.type_champ) }
.each { |type_de_champ| fields << field_hash(type_de_champ.libelle, 'type_de_champ', type_de_champ.id.to_s) }
procedure.types_de_champ_private
.reject { |tdc| [TypeDeChamp.type_champs.fetch(:header_section), TypeDeChamp.type_champs.fetch(:explication)].include?(tdc.type_champ) }
.each { |type_de_champ| fields << field_hash(type_de_champ.libelle, 'type_de_champ_private', type_de_champ.id.to_s) }
fields.flatten
end
def get_value(dossier, table, column)
case table
when 'self'
dossier.send(column)
when 'user'
dossier.user.send(column)
when 'france_connect_information'
dossier.user.france_connect_information&.send(column)
when 'entreprise'
dossier.etablissement&.send(:"entreprise_#{column}")
when 'etablissement'
dossier.etablissement&.send(column)
when 'type_de_champ'
dossier.champs.find { |c| c.type_de_champ_id == column.to_i }.value
when 'type_de_champ_private'
dossier.champs_private.find { |c| c.type_de_champ_id == column.to_i }.value
end
end
def filtered_ids(dossiers, filters)
filters.map do |filter|
case filter['table']
when 'self'
dossiers.where("? ILIKE ?", filter['column'], "%#{filter['value']}%")
when 'france_connect_information'
dossiers
.includes(user: :france_connect_information)
.where("? ILIKE ?", "france_connect_informations.#{filter['column']}", "%#{filter['value']}%")
when 'type_de_champ', 'type_de_champ_private'
relation = filter['table'] == 'type_de_champ' ? :champs : :champs_private
dossiers
.includes(relation)
.where("champs.type_de_champ_id = ?", filter['column'].to_i)
.where("champs.value ILIKE ?", "%#{filter['value']}%")
when 'entreprise'
table = 'etablissement'
if filter['column'] == 'date_creation'
date = filter['value'].to_date rescue nil
dossiers
.includes(table)
.where("#{table.pluralize}.entreprise_#{filter['column']} = ?", date)
else
dossiers
.includes(table)
.where("#{table.pluralize}.entreprise_#{filter['column']} ILIKE ?", "%#{filter['value']}%")
end
when 'user', 'etablissement'
dossiers
.includes(filter['table'])
.where("#{filter['table'].pluralize}.#{filter['column']} ILIKE ?", "%#{filter['value']}%")
end.pluck(:id)
end.reduce(:&)
end
def sorted_ids(dossiers, procedure_presentation, gestionnaire)
table = procedure_presentation.sort['table']
column = procedure_presentation.sort['column']
order = procedure_presentation.sort['order']
includes = ''
where = ''
sorted_ids = nil
case table
when 'notifications'
procedure = procedure_presentation.assign_to.procedure
dossiers_id_with_notification = gestionnaire.notifications_for_procedure(procedure)
if order == 'desc'
sorted_ids = dossiers_id_with_notification + (dossiers.order('dossiers.updated_at desc').ids - dossiers_id_with_notification)
else
sorted_ids = (dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) + dossiers_id_with_notification
end
when 'self'
order = "dossiers.#{column} #{order}"
when 'france_connect_information'
includes = { user: :france_connect_information }
order = "france_connect_informations.#{column} #{order}"
when 'type_de_champ', 'type_de_champ_private'
includes = table == 'type_de_champ' ? :champs : :champs_private
where = "champs.type_de_champ_id = #{column.to_i}"
order = "champs.value #{order}"
else
includes = table
order = "#{table.pluralize}.#{column} #{order}"
end
if sorted_ids.nil?
sorted_ids = dossiers.includes(includes).where(where).order(Dossier.sanitize_for_order(order)).pluck(:id)
end
sorted_ids
end
private
def field_hash(label, table, column)
{
'label' => label,
'table' => table,
'column' => column
}
end
end
end