From 95501eb046a163eb6aad274d57122b15058e997c Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Thu, 15 Feb 2018 20:01:36 +0100 Subject: [PATCH 1/6] saving work for later --- app/controllers/protocols_controller.rb | 5 +- app/helpers/protocols_io_helper.rb | 57 ++++++++++++++++++- ...mport_json_protocol_preview_modal.html.erb | 6 +- .../_import_json_protocol_s_desc.html.erb | 4 +- 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index 0f110a58b..f60c19650 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -640,7 +640,8 @@ 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'] = step_hash_not_null(@json_object['steps']) + byebug @json_object['steps'] = protocols_io_guid_reorder_step_json( @json_object['steps'] ) @@ -672,6 +673,8 @@ class ProtocolsController < ApplicationController sanitize_input(params['protocol']['last_modified']) ) @db_json['steps'] = {} + @json_object['steps'] = step_hash_not_null(@json_object['steps']) + byebug @db_json['steps'] = protocols_io_fill_step( @json_object, @db_json['steps'] ) diff --git a/app/helpers/protocols_io_helper.rb b/app/helpers/protocols_io_helper.rb index cafb7c94b..3ea053bd2 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -103,6 +103,8 @@ module ProtocolsIoHelper @toolong = true end text + else + '' end end @@ -129,6 +131,8 @@ module ProtocolsIoHelper @remaining -= text.length - reserved end text + else + '' end end @@ -146,10 +150,59 @@ module ProtocolsIoHelper Nokogiri::HTML::DocumentFragment.parse(text).to_html end + def step_hash_not_null(step_json) + is_null_check = false + if step_json.blank? + is_null_check = true + elsif step_json[0].blank? + is_null_check = true + elsif step_json[0]['components'].blank? + is_null_check = true + elsif step_json[0]['components'][0].blank? + is_null_check = true + elsif step_json[0]['components'][0]['component_type_id'].blank? + is_null_check = true + else + is_null_check = false + end + if is_null_check + return generate_null_step_skeleton + else + step_json + end + end + + # Creates dummy info for when empty steps json is sent, or + # hash structure is modified + def generate_null_step_skeleton + json_string = [ + { + "guid": 0, + "previous_guid": nil, + "components": + [ + { + "component_type_id": '1', + "name": 'Description', + "data": '', + "data_id": nil + }, + { + "component_type_id": '6', + "name": 'Section', + "data": 'Step', + "data_id": '0' + } + ] + } + ] + json_string + end # Images are allowed in: # Step: description, expected result # Protocol description : description before_start warning # guidelines manuscript_citation + def prepare_for_view( attribute_text1, size, table = 'no_table', image_allowed = false ) @@ -314,8 +367,10 @@ module ProtocolsIoHelper end def protocols_io_guid_reorder_step_json(unordered_step_json) - base_step = unordered_step_json.find { |step| step['previous_guid'].nil? } + return '' if unordered_step_json.nil? number_of_steps = unordered_step_json.size + return unordered_step_json if number_of_steps == 1 + base_step = unordered_step_json.find { |step| step['previous_guid'].nil? } step_order = [] step_counter = 0 step_order[step_counter] = base_step diff --git a/app/views/protocols/import_export/_import_json_protocol_preview_modal.html.erb b/app/views/protocols/import_export/_import_json_protocol_preview_modal.html.erb index 4af0850ff..6f67da96a 100644 --- a/app/views/protocols/import_export/_import_json_protocol_preview_modal.html.erb +++ b/app/views/protocols/import_export/_import_json_protocol_preview_modal.html.erb @@ -30,7 +30,7 @@ - <%= f.text_field :name, :value => pio_eval_title_len(sanitize_input(@json_object['protocol_name'])), class: + <%= f.text_field :name, :value => pio_eval_title_len(sanitize_input(not_null(@json_object['protocol_name']))), class: "form-control" %>
@@ -52,14 +52,14 @@
- <% display_created_at=Time.at(@json_object['created_on'].to_i) %> + <% display_created_at=Time.at(not_null(@json_object['created_on']).to_i) %> <%= f.text_field :created_at, :value => display_created_at.to_s, readonly: true, class: "form-control" %>
- <% display_last_modified=Time.at(@json_object['last_modified'].to_i) %> + <% display_last_modified=Time.at(not_null(@json_object['last_modified']).to_i) %> <%= f.text_field :last_modified, :value => display_last_modified.to_s,readonly: true, class: "form-control" %> diff --git a/app/views/protocols/import_export/_import_json_protocol_s_desc.html.erb b/app/views/protocols/import_export/_import_json_protocol_s_desc.html.erb index 2b4eff332..55b93f3a8 100644 --- a/app/views/protocols/import_export/_import_json_protocol_s_desc.html.erb +++ b/app/views/protocols/import_export/_import_json_protocol_s_desc.html.erb @@ -40,13 +40,13 @@
<% case key['component_type_id'] when '1' %> - <% step_info_string += (key['data']) %> + <% step_info_string += not_null(key['data']) %>
<%= t('protocols.protocols_io_import.preview.strng_s_desc') %> <%= prepare_for_view(key['data'],ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL,'table',true).html_safe %>
<% when '17' %> - <% step_info_string += (key['data']) %> + <% step_info_string += not_null(key['data']) %>
<%= t('protocols.protocols_io_import.preview.s_exp_res') %> <%= prepare_for_view(key['data'],ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL,'table',true).html_safe %> From c963c948a1d12e96b88601b39d701b1809689970 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Thu, 15 Feb 2018 23:28:20 +0100 Subject: [PATCH 2/6] Fixed main issue, bad step structure now gets fixed to empty template --- app/controllers/protocols_controller.rb | 2 -- app/helpers/protocols_io_helper.rb | 22 +++++++++++----------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index f60c19650..7656b126f 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -641,7 +641,6 @@ class ProtocolsController < ApplicationController end @json_object = JSON.parse(json_file_contents) @json_object['steps'] = step_hash_not_null(@json_object['steps']) - byebug @json_object['steps'] = protocols_io_guid_reorder_step_json( @json_object['steps'] ) @@ -674,7 +673,6 @@ class ProtocolsController < ApplicationController ) @db_json['steps'] = {} @json_object['steps'] = step_hash_not_null(@json_object['steps']) - byebug @db_json['steps'] = protocols_io_fill_step( @json_object, @db_json['steps'] ) diff --git a/app/helpers/protocols_io_helper.rb b/app/helpers/protocols_io_helper.rb index 3ea053bd2..b82a9906b 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -177,21 +177,21 @@ module ProtocolsIoHelper def generate_null_step_skeleton json_string = [ { - "guid": 0, - "previous_guid": nil, - "components": + 'guid' => '0', + 'previous_guid' => nil, + 'components' => [ { - "component_type_id": '1', - "name": 'Description', - "data": '', - "data_id": nil + 'component_type_id' => '1', + 'name' => 'Description', + 'data' => '', + 'data_id' => nil }, { - "component_type_id": '6', - "name": 'Section', - "data": 'Step', - "data_id": '0' + 'component_type_id' => '6', + 'name' => 'Section', + 'data' => 'Step', + 'data_id' => '0' } ] } From fab9180b7ba81603b75f67b0b09fe24e2a445787 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Thu, 15 Feb 2018 23:40:09 +0100 Subject: [PATCH 3/6] fixed small oversight --- 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 b82a9906b..132377e94 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -367,7 +367,7 @@ module ProtocolsIoHelper end def protocols_io_guid_reorder_step_json(unordered_step_json) - return '' if unordered_step_json.nil? + return '' if unordered_step_json.blank? number_of_steps = unordered_step_json.size return unordered_step_json if number_of_steps == 1 base_step = unordered_step_json.find { |step| step['previous_guid'].nil? } From 29aea1ebd2c80ec70bfe3003bb7f15474512a971 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Wed, 21 Feb 2018 16:17:53 +0100 Subject: [PATCH 4/6] refactored according to PR --- app/controllers/protocols_controller.rb | 27 +++++++++++---------- app/helpers/protocols_io_helper.rb | 32 +++---------------------- 2 files changed, 18 insertions(+), 41 deletions(-) diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index 7656b126f..aabc9f6b5 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -640,10 +640,11 @@ 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'] = step_hash_not_null(@json_object['steps']) - @json_object['steps'] = protocols_io_guid_reorder_step_json( - @json_object['steps'] - ) + unless step_hash_null?(@json_object['steps']) + @json_object['steps'] = protocols_io_guid_reorder_step_json( + @json_object['steps'] + ) + end @protocol = Protocol.new respond_to do |format| @@ -658,24 +659,26 @@ class ProtocolsController < ApplicationController @db_json = {} @toolong = false @db_json['name'] = pio_eval_title_len( - sanitize_input(params['protocol']['name']) + sanitize_input(not_null(params['protocol']['name'])) ) # since scinote only has description field, and protocols.io has many others # ,here i am putting everything important from protocols.io into description @db_json['authors'] = pio_eval_title_len( - sanitize_input(params['protocol']['authors']) + sanitize_input(not_null(params['protocol']['authors'])) ) @db_json['created_at'] = pio_eval_title_len( - sanitize_input(params['protocol']['created_at']) + sanitize_input(not_null(params['protocol']['created_at'])) ) @db_json['updated_at'] = pio_eval_title_len( - sanitize_input(params['protocol']['last_modified']) + sanitize_input(not_null(params['protocol']['last_modified'])) ) @db_json['steps'] = {} - @json_object['steps'] = step_hash_not_null(@json_object['steps']) - @db_json['steps'] = protocols_io_fill_step( - @json_object, @db_json['steps'] - ) + + unless step_hash_null?(@json_object['steps']) + @db_json['steps'] = protocols_io_fill_step( + @json_object, @db_json['steps'] + ) + end protocol = nil respond_to do |format| transaction_error = false diff --git a/app/helpers/protocols_io_helper.rb b/app/helpers/protocols_io_helper.rb index 132377e94..ae2a45499 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -150,7 +150,7 @@ module ProtocolsIoHelper Nokogiri::HTML::DocumentFragment.parse(text).to_html end - def step_hash_not_null(step_json) + def step_hash_null?(step_json) is_null_check = false if step_json.blank? is_null_check = true @@ -166,38 +166,12 @@ module ProtocolsIoHelper is_null_check = false end if is_null_check - return generate_null_step_skeleton + return true else - step_json + return false end end - # Creates dummy info for when empty steps json is sent, or - # hash structure is modified - def generate_null_step_skeleton - json_string = [ - { - 'guid' => '0', - 'previous_guid' => nil, - 'components' => - [ - { - 'component_type_id' => '1', - 'name' => 'Description', - 'data' => '', - 'data_id' => nil - }, - { - 'component_type_id' => '6', - 'name' => 'Section', - 'data' => 'Step', - 'data_id' => '0' - } - ] - } - ] - json_string - end # Images are allowed in: # Step: description, expected result # Protocol description : description before_start warning From 350f12bdbc96d0627674ff024afa1cfd7f6c7fae Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Fri, 2 Mar 2018 22:29:51 +0100 Subject: [PATCH 5/6] using dig method now --- app/helpers/protocols_io_helper.rb | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/app/helpers/protocols_io_helper.rb b/app/helpers/protocols_io_helper.rb index ae2a45499..b1437bec1 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -151,25 +151,8 @@ module ProtocolsIoHelper end def step_hash_null?(step_json) - is_null_check = false - if step_json.blank? - is_null_check = true - elsif step_json[0].blank? - is_null_check = true - elsif step_json[0]['components'].blank? - is_null_check = true - elsif step_json[0]['components'][0].blank? - is_null_check = true - elsif step_json[0]['components'][0]['component_type_id'].blank? - is_null_check = true - else - is_null_check = false - end - if is_null_check - return true - else - return false - end + return true if step_json.dig(0, 'components', 0, 'component_type_id').nil? + false end # Images are allowed in: From e3e7514049b1ee8c55d7b875a89874307e794be4 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Mon, 5 Mar 2018 16:15:33 +0100 Subject: [PATCH 6/6] removed redundant return --- app/helpers/protocols_io_helper.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/helpers/protocols_io_helper.rb b/app/helpers/protocols_io_helper.rb index b1437bec1..9a62c264c 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -151,8 +151,7 @@ module ProtocolsIoHelper end def step_hash_null?(step_json) - return true if step_json.dig(0, 'components', 0, 'component_type_id').nil? - false + step_json.dig(0, 'components', 0, 'component_type_id').nil? end # Images are allowed in: