Skip to content

Commit

Permalink
Merge pull request #10412 from tchak/fix-champ-type-de-champ-mismatch
Browse files Browse the repository at this point in the history
fix(champ): champ value formatters should check champ type
  • Loading branch information
tchak authored May 13, 2024
2 parents d4e6e2e + a1db089 commit 1750641
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
27 changes: 18 additions & 9 deletions app/models/type_de_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,7 @@ def public_id(row_id)
class << self
def champ_value(type_champ, champ)
dynamic_type_class = type_champ_to_class_name(type_champ).constantize
# special case for linked drop down champ – it's blank implementation is not what you think
if (type_champ != TypeDeChamp.type_champs.fetch(:linked_drop_down_list) || champ.nil? || champ.value.blank?) && champ.blank?
if use_default_value?(type_champ, champ)
dynamic_type_class.champ_default_value
else
dynamic_type_class.champ_value(champ)
Expand All @@ -696,8 +695,7 @@ def champ_value(type_champ, champ)

def champ_value_for_api(type_champ, champ, version = 2)
dynamic_type_class = type_champ_to_class_name(type_champ).constantize
# special case for linked drop down champ – it's blank implementation is not what you think
if (type_champ != TypeDeChamp.type_champs.fetch(:linked_drop_down_list) || champ.nil? || champ.value.blank?) && champ.blank?
if use_default_value?(type_champ, champ)
dynamic_type_class.champ_default_api_value(version)
else
dynamic_type_class.champ_value_for_api(champ, version)
Expand All @@ -706,20 +704,18 @@ def champ_value_for_api(type_champ, champ, version = 2)

def champ_value_for_export(type_champ, champ, path = :value)
dynamic_type_class = type_champ_to_class_name(type_champ).constantize
# special case for linked drop down champ – it's blank implementation is not what you think
if (type_champ != TypeDeChamp.type_champs.fetch(:linked_drop_down_list) || champ.nil? || champ.value.blank?) && champ.blank?
if use_default_value?(type_champ, champ)
dynamic_type_class.champ_default_export_value(path)
else
dynamic_type_class.champ_value_for_export(champ, path)
end
end

def champ_value_for_tag(type_champ, champ, path = :value)
dynamic_type_class = type_champ_to_class_name(type_champ).constantize
# special case for linked drop down champ – it's blank implementation is not what you think
if (type_champ != TypeDeChamp.type_champs.fetch(:linked_drop_down_list) || champ.nil? || champ.value.blank?) && champ.blank?
if use_default_value?(type_champ, champ)
''
else
dynamic_type_class = type_champ_to_class_name(type_champ).constantize
dynamic_type_class.champ_value_for_tag(champ, path)
end
end
Expand All @@ -731,6 +727,19 @@ def type_champ_to_champ_class_name(type_champ)
def type_champ_to_class_name(type_champ)
"TypesDeChamp::#{type_champ.classify}TypeDeChamp"
end

private

def use_default_value?(type_champ, champ)
# no champ
return true if champ.nil?
# type de champ on the revision changed
return true if type_champ_to_champ_class_name(type_champ) != champ.type
# special case for linked drop down champ – it's blank implementation is not what you think
return champ.value.blank? if type_champ == TypeDeChamp.type_champs.fetch(:linked_drop_down_list)

champ.blank?
end
end

private
Expand Down
8 changes: 8 additions & 0 deletions spec/models/champ_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@

it { expect(champ.for_export).to eq('Crétinier, Mousserie') }
end

context 'when type_de_champ and champ.type mismatch' do
let(:champ_yes_no) { create(:champ_yes_no, value: 'true') }
let(:champ_text) { create(:champ_text, value: 'Hello') }

it { expect(TypeDeChamp.champ_value_for_export(champ_text.type_champ, champ_yes_no)).to eq(nil) }
it { expect(TypeDeChamp.champ_value_for_export(champ_yes_no.type_champ, champ_text)).to eq('Non') }
end
end

describe '#search_terms' do
Expand Down
2 changes: 1 addition & 1 deletion spec/serializers/champ_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
end

context 'when type champ is not piece justificative' do
let(:champ) { create(:champ, value: "blah") }
let(:champ) { create(:champ_text, value: "blah") }

it { is_expected.to include(value: "blah") }
end
Expand Down

0 comments on commit 1750641

Please sign in to comment.