2014-12-13 10:30:10 -08:00
|
|
|
class Notification < ActiveRecord::Base
|
2014-12-14 05:34:51 -08:00
|
|
|
belongs_to :recipient, class_name: 'User'
|
2014-12-14 05:49:14 -08:00
|
|
|
belongs_to :target, polymorphic: true
|
2014-12-14 05:34:51 -08:00
|
|
|
|
|
|
|
class << self
|
|
|
|
def for(recipient, options={})
|
|
|
|
self.where(options.merge!(recipient: recipient)).order(:updated_at).reverse_order
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify(recipient, target)
|
|
|
|
return nil unless target.respond_to? :notification_type
|
|
|
|
|
|
|
|
notif_type = target.notification_type
|
|
|
|
return nil unless notif_type
|
|
|
|
|
|
|
|
make_notification(recipient, target, notif_type)
|
|
|
|
end
|
|
|
|
|
2014-12-14 06:42:37 -08:00
|
|
|
def denotify(recipient, target)
|
|
|
|
return nil unless target.respond_to? :notification_type
|
|
|
|
|
|
|
|
notif_type = target.notification_type
|
|
|
|
return nil unless notif_type
|
|
|
|
|
|
|
|
notif_type.find_by(recipient: recipient, target: target).destroy
|
|
|
|
end
|
|
|
|
|
2014-12-14 05:34:51 -08:00
|
|
|
private
|
|
|
|
|
|
|
|
def make_notification(recipient, target, notification_type)
|
|
|
|
n = notification_type.new(target: target,
|
|
|
|
recipient: recipient,
|
|
|
|
new: true)
|
|
|
|
n.save!
|
|
|
|
n
|
|
|
|
end
|
|
|
|
end
|
2014-12-13 10:30:10 -08:00
|
|
|
end
|