From c2f1548931a58a30d08e4314fa2935e083cd3359 Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Wed, 2 Oct 2024 08:37:17 +0100 Subject: [PATCH] fixup! Add project dataset select format --- app/models/dataset/value.rb | 2 +- spec/models/dataset/value_spec.rb | 41 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/models/dataset/value.rb b/app/models/dataset/value.rb index 3e53dc2bd4..c1488009cb 100644 --- a/app/models/dataset/value.rb +++ b/app/models/dataset/value.rb @@ -32,7 +32,7 @@ def title def value=(new_value) case key.format when 'select' - super(new_value.to_a.reject(&:blank?).join(', ')) + super(Array(new_value).reject(&:blank?).join(', ')) else super end diff --git a/spec/models/dataset/value_spec.rb b/spec/models/dataset/value_spec.rb index 938a056309..a33c42758f 100644 --- a/spec/models/dataset/value_spec.rb +++ b/spec/models/dataset/value_spec.rb @@ -81,4 +81,45 @@ def invalid(value_to_test) end end end + + describe 'value=' do + context 'when format is a select' do + before do + value.key = FactoryBot.build(:dataset_key, :select) + end + + it 'accept an array' do + value.value = %w[a b] + expect(value.value).to eq('a, b') + end + + it 'ignores empty array values' do + value.value = %w[a] + [nil] + %w[c] + expect(value.value).to eq('a, c') + end + + it 'accepts as string' do + value.value = 'a' + expect(value.value).to eq('a') + end + end + end + + describe 'mapped_value' do + context 'when format is a boolean' do + before do + value.key = FactoryBot.build(:dataset_key, :boolean) + end + + it 'returns No for value 0' do + value.value = 0 + expect(value.mapped_value).to eq('No') + end + + it 'returns Yes for value 1' do + value.value = 1 + expect(value.mapped_value).to eq('Yes') + end + end + end end