2022-10-20 16:42:38 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Experiments
|
|
|
|
class TableViewService
|
|
|
|
include ActionView::Helpers::DateHelper
|
2022-10-27 20:41:35 +08:00
|
|
|
include CommentHelper
|
2022-10-24 21:25:02 +08:00
|
|
|
include ProjectsHelper
|
|
|
|
include InputSanitizeHelper
|
2023-07-03 17:11:51 +08:00
|
|
|
include SciFormHelper
|
2022-12-02 18:01:40 +08:00
|
|
|
include MyModulesHelper
|
2022-10-24 21:25:02 +08:00
|
|
|
include Canaid::Helpers::PermissionsHelper
|
2022-12-02 18:01:40 +08:00
|
|
|
include Rails.application.routes.url_helpers
|
2022-10-20 16:42:38 +08:00
|
|
|
|
|
|
|
COLUMNS = %i(
|
|
|
|
task_name
|
|
|
|
id
|
|
|
|
due_date
|
|
|
|
age
|
|
|
|
results
|
|
|
|
status
|
2022-11-24 19:07:52 +08:00
|
|
|
archived
|
2022-10-20 16:42:38 +08:00
|
|
|
assigned
|
|
|
|
tags
|
|
|
|
comments
|
2022-10-24 21:25:02 +08:00
|
|
|
)
|
2022-10-20 16:42:38 +08:00
|
|
|
|
2022-10-24 21:25:02 +08:00
|
|
|
PRELOAD = {
|
|
|
|
results: {},
|
|
|
|
my_module_status: {},
|
|
|
|
tags: {},
|
|
|
|
task_comments: {},
|
2022-11-09 20:54:19 +08:00
|
|
|
user_assignments: :user,
|
2022-11-14 05:33:41 +08:00
|
|
|
designated_users: {},
|
|
|
|
experiment: :project
|
2022-10-24 21:25:02 +08:00
|
|
|
}
|
2022-10-20 16:42:38 +08:00
|
|
|
|
2022-12-05 17:30:20 +08:00
|
|
|
def initialize(experiment, my_modules, user, params)
|
2022-10-20 16:42:38 +08:00
|
|
|
@my_modules = my_modules
|
2022-11-09 20:54:19 +08:00
|
|
|
@page = params[:page] || 1
|
2022-10-24 21:25:02 +08:00
|
|
|
@user = user
|
2022-11-09 20:54:19 +08:00
|
|
|
@filters = params[:filters] || []
|
2022-12-05 17:30:20 +08:00
|
|
|
@params = params
|
|
|
|
initialize_table_sorting(experiment)
|
2022-10-20 16:42:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
2023-05-23 21:29:14 +08:00
|
|
|
if @my_modules.nil?
|
|
|
|
return {
|
|
|
|
next_page: nil,
|
|
|
|
data: []
|
|
|
|
}
|
|
|
|
end
|
2022-12-05 17:30:20 +08:00
|
|
|
result = []
|
2022-11-09 20:54:19 +08:00
|
|
|
my_module_list = @my_modules
|
|
|
|
@filters.each do |name, value|
|
|
|
|
my_module_list = __send__("#{name}_filter", my_module_list, value) if value.present?
|
|
|
|
end
|
2022-11-07 18:46:54 +08:00
|
|
|
|
2022-12-05 17:30:20 +08:00
|
|
|
my_module_list = sort_records(my_module_list)
|
|
|
|
|
2022-11-09 20:54:19 +08:00
|
|
|
my_module_list = my_module_list.includes(PRELOAD)
|
|
|
|
.select('my_modules.*')
|
|
|
|
.group('my_modules.id')
|
|
|
|
.page(@page || 1)
|
|
|
|
.per(Constants::DEFAULT_ELEMENTS_PER_PAGE)
|
2022-11-07 18:46:54 +08:00
|
|
|
my_module_list.each do |my_module|
|
2022-10-20 16:42:38 +08:00
|
|
|
prepared_my_module = []
|
|
|
|
COLUMNS.each do |col|
|
|
|
|
column_data = {
|
|
|
|
column_type: col
|
|
|
|
}
|
|
|
|
column_data[:data] = __send__("#{col}_presenter", my_module)
|
|
|
|
prepared_my_module.push(column_data)
|
|
|
|
end
|
|
|
|
|
2022-11-22 22:01:12 +08:00
|
|
|
experiment = my_module.experiment
|
|
|
|
|
2022-12-05 17:30:20 +08:00
|
|
|
result.push({ id: my_module.id,
|
|
|
|
columns: prepared_my_module,
|
|
|
|
provisioning_status: my_module.provisioning_status,
|
|
|
|
urls: {
|
|
|
|
permissions: permissions_my_module_path(my_module),
|
|
|
|
actions_dropdown: actions_dropdown_my_module_path(my_module),
|
|
|
|
name_update: my_module_path(my_module),
|
|
|
|
restore: restore_my_modules_experiment_path(experiment),
|
|
|
|
provisioning_status:
|
|
|
|
my_module.provisioning_status == 'in_progress' &&
|
|
|
|
provisioning_status_my_module_url(my_module),
|
2023-02-23 21:57:38 +08:00
|
|
|
access: access_url(my_module)
|
2022-12-05 17:30:20 +08:00
|
|
|
} })
|
2022-10-20 16:42:38 +08:00
|
|
|
end
|
2022-11-07 18:46:54 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
next_page: my_module_list.next_page,
|
|
|
|
data: result
|
|
|
|
}
|
2022-10-20 16:42:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-02-23 21:57:38 +08:00
|
|
|
def access_url(my_module)
|
2023-01-13 18:50:42 +08:00
|
|
|
if can_manage_my_module_users?(@user, my_module)
|
2023-02-23 21:57:38 +08:00
|
|
|
edit_access_permissions_my_module_path(my_module)
|
2023-01-12 19:15:29 +08:00
|
|
|
else
|
2023-02-23 21:57:38 +08:00
|
|
|
access_permissions_my_module_path(my_module)
|
2023-01-09 23:04:57 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-20 16:42:38 +08:00
|
|
|
def task_name_presenter(my_module)
|
|
|
|
{
|
2022-11-11 18:22:25 +08:00
|
|
|
id: my_module.id,
|
2023-03-14 01:00:30 +08:00
|
|
|
name: escape_input(my_module.name),
|
2022-12-01 22:08:59 +08:00
|
|
|
provisioning_status: my_module.provisioning_status,
|
2023-06-02 23:16:39 +08:00
|
|
|
url: protocols_my_module_path(my_module, view_mode: my_module.archived_branch? ? 'archived' : 'active')
|
2022-10-20 16:42:38 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def id_presenter(my_module)
|
2022-11-07 18:49:25 +08:00
|
|
|
{
|
2023-01-06 17:58:41 +08:00
|
|
|
id: my_module.code
|
2022-11-07 18:49:25 +08:00
|
|
|
}
|
2022-10-20 16:42:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def due_date_presenter(my_module)
|
2022-12-02 18:01:40 +08:00
|
|
|
{
|
|
|
|
id: my_module.id,
|
|
|
|
data: ApplicationController.renderer.render(
|
2023-06-21 20:13:20 +08:00
|
|
|
partial: 'experiments/table_due_date',
|
2022-12-02 18:01:40 +08:00
|
|
|
locals: { my_module: my_module,
|
|
|
|
user: @user }
|
|
|
|
)
|
|
|
|
}
|
2022-10-20 16:42:38 +08:00
|
|
|
end
|
|
|
|
|
2022-11-24 19:07:52 +08:00
|
|
|
def archived_presenter(my_module)
|
|
|
|
if my_module.archived?
|
|
|
|
I18n.l(my_module.archived_on, format: :full_date)
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-20 16:42:38 +08:00
|
|
|
def age_presenter(my_module)
|
|
|
|
time_ago_in_words(my_module.created_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
def results_presenter(my_module)
|
|
|
|
{
|
2023-01-12 20:19:32 +08:00
|
|
|
count: (my_module.archived_branch? ? my_module.results : my_module.results.active).length,
|
2022-10-20 16:42:38 +08:00
|
|
|
url: results_my_module_path(my_module)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def status_presenter(my_module)
|
|
|
|
{
|
2023-03-14 01:00:30 +08:00
|
|
|
name: escape_input(my_module.my_module_status.name),
|
2023-08-24 20:52:11 +08:00
|
|
|
color: escape_input(my_module.my_module_status.color),
|
2023-08-29 22:12:00 +08:00
|
|
|
light_color: my_module.my_module_status.light_color?
|
2022-10-20 16:42:38 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-10-24 21:25:02 +08:00
|
|
|
def assigned_presenter(my_module)
|
2022-11-30 21:56:19 +08:00
|
|
|
{ html: ApplicationController.renderer.render(
|
2023-06-21 20:13:20 +08:00
|
|
|
partial: 'experiments/assigned_users',
|
2023-01-05 19:00:12 +08:00
|
|
|
locals: { my_module: my_module,
|
|
|
|
user: @user,
|
2023-01-06 18:43:23 +08:00
|
|
|
skip_unassigned: @params[:view_mode] == 'archived' ||
|
|
|
|
!can_manage_my_module_designated_users?(@user, my_module) }
|
2022-11-30 21:56:19 +08:00
|
|
|
) }
|
2022-10-24 21:25:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def tags_presenter(my_module)
|
2022-11-02 22:17:10 +08:00
|
|
|
{
|
|
|
|
my_module_id: my_module.id,
|
|
|
|
tags: my_module.tags.length,
|
2023-01-10 18:09:23 +08:00
|
|
|
can_create: can_manage_my_module_tags?(@user, my_module),
|
2022-11-02 22:17:10 +08:00
|
|
|
edit_url: my_module_tags_edit_path(my_module, format: :json)
|
|
|
|
}
|
2022-10-24 21:25:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def comments_presenter(my_module)
|
2022-10-27 20:41:35 +08:00
|
|
|
{
|
|
|
|
id: my_module.id,
|
|
|
|
count: my_module.comments.count,
|
2023-01-06 19:02:35 +08:00
|
|
|
can_create: can_create_my_module_comments?(@user, my_module),
|
2022-10-27 20:41:35 +08:00
|
|
|
count_unseen: count_unseen_comments(my_module, @user)
|
|
|
|
}
|
2022-10-24 21:25:02 +08:00
|
|
|
end
|
2022-11-09 20:54:19 +08:00
|
|
|
|
|
|
|
def name_filter(my_modules, value)
|
|
|
|
my_modules.where_attributes_like('my_modules.name', value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def due_date_from_filter(my_modules, value)
|
|
|
|
my_modules.where('my_modules.due_date >= ?', value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def due_date_to_filter(my_modules, value)
|
|
|
|
my_modules.where('my_modules.due_date <= ?', value)
|
|
|
|
end
|
|
|
|
|
2022-11-30 20:47:49 +08:00
|
|
|
def archived_on_from_filter(my_modules, value)
|
|
|
|
my_modules.where('my_modules.archived_on >= ?', value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def archived_on_to_filter(my_modules, value)
|
|
|
|
my_modules.where('my_modules.archived_on <= ?', value)
|
|
|
|
end
|
|
|
|
|
2022-11-09 20:54:19 +08:00
|
|
|
def assigned_users_filter(my_modules, value)
|
|
|
|
my_modules.joins(:user_my_modules).where(user_my_modules: { user_id: value })
|
|
|
|
end
|
|
|
|
|
|
|
|
def statuses_filter(my_modules, value)
|
2022-12-01 22:08:59 +08:00
|
|
|
my_modules.where(my_module_status_id: value)
|
2022-11-09 20:54:19 +08:00
|
|
|
end
|
2022-12-05 17:30:20 +08:00
|
|
|
|
|
|
|
def initialize_table_sorting(experiment)
|
|
|
|
@view_state = experiment.current_view_state(@user)
|
|
|
|
@view_mode = @params[:view_mode] || 'active'
|
|
|
|
@sort = @view_state.state.dig('my_modules', @view_mode, 'sort') || 'atoz'
|
|
|
|
if @params[:sort] && @sort != @params[:sort] && %w(due_first due_last atoz ztoa
|
|
|
|
archived_old archived_new).include?(@params[:sort])
|
|
|
|
@view_state.state['my_modules'].merge!(Hash[@view_mode, { 'sort': @params[:sort] }.stringify_keys])
|
|
|
|
@view_state.save!
|
|
|
|
@sort = @view_state.state.dig('my_modules', @view_mode, 'sort')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort_records(records)
|
|
|
|
case @sort
|
|
|
|
when 'due_first'
|
2023-01-12 18:57:48 +08:00
|
|
|
records.order(:due_date, :name)
|
2022-12-05 17:30:20 +08:00
|
|
|
when 'due_last'
|
2023-01-12 18:57:48 +08:00
|
|
|
records.order(Arel.sql("COALESCE(due_date, DATE '2100-01-01') DESC"), :name)
|
2022-12-05 17:30:20 +08:00
|
|
|
when 'atoz'
|
|
|
|
records.order(:name)
|
|
|
|
when 'ztoa'
|
|
|
|
records.order(name: :desc)
|
|
|
|
when 'archived_old'
|
|
|
|
records.order(Arel.sql('COALESCE(my_modules.archived_on, my_modules.archived_on) ASC'))
|
|
|
|
when 'archived_new'
|
|
|
|
records.order(Arel.sql('COALESCE(my_modules.archived_on, my_modules.archived_on) DESC'))
|
|
|
|
else
|
|
|
|
records
|
|
|
|
end
|
|
|
|
end
|
2022-10-20 16:42:38 +08:00
|
|
|
end
|
|
|
|
end
|