scinote-web/app/helpers/reports_helper.rb

122 lines
4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-02-12 23:52:43 +08:00
module ReportsHelper
include StringUtility
include Canaid::Helpers::PermissionsHelper
2017-03-08 21:14:03 +08:00
def render_report_element(element, provided_locals = nil)
case element.type_of
when 'experiment'
return unless can_read_experiment?(element.experiment)
when 'my_module'
return unless can_read_my_module?(element.my_module)
end
# Determine partial
view = "reports/elements/#{element.type_of}_element"
# Set locals
locals = provided_locals.nil? ? {} : provided_locals.clone
2016-02-12 23:52:43 +08:00
children_html = ''.html_safe
2017-03-08 21:14:03 +08:00
# First, recursively render element's children
2021-06-10 16:59:34 +08:00
if element.children.active.present?
element.children.active.each do |child|
children_html.safe_concat render_report_element(child, provided_locals)
2016-02-12 23:52:43 +08:00
end
end
locals[:report_element] = element
2017-03-08 21:14:03 +08:00
locals[:children] = children_html
2016-07-21 19:11:15 +08:00
render partial: view, locals: locals, formats: :html
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
# "Hack" to omit file preview URL because of WKHTML issues
def report_image_asset_url(asset)
2020-10-28 23:32:22 +08:00
preview = asset.inline? ? asset.large_preview : asset.medium_preview
image_tag(preview.processed.url(expires_in: Constants::URL_LONG_EXPIRE_TIME))
rescue ActiveStorage::FileNotFoundError
2023-06-15 21:12:51 +08:00
image_tag('icon_small/missing.svg')
rescue StandardError => e
Rails.logger.error e.message
tag.i(I18n.t('projects.reports.index.generation.file_preview_generation_error'))
2017-03-08 21:14:03 +08:00
end
2016-07-21 19:11:15 +08:00
def assigned_repository_or_snapshot(my_module, repository)
2021-06-18 16:33:31 +08:00
if repository.is_a?(RepositorySnapshot)
return my_module.repository_snapshots.find_by(parent_id: repository.parent_id, selected: true)
end
2021-06-18 16:33:31 +08:00
return nil unless my_module.assigned_repositories.exists?(id: repository.id)
2021-06-18 16:33:31 +08:00
selected_snapshot = repository.repository_snapshots.find_by(my_module: my_module, selected: true)
selected_snapshot || repository
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
2020-07-23 20:24:30 +08:00
"<span class=\"label step-label-#{style}\">[#{text}]</span>".html_safe
end
2017-06-22 22:40:50 +08:00
def font_awesome_cdn_link_tag
stylesheet_link_tag(
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/fontawesome.min.css',
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/regular.min.css',
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/solid.min.css'
)
end
def filter_steps_for_report(steps, settings)
include_completed_steps = settings.dig('task', 'protocol', 'completed_steps')
include_uncompleted_steps = settings.dig('task', 'protocol', 'uncompleted_steps')
if include_completed_steps && include_uncompleted_steps
steps
elsif include_completed_steps
steps.where(completed: true)
elsif include_uncompleted_steps
steps.where(completed: false)
else
steps.none
end
end
def order_results_for_report(results, order)
case order
when 'atoz'
results.order(name: :asc)
when 'ztoa'
results.order(name: :desc)
when 'new'
results.order(created_at: :desc)
when 'old_updated'
results.order(updated_at: :asc)
when 'new_updated'
results.order(updated_at: :desc)
else
results.order(created_at: :asc)
end
end
def report_experiment_descriptions(report)
report.report_elements.experiment.map do |experiment_element|
experiment_element.experiment.description
end
end
def assigned_to_report_repository_items(report, repository_name)
repository = Repository.accessible_by_teams(report.team).where(name: repository_name).take
return RepositoryRow.none if repository.blank?
2021-05-04 18:49:38 +08:00
my_modules = MyModule.joins(:experiment)
.where(experiment: { project: report.project })
.where(id: report.report_elements.my_module.select(:my_module_id))
repository.repository_rows.joins(:my_modules).where(my_modules: my_modules)
end
2016-02-12 23:52:43 +08:00
end