2021-07-28 19:14:07 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 21:19:08 +08:00
|
|
|
class Notification < ApplicationRecord
|
2016-11-15 22:56:32 +08:00
|
|
|
has_many :user_notifications, inverse_of: :notification, dependent: :destroy
|
2016-09-28 20:18:52 +08:00
|
|
|
has_many :users, through: :user_notifications
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :generator_user, class_name: 'User', optional: true
|
2016-09-29 20:49:58 +08:00
|
|
|
|
2016-11-08 18:46:09 +08:00
|
|
|
enum type_of: Extends::NOTIFICATIONS_TYPES
|
2016-10-03 14:20:23 +08:00
|
|
|
|
2021-02-25 14:43:04 +08:00
|
|
|
def create_user_notification(user)
|
|
|
|
return if user == generator_user
|
2021-07-28 19:14:07 +08:00
|
|
|
return unless can_send_to_user?(user)
|
|
|
|
return unless user.enabled_notifications_for?(type_of.to_sym, :web)
|
|
|
|
|
|
|
|
user_notifications.create!(user: user)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2021-02-25 14:43:04 +08:00
|
|
|
|
2021-07-28 19:14:07 +08:00
|
|
|
def can_send_to_user?(_user)
|
|
|
|
true # overridable send permission method
|
2021-02-25 14:43:04 +08:00
|
|
|
end
|
2016-09-28 20:18:52 +08:00
|
|
|
end
|