2020-08-06 16:35:45 +02:00
|
|
|
|
# == Schema Information
|
|
|
|
|
#
|
|
|
|
|
# Table name: procedure_presentations
|
|
|
|
|
#
|
|
|
|
|
# id :integer not null, primary key
|
|
|
|
|
# displayed_fields :jsonb not null
|
|
|
|
|
# filters :jsonb not null
|
|
|
|
|
# sort :jsonb not null
|
|
|
|
|
# created_at :datetime
|
|
|
|
|
# updated_at :datetime
|
|
|
|
|
# assign_to_id :integer
|
|
|
|
|
#
|
2018-03-06 13:44:29 +01:00
|
|
|
|
class ProcedurePresentation < ApplicationRecord
|
2018-10-03 16:20:27 +02:00
|
|
|
|
EXTRA_SORT_COLUMNS = {
|
2018-10-05 15:50:38 +02:00
|
|
|
|
'notifications' => ['notifications'],
|
|
|
|
|
'self' => ['id', 'state']
|
2018-10-03 16:20:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 12:52:00 +02:00
|
|
|
|
TABLE = 'table'
|
|
|
|
|
COLUMN = 'column'
|
2022-09-26 17:14:20 +02:00
|
|
|
|
ORDER = 'order'
|
|
|
|
|
|
2021-04-16 12:52:00 +02:00
|
|
|
|
SLASH = '/'
|
|
|
|
|
TYPE_DE_CHAMP = 'type_de_champ'
|
|
|
|
|
TYPE_DE_CHAMP_PRIVATE = 'type_de_champ_private'
|
|
|
|
|
|
2021-07-20 14:00:29 +02:00
|
|
|
|
FILTERS_VALUE_MAX_LENGTH = 100
|
|
|
|
|
|
2020-07-20 16:59:27 +02:00
|
|
|
|
belongs_to :assign_to, optional: false
|
2022-04-05 15:53:15 +02:00
|
|
|
|
has_many :exports, dependent: :destroy
|
|
|
|
|
|
2022-03-31 16:32:45 +02:00
|
|
|
|
delegate :procedure, :instructeur, to: :assign_to
|
2018-10-02 17:04:23 +02:00
|
|
|
|
|
|
|
|
|
validate :check_allowed_displayed_fields
|
|
|
|
|
validate :check_allowed_sort_column
|
2018-10-05 15:31:24 +02:00
|
|
|
|
validate :check_allowed_sort_order
|
2018-10-02 17:04:23 +02:00
|
|
|
|
validate :check_allowed_filter_columns
|
2021-07-20 14:00:29 +02:00
|
|
|
|
validate :check_filters_max_length
|
2018-10-02 17:04:23 +02:00
|
|
|
|
|
2022-09-12 16:30:29 +02:00
|
|
|
|
def self_fields
|
|
|
|
|
[
|
|
|
|
|
field_hash('self', 'created_at', type: :date),
|
|
|
|
|
field_hash('self', 'updated_at', type: :date),
|
|
|
|
|
field_hash('self', 'depose_at', type: :date),
|
|
|
|
|
field_hash('self', 'en_construction_at', type: :date),
|
|
|
|
|
field_hash('self', 'en_instruction_at', type: :date),
|
|
|
|
|
field_hash('self', 'processed_at', type: :date),
|
|
|
|
|
field_hash('self', 'updated_since', type: :date, virtual: true),
|
|
|
|
|
field_hash('self', 'depose_since', type: :date, virtual: true),
|
|
|
|
|
field_hash('self', 'en_construction_since', type: :date, virtual: true),
|
|
|
|
|
field_hash('self', 'en_instruction_since', type: :date, virtual: true),
|
|
|
|
|
field_hash('self', 'processed_since', type: :date, virtual: true),
|
2022-09-28 15:21:28 +02:00
|
|
|
|
field_hash('self', 'state', type: :enum, scope: 'instructeurs.dossiers.filterable_state', virtual: true)
|
2018-10-05 14:45:58 +02:00
|
|
|
|
]
|
2022-09-12 16:30:29 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def fields
|
|
|
|
|
fields = self_fields
|
|
|
|
|
|
|
|
|
|
fields.push(
|
|
|
|
|
field_hash('user', 'email', type: :text),
|
|
|
|
|
field_hash('followers_instructeurs', 'email', type: :text),
|
2023-04-20 11:44:35 +02:00
|
|
|
|
field_hash('groupe_instructeur', 'id', type: :enum),
|
2023-05-22 18:10:15 +02:00
|
|
|
|
field_hash('avis', 'answer', type: :text)
|
2022-09-12 16:30:29 +02:00
|
|
|
|
)
|
2018-10-05 14:45:58 +02:00
|
|
|
|
|
2018-10-11 10:45:03 +02:00
|
|
|
|
if procedure.for_individual
|
|
|
|
|
fields.push(
|
2022-09-12 16:30:29 +02:00
|
|
|
|
field_hash("individual", "prenom", type: :text),
|
|
|
|
|
field_hash("individual", "nom", type: :text),
|
|
|
|
|
field_hash("individual", "gender", type: :text)
|
2018-10-11 10:45:03 +02:00
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
2019-07-10 18:27:26 +02:00
|
|
|
|
if !procedure.for_individual
|
2018-10-05 14:45:58 +02:00
|
|
|
|
fields.push(
|
2022-09-12 16:30:29 +02:00
|
|
|
|
field_hash('etablissement', 'entreprise_siren', type: :text),
|
|
|
|
|
field_hash('etablissement', 'entreprise_forme_juridique', type: :text),
|
|
|
|
|
field_hash('etablissement', 'entreprise_nom_commercial', type: :text),
|
|
|
|
|
field_hash('etablissement', 'entreprise_raison_sociale', type: :text),
|
|
|
|
|
field_hash('etablissement', 'entreprise_siret_siege_social', type: :text),
|
|
|
|
|
field_hash('etablissement', 'entreprise_date_creation', type: :date)
|
2018-10-05 14:45:58 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
fields.push(
|
2022-09-12 16:30:29 +02:00
|
|
|
|
field_hash('etablissement', 'siret', type: :text),
|
|
|
|
|
field_hash('etablissement', 'libelle_naf', type: :text),
|
|
|
|
|
field_hash('etablissement', 'code_postal', type: :text)
|
2018-10-05 14:45:58 +02:00
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
2021-06-16 10:48:15 +02:00
|
|
|
|
fields.concat procedure.types_de_champ_for_procedure_presentation
|
2023-02-17 14:39:59 +01:00
|
|
|
|
.pluck(:type_champ, :libelle, :private, :stable_id)
|
2023-05-09 11:38:06 +02:00
|
|
|
|
.reject { |(type_champ)| type_champ == TypeDeChamp.type_champs.fetch(:repetition) }
|
2023-02-20 06:40:57 +01:00
|
|
|
|
.map { |(type_champ, libelle, is_private, stable_id)| field_hash(is_private ? TYPE_DE_CHAMP_PRIVATE : TYPE_DE_CHAMP, stable_id.to_s, label: libelle, type: (TypeDeChamp.options_for_select?(type_champ) ? :enum : :text)) }
|
2018-10-05 14:45:58 +02:00
|
|
|
|
|
|
|
|
|
fields
|
2018-10-03 16:05:27 +02:00
|
|
|
|
end
|
|
|
|
|
|
2022-06-14 07:46:12 +02:00
|
|
|
|
def displayable_fields_for_select
|
2020-10-30 11:38:58 +01:00
|
|
|
|
[
|
2022-06-14 07:46:12 +02:00
|
|
|
|
fields.reject { |field| field['virtual'] }
|
|
|
|
|
.map { |field| [field['label'], field_id(field)] },
|
2020-10-30 11:38:58 +01:00
|
|
|
|
displayed_fields.map { |field| field_id(field) }
|
|
|
|
|
]
|
2018-10-03 14:46:12 +02:00
|
|
|
|
end
|
|
|
|
|
|
2022-06-14 07:46:12 +02:00
|
|
|
|
def filterable_fields_options
|
2022-09-12 16:30:29 +02:00
|
|
|
|
fields.map do |field|
|
|
|
|
|
[field['label'], field_id(field)]
|
|
|
|
|
end
|
2022-06-14 07:46:12 +02:00
|
|
|
|
end
|
|
|
|
|
|
2022-04-14 10:30:23 +02:00
|
|
|
|
def displayed_fields_for_headers
|
|
|
|
|
[
|
|
|
|
|
field_hash('self', 'id', classname: 'number-col'),
|
2022-04-14 17:04:42 +02:00
|
|
|
|
*displayed_fields,
|
2022-04-14 10:30:23 +02:00
|
|
|
|
field_hash('self', 'state', classname: 'state-col')
|
|
|
|
|
]
|
|
|
|
|
end
|
|
|
|
|
|
2022-03-31 16:32:45 +02:00
|
|
|
|
def sorted_ids(dossiers, count)
|
2021-04-16 12:52:00 +02:00
|
|
|
|
table, column, order = sort.values_at(TABLE, COLUMN, 'order')
|
2018-10-05 10:08:39 +02:00
|
|
|
|
|
|
|
|
|
case table
|
|
|
|
|
when 'notifications'
|
2020-09-18 15:40:26 +02:00
|
|
|
|
dossiers_id_with_notification = dossiers.merge(instructeur.followed_dossiers).with_notifications.ids
|
2018-10-05 10:08:39 +02:00
|
|
|
|
if order == 'desc'
|
2020-10-30 14:51:19 +01:00
|
|
|
|
dossiers_id_with_notification +
|
2018-10-05 10:08:39 +02:00
|
|
|
|
(dossiers.order('dossiers.updated_at desc').ids - dossiers_id_with_notification)
|
|
|
|
|
else
|
2020-10-30 14:51:19 +01:00
|
|
|
|
(dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) +
|
2018-10-05 10:08:39 +02:00
|
|
|
|
dossiers_id_with_notification
|
|
|
|
|
end
|
2021-04-16 12:52:00 +02:00
|
|
|
|
when TYPE_DE_CHAMP
|
2021-07-22 18:16:04 +02:00
|
|
|
|
ids = dossiers
|
2020-10-30 15:01:13 +01:00
|
|
|
|
.with_type_de_champ(column)
|
|
|
|
|
.order("champs.value #{order}")
|
|
|
|
|
.pluck(:id)
|
2021-07-22 18:16:04 +02:00
|
|
|
|
if ids.size != count
|
|
|
|
|
rest = dossiers.where.not(id: ids).order(id: order).pluck(:id)
|
|
|
|
|
order == 'asc' ? ids + rest : rest + ids
|
|
|
|
|
else
|
|
|
|
|
ids
|
|
|
|
|
end
|
2021-04-16 12:52:00 +02:00
|
|
|
|
when TYPE_DE_CHAMP_PRIVATE
|
2021-07-22 18:16:04 +02:00
|
|
|
|
ids = dossiers
|
2020-10-30 15:01:13 +01:00
|
|
|
|
.with_type_de_champ_private(column)
|
|
|
|
|
.order("champs.value #{order}")
|
|
|
|
|
.pluck(:id)
|
2021-07-22 18:16:04 +02:00
|
|
|
|
if ids.size != count
|
|
|
|
|
rest = dossiers.where.not(id: ids).order(id: order).pluck(:id)
|
|
|
|
|
order == 'asc' ? ids + rest : rest + ids
|
|
|
|
|
else
|
|
|
|
|
ids
|
|
|
|
|
end
|
2019-10-16 13:02:42 +02:00
|
|
|
|
when 'followers_instructeurs'
|
|
|
|
|
assert_supported_column(table, column)
|
2022-03-16 18:23:21 +01:00
|
|
|
|
# LEFT OUTER JOIN allows to keep dossiers without assigned instructeurs yet
|
2020-10-30 14:51:19 +01:00
|
|
|
|
dossiers
|
|
|
|
|
.includes(:followers_instructeurs)
|
2022-03-15 17:28:22 +01:00
|
|
|
|
.joins('LEFT OUTER JOIN users instructeurs_users ON instructeurs_users.id = instructeurs.user_id')
|
2020-10-30 14:51:19 +01:00
|
|
|
|
.order("instructeurs_users.email #{order}")
|
|
|
|
|
.pluck(:id)
|
2021-05-04 15:37:29 +02:00
|
|
|
|
.uniq
|
2023-06-02 17:05:19 +02:00
|
|
|
|
when 'avis'
|
|
|
|
|
dossiers.includes(table)
|
|
|
|
|
.order("#{self.class.sanitized_column(table, column)} #{order}")
|
|
|
|
|
.pluck(:id)
|
|
|
|
|
.uniq
|
2019-10-16 13:02:42 +02:00
|
|
|
|
when 'self', 'user', 'individual', 'etablissement', 'groupe_instructeur'
|
2020-10-30 14:51:19 +01:00
|
|
|
|
(table == 'self' ? dossiers : dossiers.includes(table))
|
|
|
|
|
.order("#{self.class.sanitized_column(table, column)} #{order}")
|
|
|
|
|
.pluck(:id)
|
2018-10-05 10:08:39 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-03 19:17:22 +02:00
|
|
|
|
def filtered_ids(dossiers, statut)
|
2022-09-12 16:30:29 +02:00
|
|
|
|
filters.fetch(statut)
|
|
|
|
|
.group_by { |filter| filter.values_at(TABLE, COLUMN) }
|
|
|
|
|
.map do |(table, column), filters|
|
2019-02-19 17:06:06 +01:00
|
|
|
|
values = filters.pluck('value')
|
2018-10-03 19:17:22 +02:00
|
|
|
|
case table
|
|
|
|
|
when 'self'
|
2022-09-12 16:30:29 +02:00
|
|
|
|
field = self_fields.find { |h| h['column'] == column }
|
|
|
|
|
if field['type'] == :date
|
|
|
|
|
dates = values
|
|
|
|
|
.filter_map { |v| Time.zone.parse(v).beginning_of_day rescue nil }
|
2021-06-10 15:24:15 +02:00
|
|
|
|
|
2022-09-12 16:30:29 +02:00
|
|
|
|
dossiers.filter_by_datetimes(column, dates)
|
2023-03-27 19:11:22 +02:00
|
|
|
|
elsif field['column'] == "state" && values.include?("pending_correction")
|
2023-04-03 17:05:54 +02:00
|
|
|
|
dossiers.joins(:corrections).where(corrections: DossierCorrection.pending)
|
2022-09-12 16:30:29 +02:00
|
|
|
|
else
|
2022-10-03 15:18:46 +02:00
|
|
|
|
dossiers.where("dossiers.#{column} IN (?)", values)
|
2022-09-12 16:30:29 +02:00
|
|
|
|
end
|
2021-04-16 12:52:00 +02:00
|
|
|
|
when TYPE_DE_CHAMP
|
2020-10-30 15:01:13 +01:00
|
|
|
|
dossiers.with_type_de_champ(column)
|
|
|
|
|
.filter_ilike(:champs, :value, values)
|
2021-04-16 12:52:00 +02:00
|
|
|
|
when TYPE_DE_CHAMP_PRIVATE
|
2020-10-30 15:01:13 +01:00
|
|
|
|
dossiers.with_type_de_champ_private(column)
|
|
|
|
|
.filter_ilike(:champs_private, :value, values)
|
2018-10-03 19:17:22 +02:00
|
|
|
|
when 'etablissement'
|
2019-02-26 16:13:22 +01:00
|
|
|
|
if column == 'entreprise_date_creation'
|
2019-02-27 12:35:08 +01:00
|
|
|
|
dates = values
|
2021-06-10 15:24:15 +02:00
|
|
|
|
.filter_map { |v| v.to_date rescue nil }
|
|
|
|
|
|
2019-02-26 17:11:47 +01:00
|
|
|
|
dossiers
|
|
|
|
|
.includes(table)
|
|
|
|
|
.where(table.pluralize => { column => dates })
|
2018-10-03 19:17:22 +02:00
|
|
|
|
else
|
2019-02-14 15:03:56 +01:00
|
|
|
|
dossiers
|
|
|
|
|
.includes(table)
|
2019-02-27 12:12:34 +01:00
|
|
|
|
.filter_ilike(table, column, values)
|
|
|
|
|
end
|
2019-10-16 13:02:42 +02:00
|
|
|
|
when 'followers_instructeurs'
|
|
|
|
|
assert_supported_column(table, column)
|
|
|
|
|
dossiers
|
|
|
|
|
.includes(:followers_instructeurs)
|
2022-03-15 17:28:22 +01:00
|
|
|
|
.joins('INNER JOIN users instructeurs_users ON instructeurs_users.id = instructeurs.user_id')
|
2019-10-16 13:02:42 +02:00
|
|
|
|
.filter_ilike('instructeurs_users', :email, values)
|
2023-05-22 18:10:15 +02:00
|
|
|
|
when 'user', 'individual', 'avis'
|
2019-02-27 12:12:34 +01:00
|
|
|
|
dossiers
|
|
|
|
|
.includes(table)
|
|
|
|
|
.filter_ilike(table, column, values)
|
2019-09-18 15:25:05 +02:00
|
|
|
|
when 'groupe_instructeur'
|
2023-01-13 16:40:48 +01:00
|
|
|
|
assert_supported_column(table, column)
|
|
|
|
|
if column == 'label'
|
|
|
|
|
dossiers
|
|
|
|
|
.joins(:groupe_instructeur)
|
|
|
|
|
.filter_ilike(table, column, values)
|
|
|
|
|
else
|
|
|
|
|
dossiers
|
|
|
|
|
.joins(:groupe_instructeur)
|
|
|
|
|
.where(groupe_instructeur_id: values)
|
|
|
|
|
end
|
2018-10-03 19:17:22 +02:00
|
|
|
|
end.pluck(:id)
|
|
|
|
|
end.reduce(:&)
|
|
|
|
|
end
|
|
|
|
|
|
2022-04-06 09:12:15 +02:00
|
|
|
|
def filtered_sorted_ids(dossiers, statut, count: nil)
|
2023-05-09 13:39:46 +02:00
|
|
|
|
dossiers_by_statut = dossiers.by_statut(statut, instructeur)
|
2022-04-06 09:12:15 +02:00
|
|
|
|
dossiers_sorted_ids = self.sorted_ids(dossiers_by_statut, count || dossiers_by_statut.size)
|
2022-03-31 18:28:12 +02:00
|
|
|
|
|
|
|
|
|
if filters[statut].present?
|
2022-04-15 12:13:32 +02:00
|
|
|
|
dossiers_sorted_ids.intersection(filtered_ids(dossiers_by_statut, statut))
|
2022-03-31 18:28:12 +02:00
|
|
|
|
else
|
|
|
|
|
dossiers_sorted_ids
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-06-08 14:33:58 +02:00
|
|
|
|
def human_value_for_filter(filter)
|
2022-12-14 23:13:54 +01:00
|
|
|
|
if [TYPE_DE_CHAMP, TYPE_DE_CHAMP_PRIVATE].include?(filter[TABLE])
|
2021-04-16 12:52:00 +02:00
|
|
|
|
find_type_de_champ(filter[COLUMN]).dynamic_type.filter_to_human(filter['value'])
|
2022-12-14 23:13:54 +01:00
|
|
|
|
elsif filter['column'] == 'state'
|
2023-03-27 19:11:22 +02:00
|
|
|
|
if filter['value'] == 'pending_correction'
|
|
|
|
|
Dossier.human_attribute_name("pending_correction.for_instructeur")
|
|
|
|
|
else
|
|
|
|
|
Dossier.human_attribute_name("state.#{filter['value']}")
|
|
|
|
|
end
|
2023-01-13 16:40:48 +01:00
|
|
|
|
elsif filter['table'] == 'groupe_instructeur' && filter['column'] == 'id'
|
|
|
|
|
instructeur.groupe_instructeurs
|
2023-04-25 14:15:40 +02:00
|
|
|
|
.find { _1.id == filter['value'].to_i }&.label || filter['value']
|
2020-06-23 11:33:46 +02:00
|
|
|
|
else
|
|
|
|
|
filter['value']
|
2020-06-08 14:33:58 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def add_filter(statut, field, value)
|
|
|
|
|
if value.present?
|
2021-04-16 12:52:00 +02:00
|
|
|
|
table, column = field.split(SLASH)
|
2020-06-08 14:33:58 +02:00
|
|
|
|
label = find_field(table, column)['label']
|
|
|
|
|
|
2020-06-23 11:33:46 +02:00
|
|
|
|
case table
|
2021-04-16 12:52:00 +02:00
|
|
|
|
when TYPE_DE_CHAMP, TYPE_DE_CHAMP_PRIVATE
|
2020-10-30 14:32:53 +01:00
|
|
|
|
value = find_type_de_champ(column).dynamic_type.human_to_filter(value)
|
2020-06-08 14:33:58 +02:00
|
|
|
|
end
|
|
|
|
|
|
2020-10-30 14:32:53 +01:00
|
|
|
|
updated_filters = filters.dup
|
2020-06-08 14:33:58 +02:00
|
|
|
|
updated_filters[statut] << {
|
|
|
|
|
'label' => label,
|
2021-04-16 12:52:00 +02:00
|
|
|
|
TABLE => table,
|
|
|
|
|
COLUMN => column,
|
2020-06-08 14:33:58 +02:00
|
|
|
|
'value' => value
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 14:32:53 +01:00
|
|
|
|
update!(filters: updated_filters)
|
2020-06-08 14:33:58 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-10-30 14:34:00 +01:00
|
|
|
|
def remove_filter(statut, field, value)
|
2021-04-16 12:52:00 +02:00
|
|
|
|
table, column = field.split(SLASH)
|
2020-10-30 14:34:00 +01:00
|
|
|
|
|
|
|
|
|
updated_filters = filters.dup
|
|
|
|
|
updated_filters[statut] = filters[statut].reject do |filter|
|
2021-04-16 12:52:00 +02:00
|
|
|
|
filter.values_at(TABLE, COLUMN, 'value') == [table, column, value]
|
2020-10-30 14:34:00 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
update!(filters: updated_filters)
|
|
|
|
|
end
|
2020-10-30 13:33:42 +01:00
|
|
|
|
|
|
|
|
|
def update_displayed_fields(values)
|
|
|
|
|
if values.nil?
|
|
|
|
|
values = []
|
|
|
|
|
end
|
|
|
|
|
|
2021-04-16 12:52:00 +02:00
|
|
|
|
fields = values.map { |value| find_field(*value.split(SLASH)) }
|
2020-10-30 13:33:42 +01:00
|
|
|
|
|
|
|
|
|
update!(displayed_fields: fields)
|
|
|
|
|
|
|
|
|
|
if !values.include?(field_id(sort))
|
|
|
|
|
update!(sort: Procedure.default_sort)
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-10-30 14:31:24 +01:00
|
|
|
|
|
2022-09-26 17:14:20 +02:00
|
|
|
|
def update_sort(table, column, order)
|
2020-10-30 14:31:24 +01:00
|
|
|
|
update!(sort: {
|
2021-04-16 12:52:00 +02:00
|
|
|
|
TABLE => table,
|
|
|
|
|
COLUMN => column,
|
2022-12-20 11:46:17 +01:00
|
|
|
|
ORDER => order.presence || opposite_order_for(table, column)
|
2020-10-30 14:31:24 +01:00
|
|
|
|
})
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-26 17:14:20 +02:00
|
|
|
|
def opposite_order_for(table, column)
|
|
|
|
|
if sort.values_at(TABLE, COLUMN) == [table, column]
|
|
|
|
|
sort['order'] == 'asc' ? 'desc' : 'asc'
|
2022-12-20 11:46:17 +01:00
|
|
|
|
elsif [table, column] == ["notifications", "notifications"]
|
|
|
|
|
'desc' # default order for notifications
|
2022-09-26 17:14:20 +02:00
|
|
|
|
else
|
|
|
|
|
'asc'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-04-05 18:11:12 +02:00
|
|
|
|
def snapshot
|
|
|
|
|
slice(:filters, :sort, :displayed_fields)
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-12 16:30:29 +02:00
|
|
|
|
def field_type(field_id)
|
|
|
|
|
find_field(*field_id.split(SLASH))['type']
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def field_enum(field_id)
|
2023-01-13 16:40:48 +01:00
|
|
|
|
field = find_field(*field_id.split(SLASH))
|
|
|
|
|
if field['scope'].present?
|
|
|
|
|
I18n.t(field['scope']).map(&:to_a).map(&:reverse)
|
|
|
|
|
elsif field['table'] == 'groupe_instructeur'
|
2023-01-31 12:38:47 +01:00
|
|
|
|
instructeur.groupe_instructeurs.filter_map do
|
|
|
|
|
if _1.procedure_id == procedure.id
|
|
|
|
|
[_1.label, _1.id]
|
|
|
|
|
end
|
|
|
|
|
end
|
2023-02-17 14:39:59 +01:00
|
|
|
|
else
|
2023-02-24 13:32:46 +01:00
|
|
|
|
find_type_de_champ(field['column']).options_for_select
|
2023-01-13 16:40:48 +01:00
|
|
|
|
end
|
2022-09-12 16:30:29 +02:00
|
|
|
|
end
|
|
|
|
|
|
2023-04-05 10:05:51 +02:00
|
|
|
|
def sortable?(field)
|
|
|
|
|
sort['table'] == field['table'] && sort['column'] == field['column']
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def aria_sort(order, field)
|
|
|
|
|
if sortable?(field)
|
|
|
|
|
if order == 'asc'
|
|
|
|
|
{ "aria-sort": "ascending" }
|
|
|
|
|
elsif order == 'desc'
|
|
|
|
|
{ "aria-sort": "descending" }
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
{}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-02 17:04:23 +02:00
|
|
|
|
private
|
|
|
|
|
|
2020-10-30 11:38:58 +01:00
|
|
|
|
def field_id(field)
|
2021-04-16 12:52:00 +02:00
|
|
|
|
field.values_at(TABLE, COLUMN).join(SLASH)
|
2020-10-30 11:38:58 +01:00
|
|
|
|
end
|
|
|
|
|
|
2020-06-08 14:33:58 +02:00
|
|
|
|
def find_field(table, column)
|
2021-04-16 12:52:00 +02:00
|
|
|
|
fields.find { |field| field.values_at(TABLE, COLUMN) == [table, column] }
|
2020-06-08 14:33:58 +02:00
|
|
|
|
end
|
|
|
|
|
|
2020-10-30 15:07:24 +01:00
|
|
|
|
def find_type_de_champ(column)
|
2022-07-28 11:28:17 +02:00
|
|
|
|
TypeDeChamp
|
|
|
|
|
.joins(:revision_types_de_champ)
|
|
|
|
|
.where(revision_types_de_champ: { revision_id: procedure.revisions })
|
|
|
|
|
.order(:created_at)
|
|
|
|
|
.find_by(stable_id: column)
|
2020-10-30 15:07:24 +01:00
|
|
|
|
end
|
|
|
|
|
|
2018-10-10 06:04:29 +02:00
|
|
|
|
def check_allowed_displayed_fields
|
|
|
|
|
displayed_fields.each do |field|
|
2019-02-27 16:48:56 +01:00
|
|
|
|
check_allowed_field(:displayed_fields, field)
|
2018-10-10 06:04:29 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check_allowed_sort_column
|
2019-02-27 16:48:56 +01:00
|
|
|
|
check_allowed_field(:sort, sort, EXTRA_SORT_COLUMNS)
|
2018-10-10 06:04:29 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-10-05 15:31:24 +02:00
|
|
|
|
def check_allowed_sort_order
|
|
|
|
|
order = sort['order']
|
|
|
|
|
if !["asc", "desc"].include?(order)
|
|
|
|
|
errors.add(:sort, "#{order} n’est pas une ordre permis")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-10 06:04:29 +02:00
|
|
|
|
def check_allowed_filter_columns
|
2020-10-01 18:46:39 +02:00
|
|
|
|
filters.each do |key, columns|
|
|
|
|
|
return true if key == 'migrated'
|
2018-10-10 06:04:29 +02:00
|
|
|
|
columns.each do |column|
|
2019-02-27 16:48:56 +01:00
|
|
|
|
check_allowed_field(:filters, column)
|
2018-10-10 06:04:29 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-02-27 16:48:56 +01:00
|
|
|
|
def check_allowed_field(kind, field, extra_columns = {})
|
2021-04-16 12:52:00 +02:00
|
|
|
|
table, column = field.values_at(TABLE, COLUMN)
|
2019-02-27 16:48:56 +01:00
|
|
|
|
if !valid_column?(table, column, extra_columns)
|
|
|
|
|
errors.add(kind, "#{table}.#{column} n’est pas une colonne permise")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-07-20 14:00:29 +02:00
|
|
|
|
def check_filters_max_length
|
|
|
|
|
individual_filters = filters.values.flatten.filter { |f| f.is_a?(Hash) }
|
|
|
|
|
individual_filters.each do |filter|
|
|
|
|
|
if filter['value']&.length.to_i > FILTERS_VALUE_MAX_LENGTH
|
|
|
|
|
errors.add(:filters, :too_long)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-09-12 16:30:29 +02:00
|
|
|
|
def field_hash(table, column, label: nil, classname: '', virtual: false, type: :text, scope: '')
|
2018-10-05 14:45:58 +02:00
|
|
|
|
{
|
2022-04-14 10:30:23 +02:00
|
|
|
|
'label' => label || I18n.t(column, scope: [:activerecord, :attributes, :procedure_presentation, :fields, table]),
|
2021-04-16 12:52:00 +02:00
|
|
|
|
TABLE => table,
|
2022-04-14 10:30:23 +02:00
|
|
|
|
COLUMN => column,
|
2022-06-14 07:46:12 +02:00
|
|
|
|
'classname' => classname,
|
2022-09-12 16:30:29 +02:00
|
|
|
|
'virtual' => virtual,
|
|
|
|
|
'type' => type,
|
|
|
|
|
'scope' => scope
|
2018-10-05 14:45:58 +02:00
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2019-03-04 17:21:48 +01:00
|
|
|
|
def valid_column?(table, column, extra_columns = {})
|
|
|
|
|
valid_columns_for_table(table).include?(column) ||
|
|
|
|
|
extra_columns[table]&.include?(column)
|
2018-10-05 14:45:58 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-10-05 14:57:49 +02:00
|
|
|
|
def valid_columns_for_table(table)
|
|
|
|
|
@column_whitelist ||= fields
|
2021-04-16 12:52:00 +02:00
|
|
|
|
.group_by { |field| field[TABLE] }
|
|
|
|
|
.transform_values { |fields| Set.new(fields.pluck(COLUMN)) }
|
2018-10-05 14:45:58 +02:00
|
|
|
|
|
2018-10-05 14:57:49 +02:00
|
|
|
|
@column_whitelist[table] || []
|
2018-10-05 14:45:58 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-04-09 14:32:29 +02:00
|
|
|
|
def self.sanitized_column(association, column)
|
|
|
|
|
table = if association == 'self'
|
|
|
|
|
Dossier.table_name
|
2019-10-16 13:02:42 +02:00
|
|
|
|
elsif (association_reflection = Dossier.reflect_on_association(association))
|
|
|
|
|
association_reflection.klass.table_name
|
2019-04-09 14:32:29 +02:00
|
|
|
|
else
|
2019-10-16 13:02:42 +02:00
|
|
|
|
# Allow filtering on a joined table alias (which doesn’t exist
|
|
|
|
|
# in the ActiveRecord domain).
|
|
|
|
|
association
|
2019-04-09 14:32:29 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
[table, column]
|
2019-02-26 19:12:05 +01:00
|
|
|
|
.map { |name| ActiveRecord::Base.connection.quote_column_name(name) }
|
|
|
|
|
.join('.')
|
2018-10-05 10:08:39 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-10-16 13:02:42 +02:00
|
|
|
|
def assert_supported_column(table, column)
|
|
|
|
|
if table == 'followers_instructeurs' && column != 'email'
|
|
|
|
|
raise ArgumentError, 'Table `followers_instructeurs` only supports the `email` column.'
|
|
|
|
|
end
|
2023-01-13 16:40:48 +01:00
|
|
|
|
if table == 'groupe_instructeur' && (column != 'label' && column != 'id')
|
|
|
|
|
raise ArgumentError, 'Table `groupe_instructeur` only supports the `label` or `id` column.'
|
|
|
|
|
end
|
2019-10-16 13:02:42 +02:00
|
|
|
|
end
|
2017-10-02 17:03:30 +02:00
|
|
|
|
end
|