scinote-web/app/helpers/reports_helper.rb

103 lines
3.2 KiB
Ruby
Raw Normal View History

2016-02-12 23:52:43 +08:00
module ReportsHelper
2017-03-08 21:14:03 +08:00
def render_new_element(hide)
render partial: 'reports/elements/new_element.html.erb',
locals: { hide: hide }
end
2016-02-12 23:52:43 +08:00
2017-03-08 21:14:03 +08:00
def render_report_element(element, provided_locals = nil)
children_html = ''.html_safe
2016-02-12 23:52:43 +08:00
2017-03-08 21:14:03 +08:00
# First, recursively render element's children
if element.comments? || element.project_header?
# Render no children
elsif element.result?
# Special handling for result comments
if element.has_children?
children_html.safe_concat render_new_element(true)
element.children.each do |child|
2017-03-08 21:41:42 +08:00
children_html
.safe_concat render_report_element(child, provided_locals)
2017-03-08 21:14:03 +08:00
end
else
children_html.safe_concat render_new_element(false)
2016-02-12 23:52:43 +08:00
end
else
2017-03-08 21:14:03 +08:00
if element.has_children?
element.children.each do |child|
children_html.safe_concat render_new_element(false)
2017-03-08 21:41:42 +08:00
children_html
.safe_concat render_report_element(child, provided_locals)
2017-03-08 21:14:03 +08:00
end
2016-02-12 23:52:43 +08:00
end
2017-03-08 21:14:03 +08:00
children_html.safe_concat render_new_element(false)
2016-02-12 23:52:43 +08:00
end
2017-03-08 21:14:03 +08:00
file_name = element.type_of
2017-03-09 17:51:54 +08:00
if element.type_of.in? %w(step result_asset result_table result_text)
file_name = "my_module_#{element.type_of.singularize}"
end
2017-03-08 21:14:03 +08:00
view = "reports/elements/#{file_name}_element.html.erb"
2016-07-21 19:11:15 +08:00
2017-03-08 21:14:03 +08:00
locals = provided_locals.nil? ? {} : provided_locals.clone
locals[:children] = children_html
2016-07-21 19:11:15 +08:00
2017-03-08 21:14:03 +08:00
# ReportExtends is located in config/initializers/extends/report_extends.rb
2017-03-09 17:51:54 +08:00
2017-03-08 21:14:03 +08:00
ReportElement.type_ofs.keys.each do |type|
2017-03-24 23:25:50 +08:00
next unless element.public_send("#{type}?")
element.element_references.each do |el_ref|
locals[el_ref.class.name.underscore.to_sym] = el_ref
end
2017-03-08 21:41:42 +08:00
locals[:order] = element
.sort_order if type.in? ReportExtends::SORTED_ELEMENTS
2017-03-08 21:14:03 +08:00
end
2016-02-12 23:52:43 +08:00
2017-03-08 21:14:03 +08:00
(render partial: view, locals: locals).html_safe
end
2016-02-12 23:52:43 +08:00
2017-03-08 21:14:03 +08:00
# "Hack" to omit file preview URL because of WKHTML issues
def report_image_asset_url(asset)
2017-03-08 21:41:42 +08:00
prefix = ''
if ENV['PAPERCLIP_STORAGE'].present? &&
ENV['MAIL_SERVER_URL'].present? &&
ENV['PAPERCLIP_STORAGE'] == 'filesystem'
prefix = ENV['MAIL_SERVER_URL']
end
if !prefix.empty? &&
!prefix.include?('http://') &&
!prefix.include?('https://')
prefix = "http://#{prefix}"
end
2017-03-08 21:14:03 +08:00
url = prefix + asset.url(:medium, timeout: Constants::URL_LONG_EXPIRE_TIME)
image_tag(url)
end
2016-07-21 19:11:15 +08:00
2017-03-08 21:41:42 +08:00
# "Hack" to load Glyphicons css directly from the CDN
# site so they work in report
2017-03-08 21:14:03 +08:00
def bootstrap_cdn_link_tag
2017-03-08 21:41:42 +08:00
specs = Gem.loaded_specs['bootstrap-sass']
return '' unless specs.present?
stylesheet_link_tag("http://netdna.bootstrapcdn.com/bootstrap/" \
"#{specs.version.version}/css/bootstrap.min.css",
media: 'all')
2017-03-08 21:14:03 +08:00
end
2016-07-21 19:11:15 +08:00
2017-03-08 21:14:03 +08:00
def font_awesome_cdn_link_tag
stylesheet_link_tag(
2017-03-08 21:41:42 +08:00
'https://maxcdn.bootstrapcdn.com/font-awesome' \
'/4.6.3/css/font-awesome.min.css'
2017-03-08 21:14:03 +08:00
)
end
def step_status_label(step)
if step.completed
style = 'success'
text = t('protocols.steps.completed')
else
style = 'default'
text = t('protocols.steps.uncompleted')
end
"<span class=\"label label-#{style}\">#{text}</span>".html_safe
end
2016-02-12 23:52:43 +08:00
end