Skip to content

Commit

Permalink
Improved implementation for fixed values in Script forms
Browse files Browse the repository at this point in the history
  • Loading branch information
abujeda committed Sep 25, 2023
1 parent 3b233d7 commit 6e0f2b6
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 20 deletions.
6 changes: 4 additions & 2 deletions apps/dashboard/app/controllers/scripts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions apps/dashboard/app/javascript/packs/script_edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,33 @@ function addInProgressField(event) {
entireDiv.remove();
}

function fixedFieldEnabled(checkbox, dataElement) {
dataElement.disabled = true;
const input = $('<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;

Expand Down Expand Up @@ -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(() => {
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/models/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%-
field_id = "#{form.object_name}_#{attrib.id}"

fixed_id = "#{field_id}_fixed"
fixed_name = "#{form.object_name}[#{attrib.id}_fixed]"
-%>
<div class="list-group col-md-4">
<div class="list-group-item mb-3">
<input type="checkbox" id="<%= fixed_id %>" name="<%= fixed_name %>" value="true" <%= fixed ? 'checked' : '' %> data-select-toggler="fixed" data-select-id="<%= field_id %>">
<label class="form-check-label" for="<%= fixed_id %>"><%= t('dashboard.jobs_scripts_static_field') %></label>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@

<div class="d-none edit-group mb-3">

<div class="list-group col-md-4">
<div class="list-group-item mb-3">
<input type="checkbox" id="<%= fixed_id %>" name="<%= fixed_name %>" value="true" <%= fixed ? 'checked' : '' %>>
<label class="form-check-label" for="<%= fixed_id %>"><%= t('dashboard.jobs_scripts_static_field') %></label>
</div>
</div>

<%= render(partial: 'scripts/editable_form_fields/edit_fixed_field', locals: { form: form, attrib: attrib, fixed: fixed }) %>

<label for="<%= min_edit_id %>">Minimum</label>
<input min="1" step="1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@
field_id = "#{form.object_name}_#{attrib.id}"
exclude_id = "#{field_id}_exclude"
exclude_name = "#{form.object_name}[#{attrib.id}_exclude]"

fixed_id = "#{field_id}_fixed"
fixed_name = "#{form.object_name}[#{attrib.id}_fixed]"
-%>

<div class="editable-form-field">
<%= create_widget(form, attrib, format: format, hide_excludable: false, hide_fixed: false) %>

<div class="d-none edit-group">

<div class="list-group col-md-4">
<div class="list-group-item mb-3">
<input type="checkbox" id="<%= fixed_id %>" name="<%= fixed_name %>" value="true" <%= fixed ? 'checked' : '' %>>
<label class="form-check-label" for="<%= fixed_id %>"><%= t('dashboard.jobs_scripts_static_field') %></label>
</div>
</div>
<%= render(partial: 'scripts/editable_form_fields/edit_fixed_field', locals: { form: form, attrib: attrib, fixed: fixed }) %>

<ol class="list-group text-center col-md-4 mb-3">
<%- attrib.select_choices(hide_excludable: false).each do |select_data| %>
Expand Down Expand Up @@ -55,5 +47,5 @@
value="<%= attrib.exclude_select_choices.join(',') %>">
</input>

<%= 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 }) %>
</div>
10 changes: 10 additions & 0 deletions apps/dashboard/test/models/script_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6e0f2b6

Please sign in to comment.