Skip to content

Commit

Permalink
Global form items (#3840)
Browse files Browse the repository at this point in the history
Add support for defining global form attributes so you can define them once and reuse them all over.
  • Loading branch information
johrstrom authored Oct 11, 2024
1 parent 12ba512 commit 1305b7c
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
1 change: 1 addition & 0 deletions apps/dashboard/app/lib/smart_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions apps/dashboard/app/lib/smart_attributes/attribute_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions apps/dashboard/config/configuration_singleton.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down
70 changes: 69 additions & 1 deletion apps/dashboard/test/system/batch_connect_widgets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,72 @@ def make_bc_app(dir, form)
assert_equal label.text, "Number of Cores (1-8)"
end
end
end

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

0 comments on commit 1305b7c

Please sign in to comment.