scinote-web/app/models/activity.rb

129 lines
3.6 KiB
Ruby
Raw Normal View History

2017-06-23 21:19:08 +08:00
class Activity < ApplicationRecord
include InputSanitizeHelper
2016-10-03 17:37:29 +08:00
after_create :generate_notification
2016-02-12 23:52:43 +08:00
enum type_of: [
:create_project,
:rename_project,
:change_project_visibility,
:archive_project,
:restore_project,
:assign_user_to_project,
:change_user_role_on_project,
:unassign_user_from_project,
:create_module,
:clone_module,
:archive_module,
:restore_module,
:change_module_description,
:assign_user_to_module,
:unassign_user_from_module,
:create_step,
:destroy_step,
:add_comment_to_step,
:complete_step,
:uncomplete_step,
:check_step_checklist_item,
:uncheck_step_checklist_item,
:edit_step,
:add_result,
:add_comment_to_result,
:archive_result,
:edit_result,
2016-11-02 16:18:06 +08:00
:create_experiment,
:edit_experiment,
:archive_experiment,
2016-08-16 14:49:24 +08:00
:clone_experiment,
:move_experiment,
:add_comment_to_project,
:edit_project_comment,
:delete_project_comment,
:add_comment_to_module,
:edit_module_comment,
:delete_module_comment,
:edit_step_comment,
:delete_step_comment,
:edit_result_comment,
:delete_result_comment,
2016-09-29 21:30:55 +08:00
:destroy_result,
:start_edit_wopi_file,
:unlock_wopi_file,
2016-11-02 16:18:06 +08:00
:load_protocol_from_file,
:load_protocol_from_repository,
:revert_protocol,
:create_report,
:delete_report,
:edit_report,
:assign_sample,
:unassign_sample,
:complete_task,
2017-06-06 23:35:29 +08:00
:uncomplete_task,
:assign_repository_record,
:unassign_repository_record
2016-02-12 23:52:43 +08:00
]
validates :type_of, presence: true
2017-06-28 22:44:47 +08:00
belongs_to :project, inverse_of: :activities
2017-06-28 21:21:32 +08:00
belongs_to :experiment, inverse_of: :activities, optional: true
belongs_to :my_module, inverse_of: :activities, optional: true
2017-06-28 22:44:47 +08:00
belongs_to :user, inverse_of: :activities
2016-10-03 17:37:29 +08:00
private
def generate_notification
2016-11-02 20:32:15 +08:00
if %w(assign_user_to_project
assign_user_to_module unassign_user_from_module).include? type_of
2016-10-03 17:37:29 +08:00
notification_type = :assignment
else
notification_type = :recent_changes
end
project_m = "<a href='#{Rails
.application
.routes
.url_helpers
.project_path(project)}'>
#{project.name}</a>"
2017-04-21 00:14:12 +08:00
if experiment
experiment_m = "| #{I18n.t('search.index.experiment')}
<a href='#{Rails
.application
.routes
.url_helpers
.canvas_experiment_path(experiment)}'>
#{experiment.name}</a>"
end
if my_module
task_m = "| #{I18n.t('search.index.module')}
<a href='#{Rails
.application
.routes
.url_helpers
.protocols_my_module_path(my_module)}'>
#{my_module.name}</a>"
end
2016-10-03 17:37:29 +08:00
notification = Notification.create(
type_of: notification_type,
title: sanitize_input(message, %w(strong a)),
message: sanitize_input(
"#{I18n.t('search.index.project')}
#{project_m} #{experiment_m} #{task_m}",
%w(strong a)
),
2016-10-03 17:37:29 +08:00
generator_user_id: user.id
)
project.users.each do |project_user|
2016-10-05 15:46:18 +08:00
next if project_user == user
2016-10-04 21:52:48 +08:00
next if !project_user.assignments_notification &&
notification.type_of == 'assignment'
next if !project_user.recent_notification &&
notification.type_of == 'recent_changes'
2016-10-03 17:37:29 +08:00
UserNotification.create(notification: notification, user: project_user)
end
end
2016-02-12 23:52:43 +08:00
end