2023-10-17 18:02:55 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class NotificationSerializer < ActiveModel::Serializer
|
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
|
|
|
|
attributes :id, :title, :message, :created_at, :read_at, :type, :breadcrumbs, :checked, :today
|
|
|
|
|
|
|
|
def title
|
|
|
|
object.to_notification.title
|
|
|
|
end
|
|
|
|
|
|
|
|
def breadcrumbs
|
|
|
|
subject = object.to_notification.subject
|
|
|
|
generate_breadcrumbs(subject, []) if subject
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
|
|
|
object.to_notification.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def created_at
|
|
|
|
I18n.l(object.created_at, format: :full)
|
|
|
|
end
|
|
|
|
|
|
|
|
def today
|
|
|
|
object.created_at.today?
|
|
|
|
end
|
|
|
|
|
|
|
|
def checked
|
|
|
|
object.read_at.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def generate_breadcrumbs(subject, breadcrumbs)
|
2023-12-11 18:28:24 +08:00
|
|
|
return [] if subject.is_a?(NonExistantRecord)
|
|
|
|
|
2023-10-17 18:02:55 +08:00
|
|
|
case subject
|
|
|
|
when Project
|
|
|
|
parent = subject.team
|
|
|
|
url = project_path(subject)
|
|
|
|
when Experiment
|
|
|
|
parent = subject.project
|
2023-11-30 17:54:40 +08:00
|
|
|
url = my_modules_experiment_path(subject)
|
2023-10-17 18:02:55 +08:00
|
|
|
when MyModule
|
|
|
|
parent = subject.experiment
|
|
|
|
url = protocols_my_module_path(subject)
|
|
|
|
when Protocol
|
|
|
|
if subject.in_repository?
|
|
|
|
parent = subject.team
|
|
|
|
url = protocol_path(subject)
|
|
|
|
else
|
|
|
|
parent = subject.my_module
|
|
|
|
url = protocols_my_module_path(subject.my_module)
|
|
|
|
end
|
|
|
|
when Result
|
|
|
|
parent = subject.my_module
|
|
|
|
url = my_module_results_path(subject.my_module)
|
|
|
|
when ProjectFolder
|
|
|
|
parent = subject.team
|
|
|
|
url = project_folder_path(subject)
|
|
|
|
when RepositoryBase
|
|
|
|
parent = subject.team
|
|
|
|
url = repository_path(subject)
|
|
|
|
when RepositoryRow
|
|
|
|
parent = subject.team
|
|
|
|
url = repository_path(subject.repository)
|
2023-12-12 19:11:52 +08:00
|
|
|
when Report
|
|
|
|
parent = subject.team
|
|
|
|
url = reports_path(
|
|
|
|
preview_report_id: subject.id,
|
|
|
|
preview_type: object.params[:report_type],
|
|
|
|
team_id: subject.team.id
|
|
|
|
)
|
2023-10-17 18:02:55 +08:00
|
|
|
when LabelTemplate
|
|
|
|
parent = subject.team
|
|
|
|
url = label_template_path(subject)
|
|
|
|
when Team
|
|
|
|
parent = nil
|
2023-12-11 18:27:58 +08:00
|
|
|
url = projects_path(team: subject.id)
|
2023-10-17 18:02:55 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
breadcrumbs << { name: subject.name, url: url } if subject.name.present?
|
|
|
|
|
|
|
|
if parent
|
|
|
|
generate_breadcrumbs(parent, breadcrumbs)
|
|
|
|
else
|
|
|
|
breadcrumbs.reverse
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|