2022-10-20 16:42:38 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Experiments
|
|
|
|
class TableViewService
|
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
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
|
2022-12-02 18:01:40 +08:00
|
|
|
include BootstrapFormHelper
|
|
|
|
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-11-09 20:54:19 +08:00
|
|
|
def initialize(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-10-20 16:42:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
|
|
|
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-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
|
|
|
|
project = experiment.project
|
|
|
|
|
|
|
|
result[my_module.id] = {
|
|
|
|
columns: prepared_my_module,
|
2022-12-01 22:08:59 +08:00
|
|
|
provisioning_status: my_module.provisioning_status,
|
2022-11-22 22:01:12 +08:00
|
|
|
urls: {
|
|
|
|
permissions: permissions_my_module_path(my_module),
|
|
|
|
actions_dropdown: actions_dropdown_my_module_path(my_module),
|
|
|
|
name_update: my_module_path(my_module),
|
2022-12-01 22:08:59 +08:00
|
|
|
provisioning_status:
|
|
|
|
my_module.provisioning_status == 'in_progress' && provisioning_status_my_module_url(my_module),
|
2022-11-22 22:01:12 +08:00
|
|
|
access: edit_access_permissions_project_experiment_my_module_path(project, experiment, my_module)
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
def task_name_presenter(my_module)
|
|
|
|
{
|
2022-11-11 18:22:25 +08:00
|
|
|
id: my_module.id,
|
2022-10-20 16:42:38 +08:00
|
|
|
name: my_module.name,
|
2022-12-01 22:08:59 +08:00
|
|
|
provisioning_status: my_module.provisioning_status,
|
2022-10-20 16:42:38 +08:00
|
|
|
url: protocols_my_module_path(my_module)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def id_presenter(my_module)
|
2022-11-07 18:49:25 +08:00
|
|
|
{
|
2022-11-22 22:01:12 +08:00
|
|
|
id: my_module.id
|
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(
|
|
|
|
partial: 'experiments/table_due_date.html.erb',
|
|
|
|
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)
|
|
|
|
{
|
2022-10-27 15:52:20 +08:00
|
|
|
count: 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)
|
|
|
|
{
|
|
|
|
name: my_module.my_module_status.name,
|
|
|
|
color: my_module.my_module_status.color
|
|
|
|
}
|
|
|
|
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(
|
|
|
|
partial: 'experiments/assigned_users.html.erb',
|
|
|
|
locals: { my_module: my_module, user: @user }
|
|
|
|
) }
|
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,
|
|
|
|
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,
|
|
|
|
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-10-20 16:42:38 +08:00
|
|
|
end
|
|
|
|
end
|