2023-12-12 19:17:38 +08:00
|
|
|
module Lists
|
|
|
|
class MyModuleSerializer < ActiveModel::Serializer
|
|
|
|
include Canaid::Helpers::PermissionsHelper
|
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
include ActionView::Helpers::DateHelper
|
|
|
|
include CommentHelper
|
|
|
|
|
|
|
|
ATTRIBUTES = %i(
|
|
|
|
name
|
|
|
|
provisioning_status
|
|
|
|
code
|
|
|
|
urls
|
|
|
|
due_date
|
|
|
|
due_date_status
|
|
|
|
archived
|
|
|
|
archived_on
|
|
|
|
age
|
|
|
|
results
|
|
|
|
status
|
|
|
|
designated_users
|
|
|
|
tags
|
|
|
|
comments
|
|
|
|
due_date_formatted
|
|
|
|
permissions
|
|
|
|
default_public_user_role_id
|
2024-02-22 20:38:24 +08:00
|
|
|
team
|
2023-12-12 19:17:38 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def attributes(_options = {})
|
|
|
|
ATTRIBUTES.index_with do |attribute|
|
|
|
|
__send__(attribute)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
delegate :name, to: :object
|
|
|
|
|
|
|
|
delegate :provisioning_status, to: :object
|
|
|
|
|
|
|
|
def default_public_user_role_id
|
|
|
|
object.experiment.project.default_public_user_role_id
|
|
|
|
end
|
|
|
|
|
|
|
|
delegate :code, to: :object
|
|
|
|
|
|
|
|
def permissions
|
|
|
|
{
|
|
|
|
manage_designated_users: can_manage_my_module_designated_users?(object),
|
|
|
|
manage_tags: can_manage_my_module_tags?(object),
|
|
|
|
create_comments: can_create_my_module_comments?(object)
|
|
|
|
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def urls
|
|
|
|
user = scope[:user] || @instance_options[:user]
|
|
|
|
|
|
|
|
urls_list = {
|
2024-03-12 17:10:05 +08:00
|
|
|
show: protocols_my_module_path(object, view_mode: archived ? 'archived' : 'active'),
|
2023-12-12 19:17:38 +08:00
|
|
|
results: my_module_results_path(object),
|
|
|
|
assign_tags: my_module_my_module_tags_path(object),
|
|
|
|
assigned_tags: assigned_tags_my_module_my_module_tags_path(object),
|
|
|
|
users_list: search_my_module_user_my_module_path(object, my_module_id: object.id),
|
|
|
|
experiments_to_move: experiments_to_move_experiment_path(object.experiment),
|
|
|
|
move: move_modules_experiment_path(object.experiment),
|
|
|
|
update: my_module_path(object),
|
2024-03-18 18:29:02 +08:00
|
|
|
show_access: access_permissions_my_module_path(object),
|
|
|
|
provisioning_status: provisioning_status_my_module_url(object)
|
2023-12-12 19:17:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if can_manage_project_users?(object.experiment.project)
|
|
|
|
urls_list[:update_access] = access_permissions_my_module_path(object)
|
|
|
|
end
|
|
|
|
|
|
|
|
if can_update_my_module_due_date?(object)
|
|
|
|
urls_list[:update_due_date] = my_module_path(object, user, format: :json)
|
|
|
|
end
|
|
|
|
|
|
|
|
urls_list
|
|
|
|
end
|
|
|
|
|
|
|
|
def due_date
|
|
|
|
I18n.l(object.due_date, format: :default) if object.due_date
|
|
|
|
end
|
|
|
|
|
|
|
|
def due_date_formatted
|
|
|
|
I18n.l(object.due_date, format: :full_date) if object.due_date
|
|
|
|
end
|
|
|
|
|
|
|
|
def due_date_status
|
2024-03-12 17:10:05 +08:00
|
|
|
if (archived || object.completed?) && object.due_date
|
2023-12-12 19:17:38 +08:00
|
|
|
return :ok
|
|
|
|
elsif object.is_one_day_prior? && !object.completed?
|
|
|
|
return :one_day_prior
|
|
|
|
elsif object.is_overdue? && !object.completed?
|
|
|
|
return :overdue
|
|
|
|
end
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def archived
|
2024-03-12 17:10:05 +08:00
|
|
|
object.archived_branch?
|
2023-12-12 19:17:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def archived_on
|
|
|
|
I18n.l(object.archived_on, format: :full_date) if object.archived?
|
|
|
|
end
|
|
|
|
|
|
|
|
def age
|
|
|
|
time_ago_in_words(object.created_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
def results
|
2024-03-12 17:10:05 +08:00
|
|
|
(archived ? object.results : object.results.active).length
|
2023-12-12 19:17:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
|
|
|
{
|
|
|
|
name: object.my_module_status.name,
|
|
|
|
color: object.my_module_status.color,
|
|
|
|
light_color: object.my_module_status.light_color?
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def designated_users
|
|
|
|
object.user_my_modules.map do |user_my_module|
|
|
|
|
user = user_my_module.user
|
|
|
|
{
|
|
|
|
id: user.id,
|
|
|
|
name: user.name,
|
|
|
|
avatar: avatar_path(user, :icon_small)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tags
|
2024-02-15 17:10:15 +08:00
|
|
|
object.tags.map do |tag|
|
|
|
|
{
|
|
|
|
id: tag.id,
|
|
|
|
name: tag.name,
|
|
|
|
color: tag.color
|
|
|
|
}
|
|
|
|
end
|
2023-12-12 19:17:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def comments
|
|
|
|
@user = scope[:user] || @instance_options[:user]
|
|
|
|
{
|
|
|
|
count: object.comments.count,
|
|
|
|
count_unseen: count_unseen_comments(object, @user)
|
|
|
|
}
|
|
|
|
end
|
2024-02-22 20:38:24 +08:00
|
|
|
|
|
|
|
def team
|
|
|
|
object.team.name
|
|
|
|
end
|
2023-12-12 19:17:38 +08:00
|
|
|
end
|
|
|
|
end
|