scinote-web/app/controllers/navigator/base_controller.rb

211 lines
8.7 KiB
Ruby
Raw Normal View History

2023-04-21 21:25:52 +08:00
# frozen_string_literal: true
module Navigator
class BaseController < ApplicationController
private
def project_serializer(project, archived)
2023-04-21 21:25:52 +08:00
{
id: project.code,
name: project.name,
2023-12-07 03:53:11 +08:00
url: experiments_path(project_id: project, view_mode: archived ? 'archived' : 'active'),
2023-04-21 21:25:52 +08:00
archived: project.archived,
type: :project,
has_children: project.has_children,
children_url: navigator_project_path(project),
disabled: project.disabled
2023-04-21 21:25:52 +08:00
}
end
def folder_serializer(folder, archived)
2023-04-21 21:25:52 +08:00
{
id: folder.code,
name: folder.name,
url: project_folder_path(folder, view_mode: archived ? 'archived' : 'active'),
2023-04-21 21:25:52 +08:00
archived: folder.archived,
type: :folder,
has_children: folder.try(:has_children),
2023-04-21 21:25:52 +08:00
children_url: navigator_project_folder_path(folder)
}
end
def experiment_serializer(experiment, archived)
2023-04-21 21:25:52 +08:00
{
id: experiment.code,
name: experiment.name,
url: my_modules_experiment_path(experiment, view_mode: archived ? 'archived' : 'active'),
archived: experiment.archived_branch?,
2023-04-21 21:25:52 +08:00
type: :experiment,
has_children: experiment.has_children,
children_url: navigator_experiment_path(experiment)
}
end
def my_module_serializer(my_module, archived)
2023-04-21 21:25:52 +08:00
{
id: my_module.code,
name: my_module.name,
type: :my_module,
url: protocols_my_module_path(my_module, view_mode: archived ? 'archived' : 'active'),
archived: my_module.archived_branch?,
2023-04-21 21:25:52 +08:00
has_children: false
}
end
def fetch_projects(object = nil, archived = false)
if object&.is_a?(ProjectFolder)
folder = object
project = nil
else
folder = object&.project_folder
project = object
end
2023-04-21 21:25:52 +08:00
has_children_sql = if !archived
'SUM(CASE WHEN experiments.archived IS FALSE OR
(projects.archived IS TRUE AND experiments.id IS NOT NULL)
THEN 1 ELSE 0 END) > 0 AS has_children'
2023-04-21 21:25:52 +08:00
else
'SUM(CASE WHEN experiments.archived IS TRUE OR
my_modules.archived IS TRUE OR
(projects.archived IS TRUE AND experiments.id IS NOT NULL)
THEN 1 ELSE 0 END) > 0 AS has_children'
2023-04-21 21:25:52 +08:00
end
disabled_sql = 'SUM(CASE WHEN project_user_roles IS NULL THEN 0 ELSE 1 END) < 1 AS disabled'
2023-04-21 21:25:52 +08:00
current_team.projects
.where(project_folder_id: folder)
2023-05-11 17:21:50 +08:00
.visible_to(current_user, current_team)
2023-04-21 21:25:52 +08:00
.with_children_viewable_by_user(current_user)
.joins("LEFT OUTER JOIN user_assignments project_user_assignments
ON project_user_assignments.assignable_type = 'Project'
AND project_user_assignments.assignable_id = projects.id
AND project_user_assignments.user_id = #{current_user.id}
LEFT OUTER JOIN user_roles project_user_roles
ON project_user_roles.id = project_user_assignments.user_role_id
AND project_user_roles.permissions @> ARRAY['#{ProjectPermissions::READ}']::varchar[]")
.where('projects.archived = :archived OR
(
(
experiments.archived = :archived OR
my_modules.archived = :archived
) AND
:archived IS TRUE
) OR
projects.id = :project_id',
archived: archived,
project_id: project&.id || -1)
2023-04-21 21:25:52 +08:00
.select(
'projects.id',
'projects.name',
'projects.archived',
disabled_sql,
2023-04-21 21:25:52 +08:00
has_children_sql
).group('projects.id')
end
def fetch_project_folders(object = nil, archived = false)
folder = if object.is_a?(ProjectFolder)
object
else
object&.project_folder
end
has_children_sql = 'SUM(CASE WHEN viewable_projects.id IS NOT NULL OR project_folders_project_folders.id IS NOT NULL
THEN 1 ELSE 0 END) > 0'
2023-04-21 21:25:52 +08:00
current_team.project_folders.where(parent_folder: folder)
.left_outer_joins(:projects, project_folders: {})
.joins(
"LEFT OUTER JOIN (#{Project.viewable_by_user(current_user, current_team).where(archived: archived).to_sql}) " \
"viewable_projects ON viewable_projects.project_folder_id = project_folders.id"
)
2023-04-21 21:25:52 +08:00
.select(
'project_folders.id',
'project_folders.name',
'project_folders.archived',
"#{has_children_sql} AS has_children"
2023-04-21 21:25:52 +08:00
).group('project_folders.id')
.having('project_folders.archived = :archived', archived: archived)
2023-04-21 21:25:52 +08:00
end
def fetch_experiments(object, archived = false)
if object.is_a?(Project)
project = object
experiment = nil
else
project = object.project
experiment = object
end
2023-04-21 21:25:52 +08:00
has_children_sql = if !archived
'SUM(CASE WHEN my_modules.archived IS FALSE OR
(experiments.archived IS TRUE AND my_modules.id IS NOT NULL)
THEN 1 ELSE 0 END) > 0 AS has_children'
elsif project.archived?
'COUNT(my_modules.id) > 0 AS has_children'
2023-04-21 21:25:52 +08:00
else
'SUM(CASE WHEN my_modules.archived IS TRUE OR
(experiments.archived IS TRUE AND my_modules.id IS NOT NULL)
THEN 1 ELSE 0 END) > 0 AS has_children'
2023-04-21 21:25:52 +08:00
end
experiments = project.experiments
.viewable_by_user(current_user, current_team)
.with_children_viewable_by_user(current_user)
.select(
'experiments.id',
'experiments.name',
'experiments.archived',
'experiments.project_id',
has_children_sql
).group('experiments.id')
unless project.archived?
experiments = experiments.where('
experiments.archived = :archived OR
my_modules.archived = :archived AND
:archived IS TRUE OR
experiments.id = :experiment_id
', archived: archived, experiment_id: experiment&.id || -1)
end
experiments
2023-04-21 21:25:52 +08:00
end
def fetch_my_modules(experiment, archived = false)
my_modules = experiment.my_modules
.viewable_by_user(current_user, current_team)
my_modules = my_modules.where(archived: archived) unless experiment.archived_branch?
my_modules
2023-04-21 21:25:52 +08:00
end
def build_folder_tree(folder, children)
archived = params[:archived] == 'true'
tree = fetch_projects(folder.parent_folder, archived).map { |i| project_serializer(i, archived) } +
fetch_project_folders(folder.parent_folder, archived).map { |i| folder_serializer(i, archived) }
# Tree will not contain folder when folder archived state and params archived values are contradictory
tree.find { |i| i[:id] == folder.code } || (tree << folder_serializer(folder, archived))
2023-04-21 21:25:52 +08:00
tree.find { |i| i[:id] == folder.code }[:children] = children
tree = build_folder_tree(folder.parent_folder, tree) if folder.parent_folder.present?
2023-04-21 21:25:52 +08:00
tree
end
def project_level_branch(object = nil)
archived = params[:archived] == 'true'
fetch_projects(object, archived)
.map { |i| project_serializer(i, archived) } +
fetch_project_folders(object, archived)
.map { |i| folder_serializer(i, archived) }
2023-04-21 21:25:52 +08:00
end
def experiment_level_branch(object)
archived = params[:archived] == 'true'
fetch_experiments(object, archived)
.map { |i| experiment_serializer(i, archived) }
2023-04-21 21:25:52 +08:00
end
def my_module_level_branch(experiment)
archived = params[:archived] == 'true'
2023-04-21 21:25:52 +08:00
fetch_my_modules(experiment, archived)
.map { |i| my_module_serializer(i, archived) }
2023-04-21 21:25:52 +08:00
end
end
end