1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-02 13:24:20 +01:00
fab-manager/app/models/notification.rb

61 lines
1.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# Notification is an in-system alert that is shown to a specific user until it is marked as read.
2020-03-25 10:16:47 +01:00
class Notification < ApplicationRecord
belongs_to :notification_type
belongs_to :receiver, polymorphic: true
belongs_to :attached_object, polymorphic: true
# This scope filter a user's in system (push) notifications :
# It fetch his notifications where no notification preference is made,
# or if this preference specify that the user accepts in system notification
scope :delivered_in_system, lambda { |user|
left_outer_joins(notification_type: :notification_preferences)
.where(<<-SQL.squish, user.id)
(notification_preferences.user_id = ? AND notification_preferences.in_system IS TRUE)
OR notification_preferences.id IS NULL
SQL
}
validates :receiver_id,
:receiver_type,
:attached_object_id,
:attached_object_type,
:notification_type_id,
presence: true
def notification_type
NotificationType.find(notification_type_id).name
end
def mark_as_read
update(is_read: true)
end
def mark_as_send
update(is_send: true)
end
def deliver_now
NotificationsMailer.send_mail_by(self).deliver_now if save
end
def deliver_later
NotificationsMailer.send_mail_by(self).deliver_later if save
end
2016-03-23 18:39:41 +01:00
def deliver_with_preferences(user, notification_type)
preference = NotificationPreference.find_by(notification_type: notification_type, user: user)
# Set as read if user do not want push notifications
self.is_read = true if preference && preference.in_system == false
# Save notification if user do not want email notifications ; else, deliver.
preference && preference.email == false ? save : deliver_later
end
2016-03-23 18:39:41 +01:00
def get_meta_data(key)
meta_data.try(:[], key.to_s)
end
2015-05-05 03:10:25 +02:00
end