From 17e8367d19e2c67314f10c356d1cfc7e09459fd5 Mon Sep 17 00:00:00 2001 From: Anthony Truskinger Date: Wed, 21 Sep 2016 12:07:24 +1000 Subject: [PATCH] Updated admin script form to support new field :analysis_action_params was not changeable and had no form presence. Fixed it. --- app/controllers/admin/scripts_controller.rb | 3 ++- app/models/analysis_jobs_item.rb | 2 +- app/models/script.rb | 5 ++++- app/views/admin/scripts/_form.html.haml | 3 ++- app/views/admin/scripts/show.html.haml | 6 +++++ lib/validators/json_validator.rb | 25 +++++++++++++++++++++ 6 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 lib/validators/json_validator.rb diff --git a/app/controllers/admin/scripts_controller.rb b/app/controllers/admin/scripts_controller.rb index 28dd8db1..44d772ce 100644 --- a/app/controllers/admin/scripts_controller.rb +++ b/app/controllers/admin/scripts_controller.rb @@ -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 diff --git a/app/models/analysis_jobs_item.rb b/app/models/analysis_jobs_item.rb index 9d3301b1..0a3d2657 100644 --- a/app/models/analysis_jobs_item.rb +++ b/app/models/analysis_jobs_item.rb @@ -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({ diff --git a/app/models/script.rb b/app/models/script.rb index e02342a5..aa26b4f5 100644 --- a/app/models/script.rb +++ b/app/models/script.rb @@ -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 diff --git a/app/views/admin/scripts/_form.html.haml b/app/views/admin/scripts/_form.html.haml index b6fcccf5..d0d75eef 100644 --- a/app/views/admin/scripts/_form.html.haml +++ b/app/views/admin/scripts/_form.html.haml @@ -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' \ No newline at end of file diff --git a/app/views/admin/scripts/show.html.haml b/app/views/admin/scripts/show.html.haml index 26ce084e..e62e5797 100644 --- a/app/views/admin/scripts/show.html.haml +++ b/app/views/admin/scripts/show.html.haml @@ -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} diff --git a/lib/validators/json_validator.rb b/lib/validators/json_validator.rb new file mode 100644 index 00000000..56e0032c --- /dev/null +++ b/lib/validators/json_validator.rb @@ -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 \ No newline at end of file