Skip to content

Commit

Permalink
fix(referentiel): implement champ_value for referentiel champs
Browse files Browse the repository at this point in the history
  • Loading branch information
tchak committed Mar 3, 2025
1 parent f668503 commit e6f9765
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 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)

Check warning on line 7 in app/models/types_de_champ/drop_down_list_type_de_champ.rb

View check run for this annotation

Codecov / codecov/patch

app/models/types_de_champ/drop_down_list_type_de_champ.rb#L7

Added line #L7 was not covered by tests
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

Check warning on line 22 in app/models/types_de_champ/drop_down_list_type_de_champ.rb

View check run for this annotation

Codecov / codecov/patch

app/models/types_de_champ/drop_down_list_type_de_champ.rb#L22

Added line #L22 was not covered by tests
champ.referentiel_item_value(path)
else
super
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 e6f9765

Please sign in to comment.