From 6ebdea5dcef569f23a83ad95f31e5cac5cf54b60 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Fri, 22 Sep 2017 15:12:29 +0200 Subject: [PATCH] Fixed old workaround with inconsistent protocols.io json formatting and made the workaround nicer (now its only one if statement, instead of one whole block of duplicated code) --- app/controllers/protocols_controller.rb | 88 ++------------ ...mport_json_protocol_preview_modal.html.erb | 111 +++--------------- 2 files changed, 25 insertions(+), 174 deletions(-) diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index 442cd23fc..d4452e5cc 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -608,7 +608,7 @@ class ProtocolsController < ApplicationController def protocolsio_import_create @protocolsio_too_big=false file_size=(File.size(params[:json_file].path)) - + if(file_size/1000>Constants::FILE_MAX_SIZE_MB) @protocolsio_too_big=true respond_to do |format| @@ -699,8 +699,12 @@ def protocolsio_import_save step["components"].each do |key,value| element_string=nil - if counter>1 #here i made an if to distinguish the first step from the others, because the first step - #sometimes has weird nesting that requires different handling + if counter<=1 #here i made an if to distinguish the first step from the others, because the first step + #sometimes has index values as keys instead of hashes + if value.class==Hash + key=value + end + end if whitelist_simple.include?(key["component_type_id"]) case key["component_type_id"] @@ -770,85 +774,7 @@ def protocolsio_import_save end end #finished step component iteration - else #it is first step - unless value - value=key #some json files have random empty arrays in beggining of step components - end - if whitelist_simple.include?(value["component_type_id"]) - - case value["component_type_id"] - when "1" - if !value["data"].nil? && value["data"]!="" - - element_string="
"+(value["data"].to_s)+"
" - if(@import_object["steps"][step_pos.to_s]["description"]) - @import_object["steps"][step_pos.to_s]["description"]<" - @import_object["steps"][step_pos.to_s]["description"]<Developer: "+(value["source_data"]["developer"])+"
Version: "+(value["source_data"]["version"])+"
Link: "+(value["source_data"]["link"])+"
Repository: "+(value["source_data"]["repository"])+"
OS name , OS version: "+(value["source_data"]["os_name"])+" , "+(value["source_data"]["os_version"]) - @import_object["steps"][step_pos.to_s]["description"]<Link: "+(value["source_data"]["link"]) - @import_object["steps"][step_pos.to_s]["description"]<Description: "+(value["source_data"]["description"])+"
OS name , OS version: "+(value["source_data"]["os_name"])+" , "+(value["source_data"]["os_version"]) - @import_object["steps"][step_pos.to_s]["description"]<Author: "+(value["source_data"]["full_name"])+"
Link: "+(value["source_data"]["link"]) - @import_object["steps"][step_pos.to_s]["description"]<Link: "+(value["source_data"]["link"]) - @import_object["steps"][step_pos.to_s]["description"]< <% step["components"].each do |key1,value1| #finding section (title of step) %> - <% if counter >1 %> + <% if counter <=1 %> + <% if value1.class==Hash %> + <% key1=value1 %> + <% end %> + <% end %> <% if(key1["component_type_id"]=="6") %> <% if(!key1["data"].nil? && key1["data"]!="") %> <%# byebug %> <% title ||=key1["data"] %> <% end %> <% end %> - <% else %> - <% unless value1 %> - <% value1=key1 %> - <% end %> - <% if(value1["component_type_id"]=="6") %> - <% if(!value1["data"].nil? && value1["data"]!="") %> - <% #byebug %> - <% title ||=value1["data"] %> + <% end %> - <% end %> - <% end %> - <% end %> <% if title.nil? %> <%# byebug %> <% title ="Protocols.io" %> @@ -153,16 +147,19 @@ <% step["components"].each do |key,value| %> - - <% if counter>1 #here i made an if to distinguish the first step from the others, because the first step - #sometimes has weird nesting that makes the below outputs not work + <% if counter<=1 #here i made an if to distinguish the first step from the others, because the first step + #sometimes has index values as keys instead of hashes %> + <% if value.class==Hash %> + <% key=value %> + <% end %> + <% end %> + + <% if whitelist_simple.include?(key["component_type_id"]) && key["data"]!="" && key["data"] %>
<% case key["component_type_id"]%> - <% when "6" %> - - <% when "1" %> + <% when "1" %>
Description: <%=key["data"]%>
<% when "17" %>
Expected result: <%=key["data"]%>
@@ -220,83 +217,11 @@ <% else %> - <% end %> + <% end #case if%> - <% end #notranji if št 1%> - - <% else #if first step %> - - <% unless value %> - <% value=key #protocols io first step json formating is sometimes broken, this is a workaround%> - <% end %> - <% #byebug %> - <% if whitelist_simple.include?(value["component_type_id"]) && value["data"] && value["data"]!="" %> -
- <% case value["component_type_id"]%> - <% when "6" %> - - <% when "1" %> -
Description: <%=value["data"]%>
- <% when "17" %> -
Expected result: <%=value["data"]%>
- <% else %> - <% end %> - - <% elsif value && whitelist_complex.include?(value["component_type_id"]) %> - <% case value["component_type_id"]%> - <% when "8"%> -
- <%= value["name"]+": " %> <%= (value["source_data"]["name"]) %> -
- Developer : <%= (value["source_data"]["developer"]) %> -
- Version : <%= (value["source_data"]["version"]) %> -
- Link : <%= (value["source_data"]["link"]) %> -
- Repository : <%= (value["source_data"]["repository"]) %> -
- OS name , OS version : <%= (value["source_data"]["os_name"])+" , "+(value["source_data"]["os_version"]) %> - - <% when "9"%> -
- <%= value["name"]+": " %> <%= value["source_data"]["name"] %> -
- Link : <%= (value["source_data"]["link"]) %> + <% end #inner if%> - <% when "15"%> -
- <%= value["name"]+": " %> <%= value["source_data"]["name"] %> -
- Description : <%= (value["source_data"]["description"]) %> -
- OS name , OS version : <%= (value["source_data"]["os_name"])+" , "+(value["source_data"]["os_version"]) %> - - <% when "18"%> -
- This protocol also contains an attached sub-protocol: <%= (value["source_data"]["protocol_name"]) %> -
- Author: <%= (value["source_data"]["full_name"]) %> -
- <% if value["source_data"]["link"]&&value["source_data"]["link"]!="" %> - Link: <%= (value["source_data"]["link"]) %> - <% end %> - <% when "19"%> -
- <%= value["name"]+": " %> <%= value["source_data"]["body"] %> -
- Link : <%= (value["source_data"]["link"]) %> - - - <% when "20"%> - - <% else %> - - <% end %> - - <% end #inner whitelist if%> - <% end #first step if %> <% end #step component loop %> @@ -317,7 +242,7 @@ - <%# div od blocka %> + <%# block div%> <% end #step loop%>