From 5900c4564dff594d7e60ec4ec92e902550715934 Mon Sep 17 00:00:00 2001 From: Aday Bujeda Date: Mon, 25 Sep 2023 12:53:00 +0100 Subject: [PATCH] Improved implementation for fixed values in Script forms --- .../app/controllers/scripts_controller.rb | 6 ++-- .../app/javascript/packs/script_edit.js | 31 +++++++++++++++++++ apps/dashboard/app/models/script.rb | 2 +- .../_edit_fixed_field.html.erb | 12 +++++++ .../_editable_number.html.erb | 8 +---- .../_editable_select.html.erb | 12 ++----- apps/dashboard/test/models/script_test.rb | 10 ++++++ 7 files changed, 61 insertions(+), 20 deletions(-) create mode 100644 apps/dashboard/app/views/scripts/editable_form_fields/_edit_fixed_field.html.erb diff --git a/apps/dashboard/app/controllers/scripts_controller.rb b/apps/dashboard/app/controllers/scripts_controller.rb index 0746925c89..fe93fdaae0 100644 --- a/apps/dashboard/app/controllers/scripts_controller.rb +++ b/apps/dashboard/app/controllers/scripts_controller.rb @@ -7,8 +7,10 @@ class ScriptsController < ApplicationController before_action :find_script, only: [:show, :edit, :destroy, :submit, :save] SAVE_SCRIPT_KEYS = [ - :cluster, :auto_accounts, :auto_accounts_exclude, :auto_accounts_fixed, :auto_scripts, :auto_scripts_exclude, :auto_scripts_fixed, - :auto_queues, :auto_queues_exclude, :auto_queues_fixed, :auto_batch_clusters, :auto_batch_clusters_exclude, :auto_batch_clusters_fixed, + :cluster, :auto_accounts, :auto_accounts_exclude, :auto_accounts_fixed, + :auto_scripts, :auto_scripts_exclude, :auto_scripts_fixed, + :auto_queues, :auto_queues_exclude, :auto_queues_fixed, + :auto_batch_clusters, :auto_batch_clusters_exclude, :auto_batch_clusters_fixed, :bc_num_slots, :bc_num_slots_fixed, :bc_num_slots_min, :bc_num_slots_max, :bc_num_hours, :bc_num_hours_fixed, :bc_num_hours_min, :bc_num_hours_max ].freeze diff --git a/apps/dashboard/app/javascript/packs/script_edit.js b/apps/dashboard/app/javascript/packs/script_edit.js index 861551c9cd..8c9a0f9c8c 100644 --- a/apps/dashboard/app/javascript/packs/script_edit.js +++ b/apps/dashboard/app/javascript/packs/script_edit.js @@ -125,11 +125,33 @@ function addInProgressField(event) { entireDiv.remove(); } +function fixedFieldEnabled(checkbox, dataElement) { + dataElement.disabled = true; + const input = $('').attr('type','hidden').attr('name', dataElement.name).attr('value', dataElement.value); + $(checkbox).after(input); +} + +function toggleFixed(event) { + const dataElement = document.getElementById(event.target.dataset.selectId); + if (event.target.checked) { + fixedFieldEnabled(event.target, dataElement) + } else { + dataElement.disabled = false; + $(`input[type=hidden][name="${dataElement.name}"]`).remove(); + } +} + function enableOrDisableSelectOption(event) { const toggleAction = event.target.dataset.selectToggler; const li = event.target.parentElement; event.target.disabled = true; + if(toggleAction == 'fixed') { + toggleFixed(event) + event.target.disabled = false; + return + } + const inputId = event.target.dataset.target; const choice = $(li).find('[data-select-value]')[0].textContent; @@ -197,6 +219,15 @@ function initSelectFields(){ optionToToggle.disabled = true; optionToToggle.selected = false; }); + + // find all the enabled 'fixed' checkboxes + allButtons.filter((fixedFieldCheckbox) => { + return fixedFieldCheckbox.checked; + // now disable the select field + }).map((fixedFieldCheckbox) => { + const dataElement = document.getElementById(fixedFieldCheckbox.dataset.selectId); + fixedFieldEnabled(fixedFieldCheckbox, dataElement) + }); } jQuery(() => { diff --git a/apps/dashboard/app/models/script.rb b/apps/dashboard/app/models/script.rb index 1a2edde3c5..15e678c295 100644 --- a/apps/dashboard/app/models/script.rb +++ b/apps/dashboard/app/models/script.rb @@ -201,7 +201,7 @@ def self.script_form_file(script_path) # parameters you got from the controller that affect the attributes, not form. # i.e., mins & maxes you set in the form but get serialized to the 'attributes' section. def attribute_parameter?(name) - name.end_with?('_min') || name.end_with?('_max') || name.end_with?('_exclude') || name.end_with?('_fixed') + ['min', 'max', 'exclude', 'fixed'].any? { |postfix| name && name.end_with?("_#{postfix}") } end # update the 'form' portion of the yaml file given 'params' from the controller. diff --git a/apps/dashboard/app/views/scripts/editable_form_fields/_edit_fixed_field.html.erb b/apps/dashboard/app/views/scripts/editable_form_fields/_edit_fixed_field.html.erb new file mode 100644 index 0000000000..172885e721 --- /dev/null +++ b/apps/dashboard/app/views/scripts/editable_form_fields/_edit_fixed_field.html.erb @@ -0,0 +1,12 @@ +<%- + field_id = "#{form.object_name}_#{attrib.id}" + + fixed_id = "#{field_id}_fixed" + fixed_name = "#{form.object_name}[#{attrib.id}_fixed]" +-%> +
+
+ data-select-toggler="fixed" data-select-id="<%= field_id %>"> + +
+
diff --git a/apps/dashboard/app/views/scripts/editable_form_fields/_editable_number.html.erb b/apps/dashboard/app/views/scripts/editable_form_fields/_editable_number.html.erb index 987c39acf4..c65aa17857 100644 --- a/apps/dashboard/app/views/scripts/editable_form_fields/_editable_number.html.erb +++ b/apps/dashboard/app/views/scripts/editable_form_fields/_editable_number.html.erb @@ -18,13 +18,7 @@
-
-
- > - -
-
- + <%= render(partial: 'scripts/editable_form_fields/edit_fixed_field', locals: { form: form, attrib: attrib, fixed: fixed }) %>
@@ -12,12 +9,7 @@
-
-
- > - -
-
+ <%= render(partial: 'scripts/editable_form_fields/edit_fixed_field', locals: { form: form, attrib: attrib, fixed: fixed }) %>
    <%- attrib.select_choices(hide_excludable: false).each do |select_data| %> @@ -55,5 +47,5 @@ value="<%= attrib.exclude_select_choices.join(',') %>"> - <%= render(partial: 'scripts/editable_form_fields/edit_field_buttons', locals: { field_id: field_id, fixed_name: fixed_name }) %> + <%= render(partial: 'scripts/editable_form_fields/edit_field_buttons', locals: { field_id: field_id }) %>
diff --git a/apps/dashboard/test/models/script_test.rb b/apps/dashboard/test/models/script_test.rb index 7ba40e923b..087ff02bc2 100644 --- a/apps/dashboard/test/models/script_test.rb +++ b/apps/dashboard/test/models/script_test.rb @@ -3,6 +3,16 @@ require 'test_helper' class ScriptTest < ActiveSupport::TestCase + test 'supported field postfix' do + target = Script.new({ project_dir: '/path/project', id: 1234, title: 'Test Script' }) + refute target.send('attribute_parameter?', nil) + refute target.send('attribute_parameter?', '') + refute target.send('attribute_parameter?', 'account_notsupported') + assert target.send('attribute_parameter?', 'account_min') + assert target.send('attribute_parameter?', 'account_max') + assert target.send('attribute_parameter?', 'account_exclude') + assert target.send('attribute_parameter?', 'account_fixed') + end test 'creates script' do Dir.mktmpdir do |tmp| projects_path = Pathname.new(tmp)