diff --git a/app/components/instructeurs/filter_buttons_component.rb b/app/components/instructeurs/filter_buttons_component.rb index 960b67b4e..950adee9b 100644 --- a/app/components/instructeurs/filter_buttons_component.rb +++ b/app/components/instructeurs/filter_buttons_component.rb @@ -42,6 +42,37 @@ class Instructeurs::FilterButtonsComponent < ApplicationComponent end def button_content(filter) - "#{filter.column.label.truncate(50)} : #{@procedure_presentation.human_value_for_filter(filter)}" + "#{filter.label.truncate(50)} : #{human_value(filter)}" + end + + def human_value(filter_column) + column, filter = filter_column.column, filter_column.filter + + if column.type_de_champ? + find_type_de_champ(column.column).dynamic_type.filter_to_human(filter) + elsif column.dossier_state? + if filter == 'pending_correction' + Dossier.human_attribute_name("pending_correction.for_instructeur") + else + Dossier.human_attribute_name("state.#{filter}") + end + elsif column.groupe_instructeur? + current_instructeur.groupe_instructeurs + .find { _1.id == filter.to_i }&.label || filter + elsif column.type == :date + helpers.try_parse_format_date(filter) + else + filter + end + end + + def find_type_de_champ(column) + stable_id = column.to_s.split('->').first + + TypeDeChamp + .joins(:revision_types_de_champ) + .where(revision_types_de_champ: { revision_id: @procedure_presentation.procedure.revisions }) + .order(created_at: :desc) + .find_by(stable_id:) end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 88a398cd7..3fdc45795 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -113,6 +113,10 @@ module ApplicationHelper datetime.present? ? I18n.l(datetime, format:) : '' end + def try_parse_format_date(date) + date.then { Date.parse(_1) rescue nil }&.then { I18n.l(_1) } + end + def try_format_mois_effectif(etablissement) if etablissement.entreprise_effectif_mois.present? && etablissement.entreprise_effectif_annee.present? [etablissement.entreprise_effectif_mois, etablissement.entreprise_effectif_annee].join('/') diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index c45e6ac20..2df4db930 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true class ProcedurePresentation < ApplicationRecord - TYPE_DE_CHAMP = 'type_de_champ' - belongs_to :assign_to, optional: false has_many :exports, dependent: :destroy @@ -42,45 +40,4 @@ class ProcedurePresentation < ApplicationRecord columns.concat(procedure.sva_svr_columns) if procedure.sva_svr_enabled? columns end - - 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' - Dossier.human_attribute_name("pending_correction.for_instructeur") - else - Dossier.human_attribute_name("state.#{filtered_column.filter}") - end - elsif filtered_column.column.table == 'groupe_instructeur' && filtered_column.column.column == 'id' - instructeur.groupe_instructeurs - .find { _1.id == filtered_column.filter.to_i }&.label || filtered_column.filter - else - column = procedure.columns.find { _1.table == filtered_column.column.table && _1.column == filtered_column.column.column } - - if column.type == :date - parsed_date = safe_parse_date(filtered_column.filter) - - return parsed_date.present? ? I18n.l(parsed_date) : nil - end - - filtered_column.filter - end - end - - def safe_parse_date(string) - Date.parse(string) - rescue Date::Error - nil - end - - private - - def find_type_de_champ(column) - TypeDeChamp - .joins(:revision_types_de_champ) - .where(revision_types_de_champ: { revision_id: procedure.revisions }) - .order(created_at: :desc) - .find_by(stable_id: column) - end end