From 65b4bcf3a15d57f2d3ac9dadec149e8c4ac051dc Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Mon, 27 Jan 2020 10:49:01 +0100 Subject: [PATCH] =?UTF-8?q?Permet=20de=20d=C3=A9poser=20un=20dossier=20lor?= =?UTF-8?q?squ'un=20menu=20d=C3=A9roulant=20li=C3=A9=20obligatoire=20n'a?= =?UTF-8?q?=20pas=20de=20valeur=20(car=20la=20liste=20est=20l=C3=A9gitimem?= =?UTF-8?q?ent=20vide)=20dans=20le=20second=20champ.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/champs/linked_drop_down_list_champ.rb | 9 +++++++-- spec/models/champs/linked_drop_down_list_champ_spec.rb | 9 ++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) 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