Merge pull request #4710 from betagouv/4703-fix-menu-deroulant-liee

#4703 - fix menu déroulant lié obligatoire
This commit is contained in:
Keirua 2020-01-28 14:35:50 +01:00 committed by GitHub
commit 0f88409f4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View file

@ -45,8 +45,9 @@ class Champs::LinkedDropDownListChamp < Champ
value.present? ? { primary: primary_value, secondary: secondary_value } : nil
end
def mandatory_and_blank?
mandatory? && (primary_value.blank? || secondary_value.blank?)
def blank?
primary_value.blank? ||
(has_secondary_options_for_primary? && secondary_value.blank?)
end
def search_terms
@ -58,4 +59,8 @@ class Champs::LinkedDropDownListChamp < Champ
def pack_value(primary, secondary)
self.value = JSON.generate([primary, secondary])
end
def has_secondary_options_for_primary?
primary_value.present? && secondary_options[primary_value]&.any?(&:present?)
end
end

View file

@ -91,7 +91,14 @@ describe Champs::LinkedDropDownListChamp do
end
context 'when there is a secondary value' do
before { subject.secondary_value = 'Primary' }
before { subject.secondary_value = 'Secondary' }
it { is_expected.not_to be_mandatory_and_blank }
end
context 'when there is nothing to select for the secondary value' do
let(:drop_down_list) { build(:drop_down_list, value: "--A--\nAbbott\nAbelard\n--B--\n--C--\nCynthia") }
before { subject.primary_value = 'B' }
it { is_expected.not_to be_mandatory_and_blank }
end