From 1335336d9817cbb5b2ba4a8fbba62d91835d94bd Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 20 Nov 2024 16:10:01 +0100 Subject: [PATCH] refactor(column): add label_for_value method --- .../instructeurs/filter_buttons_component.rb | 25 ++----------------- app/models/column.rb | 10 ++++++++ app/services/exported_column_formatter.rb | 5 +--- .../filter_buttons_component_spec.rb | 6 ++--- 4 files changed, 16 insertions(+), 30 deletions(-) diff --git a/app/components/instructeurs/filter_buttons_component.rb b/app/components/instructeurs/filter_buttons_component.rb index d8917c277..1da240db4 100644 --- a/app/components/instructeurs/filter_buttons_component.rb +++ b/app/components/instructeurs/filter_buttons_component.rb @@ -48,31 +48,10 @@ class Instructeurs::FilterButtonsComponent < ApplicationComponent def human_value(filter_column) column, filter = filter_column.column, filter_column.filter - if column.type_de_champ? - find_type_de_champ(column.stable_id).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.dossier_labels? - Label.find(filter)&.name || filter - elsif column.type == :date + if column.type == :date || column.type == :datetime helpers.try_parse_format_date(filter) else - filter + column.label_for_value(filter) end end - - def find_type_de_champ(stable_id) - 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/models/column.rb b/app/models/column.rb index b02feda10..f41ce3cdb 100644 --- a/app/models/column.rb +++ b/app/models/column.rb @@ -51,6 +51,16 @@ class Column def dossier_column? = false def champ_column? = false + def label_for_value(value) + if options_for_select.present? + # options for select store ["trad", :enum_value] + options_for_select.to_h { |(label, value)| [value.to_s, label] } + .fetch(value.to_s, value.to_s) + else + value + end + end + private def column_id = "#{table}/#{column}" diff --git a/app/services/exported_column_formatter.rb b/app/services/exported_column_formatter.rb index 824b26d29..c71835a17 100644 --- a/app/services/exported_column_formatter.rb +++ b/app/services/exported_column_formatter.rb @@ -44,9 +44,6 @@ class ExportedColumnFormatter end def self.format_enum(column:, raw_value:) - # options for select store ["trad", :enum_value] - selected_option = column.options_for_select.find { _1[1].to_s == raw_value } - - selected_option ? selected_option.first : raw_value + column.label_for_value(raw_value) end end diff --git a/spec/components/instructeurs/filter_buttons_component_spec.rb b/spec/components/instructeurs/filter_buttons_component_spec.rb index e022bc150..5cffb1832 100644 --- a/spec/components/instructeurs/filter_buttons_component_spec.rb +++ b/spec/components/instructeurs/filter_buttons_component_spec.rb @@ -35,7 +35,7 @@ describe Instructeurs::FilterButtonsComponent, type: :component do let(:filter) { to_filter(['État du dossier', "en_construction"]) } it 'should get i18n value' do - expect(page).to have_text("En construction") + expect(page).to have_text("En construction") end end @@ -57,7 +57,7 @@ describe Instructeurs::FilterButtonsComponent, type: :component do end it 'should display all filters' do - text = "État du dossier : En construction ou État du dossier : En instruction et Date de création : 15/06/2023" + text = "État du dossier : En construction ou État du dossier : En instruction et Date de création : 15/06/2023" expect(page).to have_text(text) end end @@ -78,7 +78,7 @@ describe Instructeurs::FilterButtonsComponent, type: :component do expect(page.all('form').count).to eq(2) del_en_construction = page.all('form').first - expect(del_en_construction).to have_text('En construction') + expect(del_en_construction).to have_text('En construction') expect(del_en_construction).to have_field('filters[]', with: '', type: 'hidden') expect(del_en_construction).to have_field('filters[][id]', with: column_id, type: 'hidden') expect(del_en_construction).to have_field('filters[][filter]', with: 'en_instruction', type: 'hidden')