From 96253c108947eca6ee353a50f9948eac54d2f6a1 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Tue, 23 Jan 2018 18:40:23 +0100 Subject: [PATCH 1/6] Made some functions to help solve reordering of the protocols io steps --- app/helpers/protocols_io_helper.rb | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/app/helpers/protocols_io_helper.rb b/app/helpers/protocols_io_helper.rb index e4eb5f70a..79d10d910 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -285,6 +285,41 @@ module ProtocolsIoHelper description_string end + def protocols_io_return_first_step_guid(unordered_step_json) + return_step = 'null' + unordered_step_json['steps'].each do |step| + return_step = step unless step['previous_guid'] + end + return_step + end + + def protocols_io_guid_reorder_step_json(unordered_step_json) + base_step = protocols_io_return_first_step_guid(original_json) + step_order = [] + step_counter = 0 + step_order[step_counter] = base_step + step_order[step_counter] += 1 + while !correct_order_guid_check + unordered_step_json['steps'].each do |step| + next unless step['previous_guid'] == base_step['guid'] + step_order[step_counter] = step + step_order[step_counter] += 1 + base_step = step + end + end + end + step_order + end + + def correct_order_guid_check(step_array, unordered_array) + return false if step_array.length != unordered_array.length + step_array.each_with_index do |step, index| + break unless step_array[index + 1] + return false unless step['guid'] == step_array[index + 1]['previous_guid'] + end + true + end + def protocols_io_fill_step(original_json, newj) # newj = new json # (simple to map) id 1= step description, id 6= section (title), @@ -293,6 +328,11 @@ module ProtocolsIoHelper # id 9 = dataset, id 15 = command, id 18 = attached sub protocol # id 19= safety information , # id 20= regents (materials, like scinote samples kind of) + + # REORDER JSON STEP CORRECTLY FUNCTION GOES HERE + + test = protocols_io_return_first_step_guid(original_json) + byebug newj['0'] = {} newj['0']['position'] = 0 newj['0']['name'] = 'Protocol info' From 7fe1c20ebecff0a60d9210659062ab0beb441841 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Wed, 24 Jan 2018 09:31:43 +0100 Subject: [PATCH 2/6] Finished the protocol reorder functions and implemented them --- app/controllers/protocols_controller.rb | 3 +++ app/helpers/protocols_io_helper.rb | 29 +++++++++++++++---------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index ba024c3ec..d3e9ad1b2 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -632,6 +632,9 @@ class ProtocolsController < ApplicationController return 0 # return 0 stops the rest of the controller code from executing end @json_object = JSON.parse(json_file_contents) + + @json_object['steps'] = protocols_io_guid_reorder_step_json(@json_object) + @protocol = Protocol.new respond_to do |format| format.js {} # go to the js.erb file named the same as this controller, diff --git a/app/helpers/protocols_io_helper.rb b/app/helpers/protocols_io_helper.rb index 79d10d910..902cc8e7e 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -293,26 +293,34 @@ module ProtocolsIoHelper return_step end + def protocols_io_count_steps(step_json) + count = 0 + step_json['steps'].each do |step| + count += 1 + end + count + end + def protocols_io_guid_reorder_step_json(unordered_step_json) - base_step = protocols_io_return_first_step_guid(original_json) + base_step = protocols_io_return_first_step_guid(unordered_step_json) + number_of_steps = protocols_io_count_steps(unordered_step_json) step_order = [] step_counter = 0 step_order[step_counter] = base_step - step_order[step_counter] += 1 - while !correct_order_guid_check + step_counter += 1 + while !correct_order_guid_check(step_order,number_of_steps) unordered_step_json['steps'].each do |step| next unless step['previous_guid'] == base_step['guid'] step_order[step_counter] = step - step_order[step_counter] += 1 + step_counter += 1 base_step = step - end - end + end end step_order end - def correct_order_guid_check(step_array, unordered_array) - return false if step_array.length != unordered_array.length + def correct_order_guid_check(step_array, max) + return false if step_array.length != max step_array.each_with_index do |step, index| break unless step_array[index + 1] return false unless step['guid'] == step_array[index + 1]['previous_guid'] @@ -329,10 +337,7 @@ module ProtocolsIoHelper # id 19= safety information , # id 20= regents (materials, like scinote samples kind of) - # REORDER JSON STEP CORRECTLY FUNCTION GOES HERE - - test = protocols_io_return_first_step_guid(original_json) - byebug + original_json['steps'] = protocols_io_guid_reorder_step_json(original_json) newj['0'] = {} newj['0']['position'] = 0 newj['0']['name'] = 'Protocol info' From 346f9feaef0fcbc55714b07eefa5918426592843 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Fri, 26 Jan 2018 09:34:58 +0100 Subject: [PATCH 3/6] Refactored pull request, but also just did a quick fix on a bug where special characters in titles of steps (% & #) got escaped by sanitize helper, now the string gets sanitized first, then the special characters get unescaped by CGI. unescape html --- app/controllers/protocols_controller.rb | 2 +- app/helpers/protocols_io_helper.rb | 40 ++++++------------------- 2 files changed, 10 insertions(+), 32 deletions(-) diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index 368ad5564..77484c362 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -633,7 +633,7 @@ class ProtocolsController < ApplicationController end @json_object = JSON.parse(json_file_contents) - @json_object['steps'] = protocols_io_guid_reorder_step_json(@json_object) + @json_object['steps'] = protocols_io_guid_reorder_step_json(@json_object['steps']) @protocol = Protocol.new respond_to do |format| diff --git a/app/helpers/protocols_io_helper.rb b/app/helpers/protocols_io_helper.rb index 902cc8e7e..8d7b7a4ca 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -199,7 +199,7 @@ module ProtocolsIoHelper end def pio_stp_6(iterating_key) # protocols io section(title) parser - return pio_eval_title_len(sanitize_input(iterating_key)) if iterating_key.present? + return CGI.unescapeHTML(pio_eval_title_len(sanitize_input(iterating_key))) if iterating_key.present? t('protocols.protocols_io_import.comp_append.missing_step') end @@ -285,46 +285,24 @@ module ProtocolsIoHelper description_string end - def protocols_io_return_first_step_guid(unordered_step_json) - return_step = 'null' - unordered_step_json['steps'].each do |step| - return_step = step unless step['previous_guid'] - end - return_step - end - - def protocols_io_count_steps(step_json) - count = 0 - step_json['steps'].each do |step| - count += 1 - end - count - end - def protocols_io_guid_reorder_step_json(unordered_step_json) - base_step = protocols_io_return_first_step_guid(unordered_step_json) - number_of_steps = protocols_io_count_steps(unordered_step_json) + base_step = unordered_step_json.find { |step| step['previous_guid'].nil? } + number_of_steps = unordered_step_json.size step_order = [] step_counter = 0 step_order[step_counter] = base_step step_counter += 1 - while !correct_order_guid_check(step_order,number_of_steps) - unordered_step_json['steps'].each do |step| - next unless step['previous_guid'] == base_step['guid'] - step_order[step_counter] = step - step_counter += 1 - base_step = step - end + until correct_order_guid_check(step_order, number_of_steps) + step_order[step_counter] = + unordered_step_json.find { |step| step['previous_guid'] == base_step['guid'] } + base_step = step_order[step_counter] + step_counter += 1 end step_order end def correct_order_guid_check(step_array, max) return false if step_array.length != max - step_array.each_with_index do |step, index| - break unless step_array[index + 1] - return false unless step['guid'] == step_array[index + 1]['previous_guid'] - end true end @@ -337,7 +315,7 @@ module ProtocolsIoHelper # id 19= safety information , # id 20= regents (materials, like scinote samples kind of) - original_json['steps'] = protocols_io_guid_reorder_step_json(original_json) + original_json['steps'] = protocols_io_guid_reorder_step_json(original_json['steps']) newj['0'] = {} newj['0']['position'] = 0 newj['0']['name'] = 'Protocol info' From 4ed1555ebbc9d9f5bacf36e337e6aa3c01e33a87 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Fri, 26 Jan 2018 09:45:41 +0100 Subject: [PATCH 4/6] small fix, ordering --- app/helpers/protocols_io_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/protocols_io_helper.rb b/app/helpers/protocols_io_helper.rb index 8d7b7a4ca..d15165641 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -199,7 +199,7 @@ module ProtocolsIoHelper end def pio_stp_6(iterating_key) # protocols io section(title) parser - return CGI.unescapeHTML(pio_eval_title_len(sanitize_input(iterating_key))) if iterating_key.present? + return pio_eval_title_len(CGI.unescapeHTML(sanitize_input(iterating_key))) if iterating_key.present? t('protocols.protocols_io_import.comp_append.missing_step') end From 4ee51959eb6ae5eedfb48244d45a381274f01592 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Fri, 26 Jan 2018 09:56:59 +0100 Subject: [PATCH 5/6] fixed hound --- app/controllers/protocols_controller.rb | 4 +++- app/helpers/protocols_io_helper.rb | 12 +++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index 77484c362..6875c71e3 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -633,7 +633,9 @@ class ProtocolsController < ApplicationController end @json_object = JSON.parse(json_file_contents) - @json_object['steps'] = protocols_io_guid_reorder_step_json(@json_object['steps']) + @json_object['steps'] = protocols_io_guid_reorder_step_json( + @json_object['steps'] + ) @protocol = Protocol.new respond_to do |format| diff --git a/app/helpers/protocols_io_helper.rb b/app/helpers/protocols_io_helper.rb index d15165641..af290dc79 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -199,7 +199,9 @@ module ProtocolsIoHelper end def pio_stp_6(iterating_key) # protocols io section(title) parser - return pio_eval_title_len(CGI.unescapeHTML(sanitize_input(iterating_key))) if iterating_key.present? + if iterating_key.present? + return pio_eval_title_len(CGI.unescapeHTML(sanitize_input(iterating_key))) + end t('protocols.protocols_io_import.comp_append.missing_step') end @@ -294,7 +296,9 @@ module ProtocolsIoHelper step_counter += 1 until correct_order_guid_check(step_order, number_of_steps) step_order[step_counter] = - unordered_step_json.find { |step| step['previous_guid'] == base_step['guid'] } + unordered_step_json.find do |step| + step['previous_guid'] == base_step['guid'] + end base_step = step_order[step_counter] step_counter += 1 end @@ -315,7 +319,9 @@ module ProtocolsIoHelper # id 19= safety information , # id 20= regents (materials, like scinote samples kind of) - original_json['steps'] = protocols_io_guid_reorder_step_json(original_json['steps']) + original_json['steps'] = protocols_io_guid_reorder_step_json( + original_json['steps'] + ) newj['0'] = {} newj['0']['position'] = 0 newj['0']['name'] = 'Protocol info' From a48fc168a9a1c1a355af3a927b9392e08e46343e Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Fri, 26 Jan 2018 11:05:19 +0100 Subject: [PATCH 6/6] deleted un needed function --- app/helpers/protocols_io_helper.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/app/helpers/protocols_io_helper.rb b/app/helpers/protocols_io_helper.rb index af290dc79..6490c4e54 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -294,7 +294,7 @@ module ProtocolsIoHelper step_counter = 0 step_order[step_counter] = base_step step_counter += 1 - until correct_order_guid_check(step_order, number_of_steps) + while step_order.length != number_of_steps step_order[step_counter] = unordered_step_json.find do |step| step['previous_guid'] == base_step['guid'] @@ -305,11 +305,6 @@ module ProtocolsIoHelper step_order end - def correct_order_guid_check(step_array, max) - return false if step_array.length != max - true - end - def protocols_io_fill_step(original_json, newj) # newj = new json # (simple to map) id 1= step description, id 6= section (title),