From 7fe1c20ebecff0a60d9210659062ab0beb441841 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Wed, 24 Jan 2018 09:31:43 +0100 Subject: [PATCH] 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'