Skip to content

Commit

Permalink
Merge pull request #11386 from tchak/fix-pdf-generation
Browse files Browse the repository at this point in the history
fix(referentiel): implement champ_value for referentiel champs
  • Loading branch information
tchak authored Mar 4, 2025
2 parents 70ea903 + e6f9765 commit f21255f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
10 changes: 2 additions & 8 deletions app/models/champs/drop_down_list_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion app/models/types_de_champ/drop_down_list_type_de_champ.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down
16 changes: 10 additions & 6 deletions app/views/dossiers/dossier_vide.pdf.prawn
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/champs/drop_down_list/_show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion spec/models/champs/drop_down_list_champ_spec.rb
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down Expand Up @@ -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

0 comments on commit f21255f

Please sign in to comment.