Made the propper files and functionality for the text truncation to function

This commit is contained in:
Zanz2 2017-11-17 02:43:32 +01:00
parent b2e1623cf1
commit fa25a6785b
7 changed files with 110 additions and 42 deletions

View file

@ -643,14 +643,25 @@ class ProtocolsController < ApplicationController
def protocolsio_import_save
@json_object = JSON.parse(params['json_object'])
@db_json = {}
@db_json['name'] = sanitize_input(params['protocol']['name'])
@toolong = false
@db_json['name'] = pio_eval_title_len(
sanitize_input(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'] = sanitize_input(params['protocol']['authors'])
@db_json['created_at'] = sanitize_input(params['protocol']['created_at'])
@db_json['updated_at'] = sanitize_input(params['protocol']['last_modified'])
@db_json['authors'] = pio_eval_title_len(
sanitize_input(params['protocol']['authors'])
)
@db_json['created_at'] = pio_eval_title_len(
sanitize_input(params['protocol']['created_at'])
)
@db_json['updated_at'] = pio_eval_title_len(
sanitize_input(params['protocol']['last_modified'])
)
@db_json['steps'] = {}
@db_json['steps'] = protocols_io_fill_step(@json_object, @db_json['steps'])
@db_json['steps'] = protocols_io_fill_step(
@json_object, @db_json['steps']
)
protocol = nil
respond_to do |format|
transaction_error = false
@ -682,7 +693,7 @@ class ProtocolsController < ApplicationController
@protocolsio_general_error = false
format.json do
render json:
{ name: p_name, new_name: protocol.name, status: :ok },
{ name: @db_json['name'], new_name: @db_json['name'], status: :ok },
status: :ok
end
end
@ -946,7 +957,7 @@ class ProtocolsController < ApplicationController
# pio_stp_x means protocols io step (id of component) parser
def pio_stp_1(iterating_key) # protocols io description parser
br = '<br>'
append = br + sanitize_input(iterating_key) + br if iterating_key.present?
append = br + pio_eval_s_desc_len(sanitize_input(iterating_key)) + br if iterating_key.present?
if iterating_key.blank?
append = t('protocols.protocols_io_import.comp_append.missing_desc')
end
@ -954,7 +965,7 @@ class ProtocolsController < ApplicationController
end
def pio_stp_6(iterating_key) # protocols io section(title) parser
return sanitize_input(iterating_key) if iterating_key.present?
return pio_eval_title_len(sanitize_input(iterating_key)) if iterating_key.present?
t('protocols.protocols_io_import.comp_append.missing_step')
end
@ -962,7 +973,7 @@ class ProtocolsController < ApplicationController
if iterating_key.present?
append =
t('protocols.protocols_io_import.comp_append.expected_result') +
sanitize_input(iterating_key) + '<br>'
pio_eval_s_safe_expctres_len(sanitize_input(iterating_key)) + '<br>'
return append
end
''
@ -1012,9 +1023,9 @@ class ProtocolsController < ApplicationController
iterating_key['os_name'] &&
iterating_key['os_version']
append = t('protocols.protocols_io_import.comp_append.command.title') +
sanitize_input(iterating_key['name']) +
pio_eval_s_cmd_len(sanitize_input(iterating_key['name'])) +
t('protocols.protocols_io_import.comp_append.command.desc') +
sanitize_input(iterating_key['description']) +
pio_eval_s_cmd_desc_len(sanitize_input(iterating_key['description'])) +
t('protocols.protocols_io_import.comp_append.command.os') +
sanitize_input(iterating_key['os_name']) +
' , ' + iterating_key['os_version']
@ -1050,7 +1061,7 @@ class ProtocolsController < ApplicationController
t(
'protocols.protocols_io_import.comp_append.safety_infor.title'
) +
sanitize_input(iterating_key['body']) +
pio_eval_s_safe_expctres_len(sanitize_input(iterating_key['body'])) +
t('protocols.protocols_io_import.comp_append.general_link') +
sanitize_input(iterating_key['link'])
return append
@ -1066,7 +1077,9 @@ class ProtocolsController < ApplicationController
description_string =
if json_hash['description'].present?
'<strong>' + t('protocols.protocols_io_import.preview.prot_desc') +
'</strong>' + sanitize_input(json_hash['description'].html_safe)
'</strong>' + pio_eval_p_desc_len(
sanitize_input(json_hash['description']).html_safe
)
else
'<strong>' + t('protocols.protocols_io_import.preview.prot_desc') +
'</strong>' + t('protocols.protocols_io_import.comp_append.missing_desc')
@ -1076,26 +1089,25 @@ class ProtocolsController < ApplicationController
if e == 'created_on' && json_hash[e].present?
new_e = '<strong>' + e.humanize + '</strong>'
description_string +=
new_e.to_s + ': ' +
sanitize_input(params['protocol']['created_at'].to_s) + '<br>'
new_e.to_s + ': ' + pio_eval_p_pbldate_len(
sanitize_input(params['protocol']['created_at'].to_s) + '<br>'
)
elsif e == 'tags' && json_hash[e].any? && json_hash[e] != ''
new_e = '<strong>' + e.humanize + '</strong>'
description_string +=
new_e.to_s + ': '
tags_length_checker = ''
json_hash[e].each do |tag|
description_string +=
tags_length_checker +=
sanitize_input(tag['tag_name']) + ' , '
end
description_string += pio_eval_p_keywords_tags_len(tags_length_checker)
description_string += '<br>'
# Since protocols description field doesnt show html,i just remove it
# because its even messier (using Sanitize)
# what this does is basically appends "FIELD NAME: "+" FIELD VALUE"
# to description for various fields
elsif json_hash[e].present?
new_e = '<strong>' + e.humanize + '</strong>'
description_string +=
new_e.to_s + ': ' +
sanitize_input(json_hash[e].html_safe) + '<br>'
eval_prot_desc(sanitize_input(json_hash[e]).html_safe, e) + '<br>'
end
end
description_string

View file

@ -42,10 +42,36 @@ module ProtocolsIoHelper
string_without_tables
end
def eval_prot_desc(text, attribute_name)
case attribute_name
when 'before_start'
pio_eval_p_bfrandsafe_len(text)
when 'warning'
pio_eval_p_bfrandsafe_len(text)
when 'guidelines'
pio_eval_p_guid_len(text)
when 'publish_date'
pio_eval_p_pbldate_len(text)
when 'vendor_name'
pio_eval_p_misc_vnd_link_len(text)
when 'vendor_link'
pio_eval_p_misc_vnd_link_len(text)
when 'keywords'
pio_eval_p_keywords_tags_len(text)
when 'link'
pio_eval_p_misc_vnd_link_len(text)
else
''
end
# ( before_start warning guidelines publish_date
# vendor_name vendor_link keywords link )
end
def pio_eval_title_len(tekst)
tekst += ' ' if tekst.length <= 1
if tekst.length > 250
tekst = tekst[0..195] + t('protocols.protocols_io_import.too_long')
@toolong = true
end
tekst
end
@ -59,6 +85,7 @@ module ProtocolsIoHelper
def pio_eval_p_desc_len(tekst)
if tekst.length - 4000 > 10000
tekst = tekst[0..13940] + t('protocols.protocols_io_import.too_long')
@toolong = true
end
tekst
end
@ -66,6 +93,7 @@ module ProtocolsIoHelper
def pio_eval_p_guid_len(tekst)
if tekst.length - 2000 > 10000
tekst = tekst[0..11940] + t('protocols.protocols_io_import.too_long')
@toolong = true
end
tekst
end
@ -73,6 +101,7 @@ module ProtocolsIoHelper
def pio_eval_p_bfrandsafe_len(tekst)
if tekst.length - 2000 > 7000
tekst = tekst[0..8940] + t('protocols.protocols_io_import.too_long')
@toolong = true
end
tekst
end
@ -82,18 +111,23 @@ module ProtocolsIoHelper
def pio_eval_p_misc_vnd_link_len(tekst)
if tekst.length > 250
tekst = tekst[0..190] + t('protocols.protocols_io_import.too_long')
@toolong = true
end
tekst
end
def pio_eval_p_pbldate_len(tekst)
tekst = tekst[0..120] if tekst.length > 120
if tekst.length > 120
tekst = tekst[0..120]
@toolong = true
end
tekst
end
def pio_eval_p_keywords_tags_len(tekst)
if tekst.length > 1000
tekst = tekst[0..940] + t('protocols.protocols_io_import.too_long')
@toolong = true
end
tekst
end
@ -101,6 +135,7 @@ module ProtocolsIoHelper
def pio_eval_s_desc_len(tekst)
if tekst.length - 4000 > 20000
tekst = tekst[0..23940] + t('protocols.protocols_io_import.too_long')
@toolong = true
end
tekst
end
@ -108,6 +143,7 @@ module ProtocolsIoHelper
def pio_eval_s_cmd_desc_len(tekst)
if tekst.length - 1000 > 2500
tekst = tekst[0..3440] + t('protocols.protocols_io_import.too_long')
@toolong = true
end
tekst
end
@ -115,6 +151,7 @@ module ProtocolsIoHelper
def pio_eval_s_cmd_len(tekst)
if tekst.length - 1000 > 3000
tekst = tekst[0..3940] + t('protocols.protocols_io_import.too_long')
@toolong = true
end
tekst
end
@ -122,8 +159,8 @@ module ProtocolsIoHelper
def pio_eval_s_safe_expctres_len(tekst)
if tekst.length - 2000 > 5000
tekst = tekst[0..6940] + t('protocols.protocols_io_import.too_long')
@toolong = true
end
tekst
end
end

View file

@ -17,7 +17,6 @@ module ProtocolsImporter
if protocol.invalid? then
rename_record(protocol, :name)
end
# Okay, now save the protocol
protocol.save!

View file

@ -21,43 +21,52 @@
<% if json_object['description'].present? %>
<% prot_info_string += (json_object['description']) %>
<strong><%= t('protocols.protocols_io_import.preview.prot_desc') %></strong>
<%= pio_eval_p_desc_len(sanitize_input(string_html_table_remove(json_object['description'])).html_safe) %><br>
<% description = pio_eval_p_desc_len(sanitize_input(string_html_table_remove(json_object['description']))) %>
<%= description.html_safe %><br>
<% end %>
<% if json_object['before_start'].present? %>
<% prot_info_string += (json_object['before_start']) %>
<strong><%= t('protocols.protocols_io_import.preview.b_s_p') %></strong>
<%= pio_eval_p_bfrandsafe_len(sanitize_input(string_html_table_remove(json_object['before_start'])).html_safe) %><br>
<% before_start = pio_eval_p_bfrandsafe_len(sanitize_input(string_html_table_remove(json_object['before_start']))) %>
<%= before_start.html_safe %><br>
<% end %>
<% if json_object['warning'].present? %>
<% prot_info_string += (json_object['warning']) %>
<strong><%= t('protocols.protocols_io_import.preview.warn') %></strong>
<%= pio_eval_p_bfrandsafe_len(sanitize_input(string_html_table_remove(json_object['warning'])).html_safe) %><br>
<% sft_warning = pio_eval_p_bfrandsafe_len(sanitize_input(string_html_table_remove(json_object['warning']))) %>
<%= sft_warning.html_safe%><br>
<% end %>
<% if json_object['guidelines'].present? %>
<% prot_info_string += (json_object['guidelines']) %>
<strong><%= t('protocols.protocols_io_import.preview.guideln') %></strong>
<%= pio_eval_p_guid_len(sanitize_input(string_html_table_remove(json_object['guidelines'])).html_safe) %><br>
<% guidelines = pio_eval_p_guid_len(sanitize_input(string_html_table_remove(json_object['guidelines']))) %>
<%= guidelines.html_safe %><br>
<% end %>
<% if json_object['manuscript_citation'].present? %>
<% prot_info_string += (json_object['manuscript_citation']) %>
<strong><%= t('protocols.protocols_io_import.preview.manu_cit') %></strong>
<%= sanitize_input(string_html_table_remove(json_object['manuscript_citation']).html_safe) %><br>
<% manu_cit = sanitize_input(string_html_table_remove(json_object['manuscript_citation'])) %>
<%= manu_cit.html_safe %><br>
<% end %>
<% if json_object['publish_date'].present? %>
<strong><%= t('protocols.protocols_io_import.preview.pbl_date') %></strong>
<%= pio_eval_p_pbldate_len(sanitize_input(json_object['publish_date'])) %><br>
<% pbl_date = pio_eval_p_pbldate_len(sanitize_input(json_object['publish_date'])) %>
<%= pbl_date.html_safe %><br>
<% end %>
<% if json_object['vendor_name'].present? %>
<strong><%= t('protocols.protocols_io_import.preview.vnd_name') %></strong>
<%= pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['vendor_name'])) %><br>
<% vnd_name = pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['vendor_name'])) %>
<%= vnd_name.html_safe %><br>
<% end %>
<% if json_object['vendor_link'].present? %>
<strong><%= t('protocols.protocols_io_import.preview.vnd_link') %></strong>
<%= pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['vendor_link']).html_safe) %><br>
<% vnd_link = pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['vendor_link'])) %>
<%= vnd_link.html_safe %><br>
<% end %>
<% if json_object['keywords'].present? %>
<strong><%= t('protocols.protocols_io_import.preview.key_wrd') %></strong>
<%= pio_eval_p_keywords_tags_len(sanitize_input(json_object['keywords'])) %><br>
<% keywords = pio_eval_p_keywords_tags_len(sanitize_input(json_object['keywords'])) %>
<%= keywords.html_safe %><br>
<% end %>
<% if json_object['tags'].present? %>
<strong><%= t('protocols.protocols_io_import.preview.tags') %></strong>
@ -65,11 +74,13 @@
<% json_object['tags'].each do |tag| %>
<% string_of_tags += tag['tag_name']+' , ' %><br>
<% end %>
<%= pio_eval_p_keywords_tags_len(sanitize_input(string_of_tags))%>
<% tags = pio_eval_p_keywords_tags_len(sanitize_input(string_of_tags))%>
<%= tags.html_safe %>
<% end %>
<% if json_object['link'].present? %>
<strong><%= t('protocols.protocols_io_import.preview.p_link') %></strong>
<%= pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['link']).html_safe) %><br>
<% link = pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['link']).html_safe) %>
<%= link.html_safe %><br>
<% end %>
<% tables, garbage = protocolsio_string_to_table_element(prot_info_string) %>
<% if tables.present? %>

View file

@ -41,11 +41,13 @@
when '1' %>
<% step_info_string += (key['data']) %>
<br><strong><%= t('protocols.protocols_io_import.preview.strng_s_desc') %></strong>
<%=pio_eval_s_desc_len(sanitize_input(string_html_table_remove(key['data'])).html_safe)%><br>
<% s_desc = pio_eval_s_desc_len(sanitize_input(string_html_table_remove(key['data'])))%>
<%= s_desc.html_safe %><br>
<% when '17' %>
<% step_info_string += (key['data']) %>
<br><strong><%= t('protocols.protocols_io_import.preview.s_exp_res') %></strong>
<%=pio_eval_s_safe_expctres_len(sanitize_input(string_html_table_remove(key['data'])).html_safe)%>
<% s_exp_res = pio_eval_s_safe_expctres_len(sanitize_input(string_html_table_remove(key['data'])))%>
<%= s_exp_res.html_safe %>
<br>
<% end %>
<% elsif key && whitelist_complex.include?(key['component_type_id']) %>
@ -77,9 +79,11 @@
<% step_info_string += (key['source_data']['description']) %>
<br>
<strong><%= key['name']+': ' %></strong>
<%= pio_eval_s_cmd_len(sanitize_input(key['source_data']['name']).html_safe) %>
<% s_cmd = pio_eval_s_cmd_len(sanitize_input(key['source_data']['name'])) %>
<%= s_cmd.html_safe %>
<br><%= t('protocols.protocols_io_import.preview.s_desc') %>
<%= pio_eval_s_cmd_desc_len(sanitize_input(string_html_table_remove(key['source_data']['description'])).html_safe) %>
<% s_cmd_desc = pio_eval_s_cmd_desc_len(sanitize_input(string_html_table_remove(key['source_data']['description']))) %>
<% s_cmd_desc.html_safe %>
<br><%= t('protocols.protocols_io_import.preview.os') %>
<%= sanitize_input(key['source_data']['os_name'].html_safe)+
' , '+sanitize_input(key['source_data']['os_version'].html_safe) %>
@ -97,7 +101,8 @@
<% step_info_string += key['source_data']['body'] %>
<br>
<strong><%= key['name']+': ' %></strong>
<%= pio_eval_s_safe_expctres_len(sanitize_input(string_html_table_remove(key['source_data']['body'])).html_safe) %>
<% s_safety = pio_eval_s_safe_expctres_len(sanitize_input(string_html_table_remove(key['source_data']['body']))) %>
<%= s_safety.html_safe %>
<br><%= t('protocols.protocols_io_import.preview.s_link') %>
<%= sanitize_input(key['source_data']['link'].html_safe) %>
<% when '20'%>

View file

@ -1,7 +1,10 @@
<% if @protocolsio_general_error %>
$('#modal-import-json-protocol-preview').modal('hide');
$('#modal-import-json-protocol-preview').modal('hide');
alert(' <%= t('my_modules.protocols.load_from_file_protocol_general_error',
max: Constants::NAME_MAX_LENGTH, min: Constants::NAME_MIN_LENGTH) %>');
<% elsif @toolong%>
$('#modal-import-json-protocol-preview').modal('hide');
HelperModule.flashAlertMsg(' <%= t('protocols.index.import_results.message_warn_truncated')%>', 'success');
<% else %>
$('#modal-import-json-protocol-preview').modal('hide');
$('#protocols_io_form').trigger("reset");

View file

@ -1385,7 +1385,7 @@ en:
manager: "Protocol management"
edit: "Edit protocol"
protocols_io_import:
too_long: "... Your text is too long. Please shorten it to 300 words."
too_long: "... Your text is too long. Please shorten it."
import_description_notice: "The protocols description is listed below under \"Protocol info\"."
preview:
prot_desc: "Protocol Description: "
@ -1565,6 +1565,7 @@ en:
message_failed: "Failed to import %{nr} protocol/s."
message_ok: "Successfully imported %{nr} protocol/s."
message_ok_pio: "Successfully imported protocol from protocols.io file."
message_warn_truncated: "Some fields have been shortened due to being too long."
row_success: "Imported"
row_renamed: "Imported & renamed"
row_failed: "Failed"