diff --git a/apps/dashboard/app/lib/smart_attributes.rb b/apps/dashboard/app/lib/smart_attributes.rb index 87356b3f7c..285a0c4aa2 100644 --- a/apps/dashboard/app/lib/smart_attributes.rb +++ b/apps/dashboard/app/lib/smart_attributes.rb @@ -23,4 +23,5 @@ module SmartAttributes require 'smart_attributes/attributes/bc_vnc_resolution' require 'smart_attributes/attributes/auto_environment_variable' require 'smart_attributes/attributes/auto_cores' + require 'smart_attributes/attributes/global_attribute' end diff --git a/apps/dashboard/app/lib/smart_attributes/attribute_factory.rb b/apps/dashboard/app/lib/smart_attributes/attribute_factory.rb index 72cd0708b2..b32b1e72de 100644 --- a/apps/dashboard/app/lib/smart_attributes/attribute_factory.rb +++ b/apps/dashboard/app/lib/smart_attributes/attribute_factory.rb @@ -3,6 +3,7 @@ class AttributeFactory AUTO_MODULES_REX = /\Aauto_modules_([\w-]+)\z/.freeze AUTO_ENVIRONMENT_VARIABLE_REX = /\Aauto_environment_variable_([\w-]+)\z/.freeze + GLOBAL_ATTRIBUTE_REX = /\Aglobal_([\w-]+)\z/.freeze class << self # Build an attribute object from an id and its options @@ -11,6 +12,7 @@ class << self # @return [Attribute] the attribute object def build(id, opts = {}) id = id.to_s + if id.match?(AUTO_MODULES_REX) hpc_mod = id.match(AUTO_MODULES_REX)[1] id = 'auto_modules' @@ -19,6 +21,10 @@ def build(id, opts = {}) env_variable = id.match(AUTO_ENVIRONMENT_VARIABLE_REX)[1] id = 'auto_environment_variable' opts = opts.merge({'key' => env_variable}) + elsif id.match?(GLOBAL_ATTRIBUTE_REX) + real_id = id + id = 'global_attribute' + opts = opts.merge({ 'key' => real_id }) end build_method = "build_#{id}" diff --git a/apps/dashboard/app/lib/smart_attributes/attributes/global_attribute.rb b/apps/dashboard/app/lib/smart_attributes/attributes/global_attribute.rb new file mode 100644 index 0000000000..a8d3a534dd --- /dev/null +++ b/apps/dashboard/app/lib/smart_attributes/attributes/global_attribute.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module SmartAttributes + class AttributeFactory + # Build this attribute object with defined options + # @param opts [Hash] attribute's options + # @return [Attributes::GlobalAttribute] the attribute object + def self.build_global_attribute(opts = {}) + id = opts.delete('key').to_s + + config = Configuration.global_bc_form_item(id) + + # this behaves like a normal Attribute if there's no actual configuration for it. + config = opts if config.nil? || config.empty? + Attributes::GlobalAttribute.new(id, config) + end + end + + module Attributes + class GlobalAttribute < Attribute + end + end +end diff --git a/apps/dashboard/config/configuration_singleton.rb b/apps/dashboard/config/configuration_singleton.rb index 8672bf7bf1..68ecff7324 100644 --- a/apps/dashboard/config/configuration_singleton.rb +++ b/apps/dashboard/config/configuration_singleton.rb @@ -214,6 +214,13 @@ def launcher_default_items config.fetch(:launcher_default_items, []).to_a end + def global_bc_form_item(key) + return nil if key.nil? || key.to_s.empty? + + all = config.fetch(:global_bc_form_items, {}).to_h + all[key.to_sym] + end + # Load the dotenv local files first, then the /etc dotenv files and # the .env and .env.production or .env.development files. # diff --git a/apps/dashboard/test/system/batch_connect_widgets_test.rb b/apps/dashboard/test/system/batch_connect_widgets_test.rb index 1d59fc40e0..b7e3cf4bd4 100644 --- a/apps/dashboard/test/system/batch_connect_widgets_test.rb +++ b/apps/dashboard/test/system/batch_connect_widgets_test.rb @@ -114,4 +114,72 @@ def make_bc_app(dir, form) assert_equal label.text, "Number of Cores (1-8)" end end -end \ No newline at end of file + + test 'global_bc_form_items work correctly' do + Dir.mktmpdir do |dir| + app_dir = "#{dir}/app".tap { |d| FileUtils.mkdir(d) } + Configuration.stubs(:config).returns({ + global_bc_form_items: { + global_queues: { + widget: 'select', + label: 'Special Queues', + options: [ + ['A', 'a'], + ['B', 'b'], + ['C', 'c'] + ] + } + } + }) + + form = <<~HEREDOC + --- + cluster: + - owens + form: + - global_queues + HEREDOC + + make_bc_app(app_dir, form) + visit new_batch_connect_session_context_url('sys/app') + + widget = find("##{bc_ele_id('global_queues')}") + options = find_all_options('global_queues', nil) + label = find("[for='#{bc_ele_id('global_queues')}']") + + assert_equal('select', widget.tag_name) + assert_equal(['a', 'b', 'c'], options.map(&:value)) + assert_equal(['A', 'B', 'C'], options.map(&:text)) + + assert_equal('Special Queues', label.text) + end + end + + test 'global_bc_form_items default correctly' do + Dir.mktmpdir do |dir| + app_dir = "#{dir}/app".tap { |d| FileUtils.mkdir(d) } + + # no configuration for 'global_queues' + Configuration.stubs(:config).returns({}) + + form = <<~HEREDOC + --- + cluster: + - owens + form: + - global_queues + HEREDOC + + make_bc_app(app_dir, form) + visit new_batch_connect_session_context_url('sys/app') + + widget = find("##{bc_ele_id('global_queues')}") + label = find("[for='#{bc_ele_id('global_queues')}']") + + # not a select widget, it's a text input with the default label + assert_equal('input', widget.tag_name) + assert_equal('text', widget[:type]) + assert_equal('Global Queues', label.text) + end + end +end