move human_value_for_filter to filter_buttons_component

This commit is contained in:
simon lehericey 2024-10-28 10:15:52 +01:00
parent f50e63ea40
commit c28087ba3b
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
3 changed files with 36 additions and 44 deletions

View file

@ -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

View file

@ -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('/')

View file

@ -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