From f66850393be3ebf84ea2a0410b176694ad287076 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 3 Mar 2025 11:03:21 +0100 Subject: [PATCH 1/2] fix(referentiel): in referentiel mode do not print all the options --- app/views/dossiers/dossier_vide.pdf.prawn | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/views/dossiers/dossier_vide.pdf.prawn b/app/views/dossiers/dossier_vide.pdf.prawn index 81b4627353f..ae7af33dfe3 100644 --- a/app/views/dossiers/dossier_vide.pdf.prawn +++ b/app/views/dossiers/dossier_vide.pdf.prawn @@ -163,13 +163,17 @@ def render_single_champ(pdf, revision, type_de_champ) when TypeDeChamp.type_champs.fetch(:address), TypeDeChamp.type_champs.fetch(:carte), TypeDeChamp.type_champs.fetch(:textarea) format_in_2_lines(pdf, type_de_champ, 5) when TypeDeChamp.type_champs.fetch(:drop_down_list) - add_libelle(pdf, type_de_champ) - add_optionnal_description(pdf, type_de_champ) - add_explanation(pdf, 'Cochez la mention applicable, une seule valeur possible') - type_de_champ.drop_down_options.each do |option| - format_with_checkbox(pdf, option) + if type_de_champ.referentiel_mode? + format_in_2_lines(pdf, type_de_champ) + else + add_libelle(pdf, type_de_champ) + add_optionnal_description(pdf, type_de_champ) + add_explanation(pdf, 'Cochez la mention applicable, une seule valeur possible') + type_de_champ.drop_down_options.each do |option| + format_with_checkbox(pdf, option) + end + pdf.text "\n" end - pdf.text "\n" when TypeDeChamp.type_champs.fetch(:multiple_drop_down_list) add_libelle(pdf, type_de_champ) add_optionnal_description(pdf, type_de_champ) From e6f976571cee8b18eb874f6dcef140d253be2e8e Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 3 Mar 2025 11:10:54 +0100 Subject: [PATCH 2/2] fix(referentiel): implement champ_value for referentiel champs --- app/models/champs/drop_down_list_champ.rb | 10 ++------ .../drop_down_list_type_de_champ.rb | 11 ++++++++- .../champs/drop_down_list/_show.html.haml | 2 +- .../champs/drop_down_list_champ_spec.rb | 24 ++++++++++++++++++- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/app/models/champs/drop_down_list_champ.rb b/app/models/champs/drop_down_list_champ.rb index 1e07a6832ff..a366dfd6f57 100644 --- a/app/models/champs/drop_down_list_champ.rb +++ b/app/models/champs/drop_down_list_champ.rb @@ -78,19 +78,13 @@ def referentiel_item_column_values referentiel_headers.map { |(header, path)| [header, referentiel_item_value(path)] } end - def referentiel_item_first_column_value - return nil if referentiel.nil? - path = referentiel_headers&.first&.second - referentiel_item_value(path) - end - - private - def referentiel_headers headers = referentiel&.dig('data', 'headers') || [] headers.map { [_1, Referentiel.header_to_path(_1)] } end + private + def referentiel_from(value) if value.present? referentiel_item = type_de_champ.referentiel.items.find(value) diff --git a/app/models/types_de_champ/drop_down_list_type_de_champ.rb b/app/models/types_de_champ/drop_down_list_type_de_champ.rb index d65a9e1584e..ab03811df71 100644 --- a/app/models/types_de_champ/drop_down_list_type_de_champ.rb +++ b/app/models/types_de_champ/drop_down_list_type_de_champ.rb @@ -1,6 +1,15 @@ # frozen_string_literal: true class TypesDeChamp::DropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBase + def champ_value(champ) + if referentiel_mode? && champ.respond_to?(:referentiel) && champ.referentiel.present? + path = champ.referentiel_headers&.first&.second + champ.referentiel_item_value(path) + else + super + end + end + def champ_value_for_export(champ, path = :value) if referentiel_mode? && path != :value champ.referentiel_item_value(path) @@ -10,7 +19,7 @@ def champ_value_for_export(champ, path = :value) end def champ_value_for_tag(champ, path = :value) - if referentiel_mode? + if referentiel_mode? && path != :value champ.referentiel_item_value(path) else super diff --git a/app/views/shared/champs/drop_down_list/_show.html.haml b/app/views/shared/champs/drop_down_list/_show.html.haml index e495d9ba36b..ccb77504aad 100644 --- a/app/views/shared/champs/drop_down_list/_show.html.haml +++ b/app/views/shared/champs/drop_down_list/_show.html.haml @@ -7,7 +7,7 @@ %p= value - else - %p= champ.referentiel_item_first_column_value + %p= champ.to_s - elsif champ.used_by_routing_rules? && champ.dossier.forced_groupe_instructeur %p diff --git a/spec/models/champs/drop_down_list_champ_spec.rb b/spec/models/champs/drop_down_list_champ_spec.rb index 8d757517f4b..333976a1347 100644 --- a/spec/models/champs/drop_down_list_champ_spec.rb +++ b/spec/models/champs/drop_down_list_champ_spec.rb @@ -1,9 +1,11 @@ # frozen_string_literal: true describe Champs::DropDownListChamp do - let(:types_de_champ_public) { [{ type: :drop_down_list, drop_down_other: other }] } + let(:types_de_champ_public) { [{ type: :drop_down_list, drop_down_other: other, referentiel:, drop_down_mode: }] } let(:procedure) { create(:procedure, types_de_champ_public:) } let(:dossier) { create(:dossier, procedure:) } + let(:referentiel) { nil } + let(:drop_down_mode) { nil } let(:champ) { dossier.champs.first.tap { _1.update(value:, other:) } } let(:value) { nil } let(:other) { nil } @@ -78,4 +80,24 @@ end end end + + describe 'referentiel' do + let(:drop_down_mode) { 'advanced' } + let(:referentiel) { create(:csv_referentiel, :with_items) } + let(:item) { referentiel.items.first } + let(:value) { item.id.to_s } + + it '#referentiel_headers' do + expect(champ.referentiel_headers).to eq([["option", "option"], ["calorie (kcal)", "calorie_kcal"], ["poids (g)", "poids_g"]]) + end + + it '#to_s' do + expect(champ.value).to eq(value) + expect(champ.to_s).to eq(item.value(champ.referentiel_headers.first.second)) + end + + it '#referentiel_item_column_values' do + expect(champ.referentiel_item_column_values).to eq([["option", "fromage"], ["calorie (kcal)", "145"], ["poids (g)", "60"]]) + end + end end