Skip to content

Commit

Permalink
Updated admin script form to support new field
Browse files Browse the repository at this point in the history
:analysis_action_params was not changeable and had no form presence.
Fixed it.
  • Loading branch information
atruskie committed Sep 21, 2016
1 parent 5f96b96 commit 17e8367
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/controllers/admin/scripts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def script_params
:name, :description, :analysis_identifier,
:version, :verified,
:executable_command,
:executable_settings, :executable_settings_media_type)
:executable_settings, :executable_settings_media_type,
:analysis_action_params)
end

def get_scripts
Expand Down
2 changes: 1 addition & 1 deletion app/models/analysis_jobs_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def self.create_action_payload(analysis_job, audio_recording)
# Options invariant to the AnalysisJob are stuck in here, like:
# - file_executable
# - copy_paths
payload = analysis_job.script.analysis_action_params.dup.deep_symbolize_keys
payload = (analysis_job.script.analysis_action_params || {}).dup.deep_symbolize_keys

# merge base
payload.merge({
Expand Down
5 changes: 4 additions & 1 deletion app/models/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ class Script < ActiveRecord::Base
validates :creator, existence: true

# attribute validations
validates :name, :analysis_identifier, :executable_command, :executable_settings, :executable_settings_media_type, presence: true, length: {minimum: 2}
validates :name, :analysis_identifier, :executable_command, :executable_settings, :executable_settings_media_type, :analysis_action_params,
presence: true, length: {minimum: 2}
validate :check_version_increase, on: :create



# for the first script in a group, make sure group_id is set to the script's id
after_create :set_group_id

Expand Down
3 changes: 2 additions & 1 deletion app/views/admin/scripts/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
= f.input :name
= f.input :description, input_html: {rows: '3'}
= f.input :analysis_identifier
= f.input :version
= f.input :version, input_html: { min: '0', step: '0.1' }
= f.input :executable_command, input_html: {rows: '3'}
= f.input :executable_settings, input_html: {rows: '3'}
= f.input :executable_settings_media_type
= f.input :analysis_action_params, input_html: {rows: '3'}, as: :text
= f.input :verified, wrapper: :horizontal_boolean
= f.button :submit_cancel, 'Submit', class: 'btn btn-default'
6 changes: 6 additions & 0 deletions app/views/admin/scripts/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@
%pre
= @script.executable_settings

%h3 Default action params
%p
%pre
= @script.analysis_action_params


%h3 All versions of this script
= render partial: 'admin/scripts/scripts_table', locals: {scripts: @all_script_versions}
25 changes: 25 additions & 0 deletions lib/validators/json_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# https://gist.github.com/joost/7ee5fbcc40e377369351

# Put this code in lib/validators/json_validator.rb
# Usage in your model:
# validates :json_attribute, presence: true, json: true
#
# To have a detailed error use something like:
# validates :json_attribute, presence: true, json: {message: :some_i18n_key}
# In your yaml use:
# some_i18n_key: "detailed exception message: %{exception_message}"
class JsonValidator < ActiveModel::EachValidator

def initialize(options)
options.reverse_merge!(:message => :invalid)
super(options)
end

def validate_each(record, attribute, value)
value = value.strip if value.is_a?(String)
ActiveSupport::JSON.decode(value)
rescue MultiJson::LoadError, TypeError => exception
record.errors.add(attribute, options[:message], exception_message: exception.message)
end

end

0 comments on commit 17e8367

Please sign in to comment.