2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-06 13:44:29 +01:00
|
|
|
class ProcedurePresentation < ApplicationRecord
|
2021-04-16 12:52:00 +02:00
|
|
|
TYPE_DE_CHAMP = 'type_de_champ'
|
|
|
|
|
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
|
|
|
|
2024-10-07 15:00:05 +02:00
|
|
|
attribute :displayed_columns, :column, array: true
|
|
|
|
|
2024-09-25 17:41:58 +02:00
|
|
|
attribute :sorted_column, :sorted_column
|
|
|
|
def sorted_column = super || procedure.default_sorted_column # Dummy override to set default value
|
2024-09-20 11:12:52 +02:00
|
|
|
|
2024-09-27 12:50:57 +02:00
|
|
|
attribute :a_suivre_filters, :filtered_column, array: true
|
|
|
|
attribute :suivis_filters, :filtered_column, array: true
|
|
|
|
attribute :traites_filters, :filtered_column, array: true
|
|
|
|
attribute :tous_filters, :filtered_column, array: true
|
|
|
|
attribute :supprimes_filters, :filtered_column, array: true
|
|
|
|
attribute :supprimes_recemment_filters, :filtered_column, array: true
|
|
|
|
attribute :expirant_filters, :filtered_column, array: true
|
|
|
|
attribute :archives_filters, :filtered_column, array: true
|
2024-09-20 11:12:52 +02:00
|
|
|
|
2024-10-09 11:08:41 +02:00
|
|
|
before_create { self.displayed_columns = procedure.default_displayed_columns }
|
|
|
|
|
2024-10-11 15:29:52 +02:00
|
|
|
validates_associated :displayed_columns, :sorted_column, :a_suivre_filters, :suivis_filters,
|
|
|
|
:traites_filters, :tous_filters, :supprimes_filters, :expirant_filters, :archives_filters
|
2024-10-07 09:54:17 +02:00
|
|
|
|
2024-10-07 18:10:08 +02:00
|
|
|
def filters_for(statut)
|
|
|
|
send(filters_name_for(statut))
|
|
|
|
end
|
|
|
|
|
|
|
|
def filters_name_for(statut) = statut.tr('-', '_').then { "#{_1}_filters" }
|
|
|
|
|
2022-04-14 10:30:23 +02:00
|
|
|
def displayed_fields_for_headers
|
2024-10-28 17:38:48 +01:00
|
|
|
columns = [
|
2024-10-09 09:51:47 +02:00
|
|
|
procedure.dossier_id_column,
|
2024-10-07 15:00:05 +02:00
|
|
|
*displayed_columns,
|
2024-10-28 17:38:48 +01:00
|
|
|
procedure.dossier_state_column
|
2022-04-14 10:30:23 +02:00
|
|
|
]
|
2024-10-28 17:38:48 +01:00
|
|
|
columns.concat(procedure.sva_svr_columns) if procedure.sva_svr_enabled?
|
|
|
|
columns
|
2023-08-31 15:25:29 +02:00
|
|
|
end
|
|
|
|
|
2024-09-27 13:09:59 +02:00
|
|
|
def human_value_for_filter(filtered_column)
|
|
|
|
if filtered_column.column.table == TYPE_DE_CHAMP
|
|
|
|
find_type_de_champ(filtered_column.column.column).dynamic_type.filter_to_human(filtered_column.filter)
|
|
|
|
elsif filtered_column.column.column == 'state'
|
|
|
|
if filtered_column.filter == 'pending_correction'
|
2023-03-27 19:11:22 +02:00
|
|
|
Dossier.human_attribute_name("pending_correction.for_instructeur")
|
|
|
|
else
|
2024-09-27 13:09:59 +02:00
|
|
|
Dossier.human_attribute_name("state.#{filtered_column.filter}")
|
2023-03-27 19:11:22 +02:00
|
|
|
end
|
2024-09-27 13:09:59 +02:00
|
|
|
elsif filtered_column.column.table == 'groupe_instructeur' && filtered_column.column.column == 'id'
|
2023-01-13 16:40:48 +01:00
|
|
|
instructeur.groupe_instructeurs
|
2024-09-27 13:09:59 +02:00
|
|
|
.find { _1.id == filtered_column.filter.to_i }&.label || filtered_column.filter
|
2020-06-23 11:33:46 +02:00
|
|
|
else
|
2024-09-27 13:09:59 +02:00
|
|
|
column = procedure.columns.find { _1.table == filtered_column.column.table && _1.column == filtered_column.column.column }
|
2023-06-08 19:01:23 +02:00
|
|
|
|
2024-08-19 14:34:36 +02:00
|
|
|
if column.type == :date
|
2024-09-27 13:09:59 +02:00
|
|
|
parsed_date = safe_parse_date(filtered_column.filter)
|
2023-06-08 19:01:23 +02:00
|
|
|
|
|
|
|
return parsed_date.present? ? I18n.l(parsed_date) : nil
|
|
|
|
end
|
|
|
|
|
2024-09-27 13:09:59 +02:00
|
|
|
filtered_column.filter
|
2020-06-08 14:33:58 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-08 19:01:23 +02:00
|
|
|
def safe_parse_date(string)
|
|
|
|
Date.parse(string)
|
|
|
|
rescue Date::Error
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2018-10-02 17:04:23 +02:00
|
|
|
private
|
|
|
|
|
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 })
|
2023-11-14 09:32:25 +01:00
|
|
|
.order(created_at: :desc)
|
2022-07-28 11:28:17 +02:00
|
|
|
.find_by(stable_id: column)
|
2020-10-30 15:07:24 +01:00
|
|
|
end
|
2017-10-02 17:03:30 +02:00
|
|
|
end
|