Skip to content

Commit

Permalink
fixup! Add project dataset select format
Browse files Browse the repository at this point in the history
  • Loading branch information
gbp committed Oct 2, 2024
1 parent 3d9ba30 commit c2f1548
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/dataset/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions spec/models/dataset/value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c2f1548

Please sign in to comment.