From fcbd956d6e2e19c06eebff1d66e2b28545dd52a7 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Sun, 3 Dec 2017 01:17:49 +0100 Subject: [PATCH] Finished code beautification --- app/helpers/protocols_io_helper.rb | 198 +++++++----------- .../_import_json_protocol_p_desc.html.erb | 95 +++------ config/locales/en.yml | 44 ++-- 3 files changed, 135 insertions(+), 202 deletions(-) diff --git a/app/helpers/protocols_io_helper.rb b/app/helpers/protocols_io_helper.rb index 66c1305d3..f30fa28eb 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -147,7 +147,7 @@ module ProtocolsIoHelper end def prepare_for_view(attribute_text, size) - case(size) + case size when 'small' pio_eval_len( sanitize_input(string_html_table_remove(not_null(attribute_text))), @@ -163,20 +163,62 @@ module ProtocolsIoHelper sanitize_input(string_html_table_remove(not_null(attribute_text))), ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_BIG ) - else + when 'small-no-table' + pio_eval_len( + sanitize_input(not_null(attribute_text)), + ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL + ) + when 'medium-no-table' + pio_eval_len( + sanitize_input(not_null(attribute_text)), + ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_MEDIUM + ) + when 'big-no-table' + pio_eval_len( + sanitize_input(not_null(attribute_text)), + ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_BIG + ) end end + def fill_attributes(attribute_name, attribute_text, step_component_key) + output_string = '' + trans_string = '' + case step_component_key + when '8' + trans_string = 'protocols.protocols_io_import.comp_append.soft_packg.' + when '9' + trans_string = 'protocols.protocols_io_import.comp_append.dataset.' + when '15' + trans_string = 'protocols.protocols_io_import.comp_append.command.' + when '18' + trans_string = 'protocols.protocols_io_import.comp_append.sub_protocol.' + when '19' + trans_string = 'protocols.protocols_io_import.comp_append.safety_infor.' + end + trans_string += + if attribute_name != 'os_name' && attribute_name != 'os_version' + attribute_name + else + 'os' + end + output_string += + if attribute_name != 'os_version' + t(trans_string) + else + ' , ' + end + output_string += prepare_for_view(attribute_text, 'small-no-table') + output_string + end + # pio_stp_x means protocols io step (id of component) parser def pio_stp_1(iterating_key) # protocols io description parser br = '
' append = if iterating_key.present? br + - pio_eval_len( - sanitize_input(iterating_key), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + + prepare_for_view(iterating_key, 'small-no-table') + br else t('protocols.protocols_io_import.comp_append.missing_desc') @@ -193,10 +235,7 @@ module ProtocolsIoHelper if iterating_key.present? append = t('protocols.protocols_io_import.comp_append.expected_result') + - pio_eval_len( - sanitize_input(iterating_key), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + + prepare_for_view(iterating_key, 'small-no-table') + '
' return append end @@ -211,40 +250,13 @@ module ProtocolsIoHelper iterating_key['repository'] && iterating_key['os_name'] && iterating_key['os_version'] - append = t('protocols.protocols_io_import.comp_append.soft_packg.title') + - pio_eval_len( - sanitize_input(iterating_key['name']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + - t('protocols.protocols_io_import.comp_append.soft_packg.dev') + - pio_eval_len( - sanitize_input(iterating_key['developer']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + - t('protocols.protocols_io_import.comp_append.soft_packg.vers') + - pio_eval_len( - sanitize_input(iterating_key['version']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + - t('protocols.protocols_io_import.comp_append.general_link') + - pio_eval_len( - sanitize_input(iterating_key['link']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + - t('protocols.protocols_io_import.comp_append.soft_packg.repo') + - pio_eval_len( - sanitize_input(iterating_key['repository']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + - t('protocols.protocols_io_import.comp_append.soft_packg.os') + - pio_eval_len( - sanitize_input(iterating_key['os_name']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + ' , ' + - pio_eval_len( - sanitize_input(iterating_key['os_version']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + append = fill_attributes('name', iterating_key['name'], '8') + + fill_attributes('developer', iterating_key['developer'], '8') + + fill_attributes('version', iterating_key['version'], '8') + + fill_attributes('link', iterating_key['link'], '8') + + fill_attributes('repository', iterating_key['repository'], '8') + + fill_attributes('os_name', iterating_key['os_name'], '8') + + fill_attributes('os_version', iterating_key['os_version'], '8') return append end '' @@ -253,16 +265,8 @@ module ProtocolsIoHelper def pio_stp_9(iterating_key) # protocols io dataset parser if iterating_key['name'].present? && iterating_key['link'] - append = t('protocols.protocols_io_import.comp_append.dataset.title') + - pio_eval_len( - sanitize_input(iterating_key['name']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + - t('protocols.protocols_io_import.comp_append.general_link') + - pio_eval_len( - sanitize_input(iterating_key['link']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + append = fill_attributes('name', iterating_key['name'], '9') + + fill_attributes('link', iterating_key['link'], '9') return append end '' @@ -273,26 +277,10 @@ module ProtocolsIoHelper iterating_key['description'] && iterating_key['os_name'] && iterating_key['os_version'] - append = t('protocols.protocols_io_import.comp_append.command.title') + - pio_eval_len( - sanitize_input(iterating_key['name']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + - t('protocols.protocols_io_import.comp_append.command.desc') + - pio_eval_len( - sanitize_input(iterating_key['description']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + - t('protocols.protocols_io_import.comp_append.command.os') + - pio_eval_len( - sanitize_input(iterating_key['os_name']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + - ' , ' + - pio_eval_len( - sanitize_input(iterating_key['os_version']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + append = fill_attributes('name', iterating_key['name'], '15') + + fill_attributes('description', iterating_key['description'], '15') + + fill_attributes('os_name', iterating_key['os_name'], '15') + + fill_attributes('os_version', iterating_key['os_version'], '15') return append end '' @@ -302,26 +290,9 @@ module ProtocolsIoHelper if iterating_key['protocol_name'].present? && iterating_key['full_name'] && iterating_key['link'] - append = - t( - 'protocols.protocols_io_import.comp_append.sub_protocol.title' - ) + - pio_eval_len( - sanitize_input(iterating_key['protocol_name']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + - t( - 'protocols.protocols_io_import.comp_append.sub_protocol.author' - ) + - pio_eval_len( - sanitize_input(iterating_key['full_name']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + - t('protocols.protocols_io_import.comp_append.general_link') + - pio_eval_len( - sanitize_input(iterating_key['link']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + append = fill_attributes('protocol_name', iterating_key['protocol_name'], '18') + + fill_attributes('full_name', iterating_key['full_name'], '18') + + fill_attributes('link', iterating_key['link'], '18') return append end '' @@ -330,19 +301,8 @@ module ProtocolsIoHelper def pio_stp_19(iterating_key) # protocols io safety information parser if iterating_key['body'].present? && iterating_key['link'] - append = - t( - 'protocols.protocols_io_import.comp_append.safety_infor.title' - ) + - pio_eval_len( - sanitize_input(iterating_key['body']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + - t('protocols.protocols_io_import.comp_append.general_link') + - pio_eval_len( - sanitize_input(iterating_key['link']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + append = fill_attributes('body', iterating_key['body'], '19') + + fill_attributes('link', iterating_key['link'], '19') return append end '' @@ -355,13 +315,11 @@ module ProtocolsIoHelper ] description_string = if json_hash['description'].present? - '' + t('protocols.protocols_io_import.preview.prot_desc') + - '' + pio_eval_len( - sanitize_input(json_hash['description']), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_MEDIUM - ).html_safe + '' + t('protocols.protocols_io_import.preview.description') + + '' + + prepare_for_view(json_hash['description'], 'medium-no-table').html_safe else - '' + t('protocols.protocols_io_import.preview.prot_desc') + + '' + t('protocols.protocols_io_import.preview.description') + '' + t('protocols.protocols_io_import.comp_append.missing_desc') end description_string += '
' @@ -369,10 +327,11 @@ module ProtocolsIoHelper if e == 'created_on' && json_hash[e].present? new_e = '' + e.humanize + '' description_string += - new_e.to_s + ': ' + pio_eval_len( - sanitize_input(params['protocol']['created_at'].to_s), - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL - ) + '
' + new_e.to_s + ': ' + + prepare_for_view( + params['protocol']['created_at'].to_s, 'small-no-table' + ) + + + '
' elsif e == 'tags' && json_hash[e].any? && json_hash[e] != '' new_e = '' + e.humanize + '' description_string += @@ -382,9 +341,8 @@ module ProtocolsIoHelper tags_length_checker += sanitize_input(tag['tag_name']) + ' , ' end - description_string += pio_eval_len( - tags_length_checker, - ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_MEDIUM + description_string += prepare_for_view( + tags_length_checker, 'medium-no-table' ) description_string += '
' elsif json_hash[e].present? diff --git a/app/views/protocols/import_export/_import_json_protocol_p_desc.html.erb b/app/views/protocols/import_export/_import_json_protocol_p_desc.html.erb index d448dfa60..e82ce934d 100644 --- a/app/views/protocols/import_export/_import_json_protocol_p_desc.html.erb +++ b/app/views/protocols/import_export/_import_json_protocol_p_desc.html.erb @@ -19,69 +19,40 @@ <% prot_info_string = '' %> <% @remaining = ProtocolsIoHelper::PIO_P_AVAILABLE_LENGTH %> <% protocol_table_elements_array = [] %> - <% if json_object['description'].present? %> - <% prot_info_string += (json_object['description']) %> - <%= t('protocols.protocols_io_import.preview.prot_desc') %> - <%= prepare_for_view(json_object['description'],'medium').html_safe %> -
- <% end %> - <% if json_object['before_start'].present? %> - <% prot_info_string += (json_object['before_start']) %> - <%= t('protocols.protocols_io_import.preview.b_s_p') %> - <%= prepare_for_view(json_object['before_start'],'medium').html_safe %> -
- <% end %> - <% if json_object['warning'].present? %> - <% prot_info_string += (json_object['warning']) %> - <%= t('protocols.protocols_io_import.preview.warn') %> - <%= prepare_for_view(json_object['warning'],'medium').html_safe %> -
- <% end %> - <% if json_object['guidelines'].present? %> - <% prot_info_string += (json_object['guidelines']) %> - <%= t('protocols.protocols_io_import.preview.guideln') %> - <%= prepare_for_view(json_object['guidelines'],'medium').html_safe %> -
- <% end %> - <% if json_object['manuscript_citation'].present? %> - <% prot_info_string += (json_object['manuscript_citation']) %> - <%= t('protocols.protocols_io_import.preview.manu_cit') %> - <%= prepare_for_view(json_object['manuscript_citation'],'medium').html_safe %> -
- <% end %> - <% if json_object['publish_date'].present? %> - <%= t('protocols.protocols_io_import.preview.pbl_date') %> - <%= prepare_for_view(json_object['publish_date'],'small').html_safe %> -
- <% end %> - <% if json_object['vendor_name'].present? %> - <%= t('protocols.protocols_io_import.preview.vnd_name') %> - <%= prepare_for_view(json_object['vendor_name'],'medium').html_safe %> -
- <% end %> - <% if json_object['vendor_link'].present? %> - <%= t('protocols.protocols_io_import.preview.vnd_link') %> - <%= prepare_for_view(json_object['vendor_link'],'big').html_safe %> -
- <% end %> - <% if json_object['keywords'].present? %> - <%= t('protocols.protocols_io_import.preview.key_wrd') %> - <%= prepare_for_view(json_object['keywords'],'medium').html_safe %> -
- <% end %> - <% if json_object['tags'].present? %> - <%= t('protocols.protocols_io_import.preview.tags') %> - <% string_of_tags='' %> - <% json_object['tags'].each do |tag| %> - <% string_of_tags += tag['tag_name']+' , ' %> + <% protocol_iterate_elements = ['description','before_start','warning','guidelines','manuscript_citation', + 'publish_date','vendor_name','vendor_link','keywords','tags','link'] %> + <% add_to_info_string_elements = ['description','before_start','warning','guidelines','manuscript_citation']%> + <% big_elements = ['vendor_link','link'] %> + <% medium_elements = ['description','before_start','warning','guidelines','manuscript_citation', + 'vendor_name','keywords','tags'] %> + <% small_elements = ['publish_date'] %> + + <% protocol_iterate_elements.each do |element|%> + <% if(json_object[element].present?) %> + <% if element == 'tags' %> + <% translation_string = 'protocols.protocols_io_import.preview.' + element %> + <%= t(translation_string) %> + <% string_of_tags='' %> + <% json_object['tags'].each do |tag| %> + <% string_of_tags += tag['tag_name']+' , ' %> + <% end %> + <%= prepare_for_view(string_of_tags,'medium').html_safe %> + <% else %> + <% prot_info_string += not_null(json_object[element]) if add_to_info_string_elements.include? element %> + <% translation_string = 'protocols.protocols_io_import.preview.' + element %> + <%= t(translation_string) %> + <% if big_elements.include? element %> + <%= prepare_for_view(json_object[element],'big').html_safe %> + <% end %> + <% if medium_elements.include? element %> + <%= prepare_for_view(json_object[element],'medium').html_safe %> + <% end %> + <% if small_elements.include? element %> + <%= prepare_for_view(json_object[element],'small').html_safe %> + <% end %> + <% end %> +
<% end %> - <%= prepare_for_view(string_of_tags,'medium').html_safe %> -
- <% end %> - <% if json_object['link'].present? %> - <%= t('protocols.protocols_io_import.preview.p_link') %> - <%= prepare_for_view(json_object['link'],'big').html_safe %> -
<% end %> <% tables, garbage = protocolsio_string_to_table_element(prot_info_string) %> <% if tables.present? %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 85ddffff1..24eff00e1 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1389,11 +1389,11 @@ en: too_long: "... Text is too long so we had to cut it off." import_description_notice: "The protocols description is listed below under \"Protocol info\"." preview: - prot_desc: "Protocol Description: " - b_s_p: "Before starting protocol information:" - warn: "Protocol warning:" - guideln: "Guidelines:" - p_link: "Link:" + description: "Protocol Description: " + before_start: "Before starting protocol information:" + warning: "Protocol warning:" + guidelines: "Guidelines:" + link: "Link:" s_nobr_link: "Link:" s_link: "Link:" s_desc: "Description:" @@ -1405,11 +1405,11 @@ en: os: "OS name , OS version:" sub_prot: "This protocol also contains an attached sub-protocol:" auth: "Author:" - manu_cit: "Manuscript citation:" - pbl_date: "Publish date:" - vnd_name: "Vendor name:" - vnd_link: "Vendor link:" - key_wrd: "Keywords:" + manuscript_citation: "Manuscript citation:" + publish_date: "Publish date:" + vendor_name: "Vendor name:" + vendor_link: "Vendor link:" + keywords: "Keywords:" tags: "Tags:" comp_append: table_moved: "
There was a table here, it was moved to the end of this step. " @@ -1418,22 +1418,26 @@ en: general_link: "
Link: " expected_result: "
Expected result: " soft_packg: - title: "
Software package: " - dev: "
Developer: " - vers: "
Version: " - repo: "
Repository: " + name: "
Software package: " + developer: "
Developer: " + version: "
Version: " + link: "
Link: " + repository: "
Repository: " os: "
OS name , OS version: " dataset: - title: "
Dataset: " + name: "
Dataset: " + link: "
Link: " command: - title: "
Command: " - desc: "
Description: " + name: "
Command: " + description: "
Description: " os: "
OS name , OS version: " sub_protocol: - title: "
This step also contains an attached sub-protocol: " - author: "
Author: " + protocol_name: "
This step also contains an attached sub-protocol: " + full_name: "
Author: " + link: "
Link: " safety_infor: - title: "
Safety information: " + body: "
Safety information: " + link: "
Link: " import_export: load_file_error: "Failed to load the protocol file." import_modal: