Skip to content

Commit

Permalink
Added support for static values to scripts forms
Browse files Browse the repository at this point in the history
  • Loading branch information
abujeda committed Sep 25, 2023
1 parent cf3a5dd commit 3b233d7
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 16 deletions.
8 changes: 4 additions & 4 deletions apps/dashboard/app/controllers/scripts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +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_scripts, :auto_scripts_exclude,
:auto_queues, :auto_queues_exclude, :auto_batch_clusters, :auto_batch_clusters_exclude,
:bc_num_slots, :bc_num_slots_min, :bc_num_slots_max,
:bc_num_hours, :bc_num_hours_min, :bc_num_hours_max
: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

def new
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# Helper for creating new batch connect sessions.
module BatchConnect::SessionContextsHelper
def create_widget(form, attrib, format: nil, hide_excludable: true)
return '' if attrib.fixed?
def create_widget(form, attrib, format: nil, hide_excludable: true, hide_fixed: true)
return '' if hide_fixed && attrib.fixed?
return '' if attrib.hide_when_empty? && attrib.value.blank?

widget = attrib.widget
field_options = attrib.field_options(fmt: format)
all_options = attrib.all_options(fmt: format)

if attrib.fixed?
return render :partial => "batch_connect/session_contexts/fixed", :locals => { form: form, attrib: attrib, field_options: field_options, format: format }
end

case widget
when 'select'
form.select(attrib.id, attrib.select_choices(hide_excludable: hide_excludable), field_options, attrib.html_options)
Expand Down
6 changes: 5 additions & 1 deletion apps/dashboard/app/helpers/scripts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ module ScriptsHelper
def create_editable_widget(form, attrib, format: nil)
widget = attrib.widget
attrib.html_options = { class: 'real-field', autocomplete: 'off' }
locals = { form: form, attrib: attrib, format: format }
# For editable script elements, we want the standard render form even when they are fixed.
# We need to reset the fixed attribute to avoid being render as a read only text field.
fixed_attribute = attrib.fixed?
attrib.opts[:fixed] = false
locals = { form: form, attrib: attrib, format: format, fixed: fixed_attribute }

case widget
when 'number_field'
Expand Down
5 changes: 3 additions & 2 deletions apps/dashboard/app/models/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def respond_to_missing?(method_name, include_private = false)
end

def original_parameter(string)
match = /([\w_]+)_(?:min|max|exclude)/.match(string)
match = /([\w_]+)_(?:min|max|exclude|fixed)/.match(string)
match[1]
end

Expand Down 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?('_min') || name.end_with?('_max') || name.end_with?('_exclude') || name.end_with?('_fixed')
end

# update the 'form' portion of the yaml file given 'params' from the controller.
Expand All @@ -222,6 +222,7 @@ def update_attributes(params)
orig_param = original_parameter(key).to_sym
self[orig_param].min = value if key.end_with?('_min') && !value.to_s.empty?
self[orig_param].max = value if key.end_with?('_max') && !value.to_s.empty?
self[orig_param].opts[:fixed] = true if key.end_with?('_fixed')

if key.end_with?('_exclude')
exclude_list = value.split(',').to_a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%-
all_options = attrib.all_options(fmt: format)
all_options[:readonly] = true
%>

<%= form.text_field(attrib.id, all_options) %>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%-
<%-
field_id = "#{form.object_name}_#{attrib.id}"
min_edit_id = "#{field_id}_min"
min_edit_name = "#{form.object_name}[#{attrib.id}_min]"
Expand All @@ -8,13 +8,24 @@

current_min = attrib.field_options[:min]
current_max = attrib.field_options[:max]

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

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

<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>


<label for="<%= min_edit_id %>">Minimum</label>
<input min="1" step="1"
class="form-control edit-field" type="number" value="<%= current_min %>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
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) %>
<%= 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>

<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 @@ -45,5 +55,5 @@
value="<%= attrib.exclude_select_choices.join(',') %>">
</input>

<%= render(partial: 'scripts/editable_form_fields/edit_field_buttons', locals: { field_id: field_id }) %>
<%= render(partial: 'scripts/editable_form_fields/edit_field_buttons', locals: { field_id: field_id, fixed_name: fixed_name }) %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-%>

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

<div class="d-none edit-group">
</div>
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/views/scripts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<%= bootstrap_form_for(@script, url: submit_project_script_path) do |f| %>
<% @script.smart_attributes.each do |attrib| %>
<%# TODO generate render_format %>
<%= create_widget(f, attrib, format: nil) %>
<%= create_widget(f, attrib, format: nil, hide_fixed: false) %>
<% end %>

<%= f.submit t('dashboard.batch_connect_form_launch'), class: "btn btn-primary btn-block" %>
Expand Down
1 change: 1 addition & 0 deletions apps/dashboard/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ en:
jobs_scripts_deleted: "Script successfully deleted!"
jobs_scripts_submitted: "Successfully submited job %{job_id}."
jobs_scripts_delete_script_confirmation: "Delete all contents of script?"
jobs_scripts_static_field: "Static Value"

settings_updated: "Settings updated"

Expand Down
2 changes: 2 additions & 0 deletions apps/dashboard/test/system/jobs_app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ def add_bc_num_hours(project_id, script_id)
fill_in('script_bc_num_hours', with: 42)
fill_in('script_bc_num_hours_min', with: 20)
fill_in('script_bc_num_hours_max', with: 101)
find('#script_bc_num_hours_fixed').click
find('#save_script_bc_num_hours').click

# correctly saves
Expand Down Expand Up @@ -439,6 +440,7 @@ def add_bc_num_hours(project_id, script_id)
min: 20
step: 1
value: '42'
fixed: true
max: 101
label: Number of hours
help: ''
Expand Down

0 comments on commit 3b233d7

Please sign in to comment.