Skip to content

Commit

Permalink
always cast select_options to an array (#3872)
Browse files Browse the repository at this point in the history
Always cast select_options to an array so that upper layers can call
.first and .second safely on them.
  • Loading branch information
johrstrom authored Oct 25, 2024
1 parent 1b076a8 commit 058ce36
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
8 changes: 8 additions & 0 deletions apps/dashboard/app/lib/smart_attributes/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ def select_choices(hide_excludable: true)
else
false
end
end.map do |entry|
# always cast to array so other layers can try .first & .second for labels and values.
# and let nils fall through and get caught in validate!
if entry.is_a?(Array)
entry
elsif entry.is_a?(String) || entry.is_a?(Symbol)
[entry.to_s, entry.to_s]
end
end
end

Expand Down
39 changes: 39 additions & 0 deletions apps/dashboard/test/system/batch_connect_widgets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,43 @@ def make_bc_app(dir, form)
assert(find("##{bc_ele_id('aa_b_cc')}").visible?)
end
end

test 'radio_buttons accept scalar and array options' do
Dir.mktmpdir do |dir|
form = <<~HEREDOC
---
cluster:
- owens
form:
- scalar
- vector
attributes:
scalar:
widget: radio_button
options:
- one
- two
vector:
widget: radio_button
options:
- [Three, three]
- [Four, four]
HEREDOC

make_bc_app(dir, form)
visit new_batch_connect_session_context_url('sys/app')

# values are all lowercase
assert_equal('one', find("##{bc_ele_id('scalar_one')}").value)
assert_equal('two', find("##{bc_ele_id('scalar_two')}").value)
assert_equal('three', find("##{bc_ele_id('vector_three')}").value)
assert_equal('four', find("##{bc_ele_id('vector_four')}").value)

# one and two's labels are lowercase, but Three and Four have uppercase labels.
assert_equal('one', find("[for='#{bc_ele_id('scalar_one')}']").text)
assert_equal('two', find("[for='#{bc_ele_id('scalar_two')}']").text)
assert_equal('Three', find("[for='#{bc_ele_id('vector_three')}']").text)
assert_equal('Four', find("[for='#{bc_ele_id('vector_four')}']").text)
end
end
end

0 comments on commit 058ce36

Please sign in to comment.