From 058ce3663b449844f4febf82a5c106b38106f208 Mon Sep 17 00:00:00 2001 From: Jeff Ohrstrom Date: Fri, 25 Oct 2024 09:58:03 -0400 Subject: [PATCH] always cast select_options to an array (#3872) Always cast select_options to an array so that upper layers can call .first and .second safely on them. --- .../app/lib/smart_attributes/attribute.rb | 8 ++++ .../test/system/batch_connect_widgets_test.rb | 39 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/apps/dashboard/app/lib/smart_attributes/attribute.rb b/apps/dashboard/app/lib/smart_attributes/attribute.rb index ec39b9a31..39b8260e0 100644 --- a/apps/dashboard/app/lib/smart_attributes/attribute.rb +++ b/apps/dashboard/app/lib/smart_attributes/attribute.rb @@ -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 diff --git a/apps/dashboard/test/system/batch_connect_widgets_test.rb b/apps/dashboard/test/system/batch_connect_widgets_test.rb index d990fc25d..97e36a640 100644 --- a/apps/dashboard/test/system/batch_connect_widgets_test.rb +++ b/apps/dashboard/test/system/batch_connect_widgets_test.rb @@ -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