Skip to content

Commit

Permalink
Updates script.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
HazelGrant committed Feb 7, 2024
1 parent 96b70a3 commit 2902fad
Showing 1 changed file with 7 additions and 37 deletions.
44 changes: 7 additions & 37 deletions apps/dashboard/app/models/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ def initialize(opts = {})
@created_at = opts[:created_at]
sm_opts = {
form: opts[:form] || [],
attributes: opts[:attributes] || {},
job_environment: opts[:job_environment] || {},
attributes: opts[:attributes] || {}
}

add_required_fields(**sm_opts)
Expand All @@ -82,18 +81,13 @@ def build_smart_attributes(form: [], attributes: {})
end

def to_yaml
attributes = smart_attributes.reject do |attr|
attr.id == "auto_environment_variable"
end.each_with_object({}) do |sm, hash|
attributes = smart_attributes.each_with_object({}) do |sm, hash|
hash[sm.id.to_s] = sm.options_to_serialize
end.deep_stringify_keys

job_environment = smart_attributes.select { |attr| attr.id == "auto_environment_variable" }.first.try(:opts) || {}

hsh = { 'title' => title, 'created_at' => created_at }
hsh.merge!({ 'form' => smart_attributes.map { |sm| sm.id.to_s unless sm.id.to_s == "auto_environment_variable" }.compact })
hsh.merge!({ 'form' => smart_attributes.map { |sm| sm.id.to_s }.compact })
hsh.merge!({ 'attributes' => attributes })
hsh.merge!({ 'job_environment' => job_environment.deep_stringify_keys }) unless job_environment.empty?

hsh.to_yaml
end
Expand Down Expand Up @@ -126,12 +120,8 @@ def respond_to_missing?(method_name, include_private = false)
end

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

# Find attribute in list using the id of the attribute
Expand Down Expand Up @@ -178,7 +168,6 @@ def update(params)
# the individual smart attributes
update_form(params)
update_attributes(params)
update_job_environment(params)
end

def submit(options)
Expand Down Expand Up @@ -221,8 +210,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)
['min', 'max', 'exclude', 'fixed'].any? { |postfix| name && name.end_with?("_#{postfix}") } ||
(name && name.match?('auto_environment_variable'))
['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 Expand Up @@ -252,24 +240,6 @@ def update_attributes(params)
end
end

def update_job_environment(params)
env_var_names = params.select { |k, _| k.match?('auto_environment_variable_name_') }
return if env_var_names.empty?

env_var_values = params.select do |param|
param.match?(/\Aauto_environment_variable_(.*)_value\Z/)
end

job_environment_hash = {}

env_var_values.each do |key, value|
variable_name = key.match(/\Aauto_environment_variable_(.*)_value\Z/)[1]
job_environment_hash[variable_name.to_s] = value.to_s
end

self[:auto_environment_variable] = SmartAttributes::AttributeFactory.build('auto_environment_variable', job_environment_hash)
end

def default_attributes(smart_attr_id)
case smart_attr_id
when 'auto_scripts'
Expand Down Expand Up @@ -342,7 +312,7 @@ def adapter(cluster_id)
OodAppkit.clusters[cluster_id] || raise(ClusterNotFound, "Job specifies nonexistent '#{cluster_id}' cluster id.")
end

def add_required_fields(form: [], attributes: {}, job_environment: {})
def add_required_fields(form: [], attributes: {})
add_cluster_to_form(form: form, attributes: attributes)
add_script_to_form(form: form, attributes: attributes)
end
Expand Down

0 comments on commit 2902fad

Please sign in to comment.