scinote-web/app/helpers/my_modules_helper.rb

165 lines
5 KiB
Ruby
Raw Normal View History

2019-04-29 01:11:41 +08:00
# frozen_string_literal: true
2016-02-12 23:52:43 +08:00
module MyModulesHelper
def ordered_step_of(my_module)
2016-07-21 19:11:15 +08:00
my_module.protocol.steps.order(:position)
2016-02-12 23:52:43 +08:00
end
def ordered_checklist_items(checklist)
checklist.checklist_items.order(:position)
end
def ordered_assets(step)
view_state = step.current_view_state(current_user)
sort = case view_state.state.dig('assets', 'sort')
when 'old' then { created_at: :asc }
when 'atoz' then { 'active_storage_blobs.filename': :asc }
when 'ztoa' then { 'active_storage_blobs.filename': :desc }
else { created_at: :desc }
end
step.assets.joins(file_attachment: :blob).order(sort)
end
def az_ordered_assets_index(step, asset_id)
step.assets
.joins(file_attachment: :blob)
.order(Arel.sql('LOWER(active_storage_blobs.filename)'))
.pluck(:id)
.index(asset_id) || 0
2016-02-12 23:52:43 +08:00
end
2017-02-16 18:45:07 +08:00
def get_task_alert_color(my_module)
alert = ''
if !my_module.archived_branch? && !my_module.completed?
2017-02-16 18:45:07 +08:00
alert = ' alert-yellow' if my_module.is_one_day_prior?
alert = ' alert-red' if my_module.is_overdue?
end
alert
end
2016-02-12 23:52:43 +08:00
def is_steps_page?
2019-04-29 01:11:41 +08:00
action_name == 'steps'
2016-02-12 23:52:43 +08:00
end
def is_results_page?
2019-04-29 01:11:41 +08:00
action_name == 'results'
2016-02-12 23:52:43 +08:00
end
2020-05-08 17:56:29 +08:00
def grouped_by_prj_exp(my_modules)
ungrouped_tasks = my_modules.joins(experiment: :project)
.select('experiments.name as experiment_name,
experiments.archived as experiment_archived,
projects.name as project_name,
projects.archived as project_archived,
my_modules.*')
ungrouped_tasks.group_by { |i| [i[:project_name], i[:experiment_name]] }.map do |group, tasks|
{
project_name: group[0],
project_archived: tasks[0]&.project_archived,
experiment_name: group[1],
experiment_archived: tasks[0]&.experiment_archived,
tasks: tasks
}
end
end
2020-05-12 22:26:44 +08:00
def assigned_repository_full_view_table_path(my_module, repository)
if repository.is_a?(RepositorySnapshot)
return full_view_table_my_module_repository_snapshot_path(my_module, repository)
2020-05-08 17:56:29 +08:00
end
2020-05-12 22:26:44 +08:00
full_view_table_my_module_repository_path(my_module, repository)
2016-02-12 23:52:43 +08:00
end
2020-05-12 22:26:44 +08:00
def assigned_repository_simple_view_index_path(my_module, repository)
return index_dt_my_module_repository_snapshot_path(my_module, repository) if repository.is_a?(RepositorySnapshot)
2020-05-12 22:26:44 +08:00
index_dt_my_module_repository_path(my_module, repository)
end
def assigned_repository_simple_view_footer_label(repository)
if repository.is_a?(RepositorySnapshot)
return t('my_modules.repository.snapshots.simple_view.snapshot_bottom_label',
2020-05-26 21:59:49 +08:00
date_time: l(repository.created_at, format: :full))
2020-05-12 22:26:44 +08:00
end
t('my_modules.repository.snapshots.simple_view.live_bottom_label')
2020-05-08 17:56:29 +08:00
end
2020-05-22 21:10:21 +08:00
def assigned_repository_simple_view_name_column_id(repository)
repository.is_a?(RepositorySnapshot) ? 2 : 3
end
2021-01-12 22:15:57 +08:00
def my_module_archived_on(my_module)
if my_module.archived?
my_module.archived_on
elsif my_module.experiment.archived?
my_module.experiment.archived_on
elsif my_module.experiment.project.archived?
my_module.experiment.project.archived_on
end
end
def my_module_due_status(my_module, datetime = DateTime.current)
return if my_module.archived_branch? || my_module.completed?
if my_module.is_overdue?(datetime)
I18n.t('my_modules.details.overdue')
elsif my_module.is_one_day_prior?(datetime)
I18n.t('my_modules.details.due_soon')
else
''
end
end
def serialize_assigned_my_module_value(my_module)
[
serialize_assigned_my_module_team_data(my_module.team),
serialize_assigned_my_module_project_data(my_module.project),
serialize_assigned_my_module_experiment_data(my_module.experiment),
serialize_assigned_my_module_data(my_module)
2023-09-25 14:29:54 +08:00
]
end
private
def serialize_assigned_my_module_team_data(team)
{
2023-09-25 14:29:54 +08:00
type: team.class.name.underscore,
value: team.name,
url: projects_path(team: team.id),
archived: false
}
end
def serialize_assigned_my_module_project_data(project)
archived = project.archived?
{
2023-09-25 14:29:54 +08:00
type: project.class.name.underscore,
value: project.name,
url: project_path(project, view_mode: archived ? 'archived' : 'active'),
archived: archived
}
end
def serialize_assigned_my_module_experiment_data(experiment)
archived = experiment.archived_branch?
{
2023-09-25 14:29:54 +08:00
type: experiment.class.name.underscore,
value: experiment.name,
url: archived ? module_archive_experiment_path(experiment) : my_modules_experiment_path(experiment),
archived: archived
}
end
def serialize_assigned_my_module_data(my_module)
{
2023-09-25 14:29:54 +08:00
type: my_module.class.name.underscore,
value: my_module.name,
url: protocols_my_module_path(my_module, view_mode: my_module.archived_branch? ? 'archived' : 'active'),
archived: my_module.archived_branch?
}
end
2016-02-12 23:52:43 +08:00
end