scinote-web/app/serializers/lists/project_and_folder_serializer.rb

126 lines
3.4 KiB
Ruby
Raw Normal View History

2023-12-11 22:41:03 +08:00
# frozen_string_literal: true
2023-11-10 20:34:36 +08:00
module Lists
class ProjectAndFolderSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
2023-12-05 03:59:16 +08:00
include Canaid::Helpers::PermissionsHelper
include CommentHelper
2023-11-10 20:34:36 +08:00
attributes :name, :code, :created_at, :archived_on, :users, :urls, :folder, :hidden,
:folder_info, :default_public_user_role_id, :team, :top_level_assignable,
:comments, :updated_at, :permissions
2023-12-05 03:59:16 +08:00
def team
object.team.name
end
2023-11-10 20:34:36 +08:00
def folder
!project?
end
2023-12-05 03:59:16 +08:00
def top_level_assignable
project?
end
def hidden
object.hidden? if project?
end
2023-12-01 07:01:08 +08:00
def default_public_user_role_id
object.default_public_user_role_id if project?
end
2023-11-10 20:34:36 +08:00
def code
2024-04-09 20:30:34 +08:00
if project?
object.code
else
"F#{object.id}" # We don't have proper code for folders, but we need to correct selection
end
2023-11-10 20:34:36 +08:00
end
def created_at
I18n.l(object.created_at, format: :full_date) if project?
2023-11-10 20:34:36 +08:00
end
def updated_at
I18n.l(object.updated_at, format: :full_date) if project?
end
2023-11-10 20:34:36 +08:00
def archived_on
I18n.l(object.archived_on, format: :full) if project? && object.archived_on
end
def users
if project?
object.user_assignments.map do |ua|
{
avatar: avatar_path(ua.user, :icon_small),
full_name: ua.user_name_with_role
}
end
end
end
def comments
if project?
@user = scope[:user] || @instance_options[:user]
{
count: object.comments.count,
count_unseen: count_unseen_comments(object, @user)
}
end
end
2023-11-10 20:34:36 +08:00
def urls
2023-12-01 07:01:08 +08:00
urls_list = {
show: if project?
experiments_path(project_id: object, view_mode: object.archived ? 'archived' : 'active')
else
project_folder_path(object, view_mode: object.archived ? 'archived' : 'active')
end,
2023-11-24 18:08:28 +08:00
actions: actions_toolbar_projects_path(items: [{ id: object.id,
type: project? ? 'projects' : 'project_folders' }].to_json)
2023-11-10 20:34:36 +08:00
}
2023-12-01 07:01:08 +08:00
2023-12-05 03:59:16 +08:00
urls_list[:show] = nil if project? && !can_read_project?(object)
2023-12-01 07:01:08 +08:00
2023-12-05 03:59:16 +08:00
urls_list[:update] = if project?
2024-01-16 20:53:41 +08:00
project_path(object)
2023-12-05 03:59:16 +08:00
else
project_folder_path(object)
end
2023-12-11 22:41:03 +08:00
urls_list[:show_access] = access_permissions_project_path(object)
2023-12-05 03:59:16 +08:00
if project? && can_manage_project_users?(object)
2023-12-11 22:41:03 +08:00
urls_list[:update_access] = access_permissions_project_path(object)
2023-12-05 03:59:16 +08:00
urls_list[:new_access] = new_access_permissions_project_path(id: object.id)
urls_list[:create_access] = access_permissions_projects_path(id: object.id)
urls_list[:default_public_user_role_path] =
update_default_public_user_role_access_permissions_project_path(object)
end
2023-12-01 07:01:08 +08:00
urls_list
end
def permissions
{
create_comments: project? ? can_create_project_comments?(object) : false,
manage_users_assignments: project? ? can_manage_project_users?(object) : false
}
end
2023-12-01 07:01:08 +08:00
def folder_info
if folder
2023-12-05 03:59:16 +08:00
I18n.t('projects.index.folder.description', projects_count: object.projects_count,
folders_count: object.folders_count)
2023-12-01 07:01:08 +08:00
end
2023-11-10 20:34:36 +08:00
end
private
def project?
2023-11-24 18:08:28 +08:00
object.instance_of?(Project)
2023-11-10 20:34:36 +08:00
end
end
end