diff --git a/app/models/champs/linked_drop_down_list_champ.rb b/app/models/champs/linked_drop_down_list_champ.rb index 46011439c..7be6f01c9 100644 --- a/app/models/champs/linked_drop_down_list_champ.rb +++ b/app/models/champs/linked_drop_down_list_champ.rb @@ -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 diff --git a/spec/models/champs/linked_drop_down_list_champ_spec.rb b/spec/models/champs/linked_drop_down_list_champ_spec.rb index 64ec6d092..204015030 100644 --- a/spec/models/champs/linked_drop_down_list_champ_spec.rb +++ b/spec/models/champs/linked_drop_down_list_champ_spec.rb @@ -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