Class: Notification

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper
Defined in:
app/models/notification.rb

Direct Known Subclasses

Message

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#recipientsObject

Returns the recipients of the Notification



81
82
83
# File 'app/models/notification.rb', line 81

def recipients
  @recipients
end

Class Method Details

.notify_all(recipients, subject, body, obj = nil, sanitize_text = true, notification_code = nil) ⇒ Object

Sends a Notification to all the recipients



24
25
26
27
28
29
30
31
# File 'app/models/notification.rb', line 24

def notify_all(recipients,subject,body,obj = nil,sanitize_text = true,notification_code=nil)
  notification = Notification.new({:body => body, :subject => subject})
  notification.recipients = recipients.is_a?(Array) ? recipients : [recipients]
  notification.recipients = notification.recipients.uniq
  notification.notified_object = obj if obj.present?
  notification.notification_code = notification_code if notification_code.present?
  return notification.deliver sanitize_text
end

.successful_delivery?(receipts) ⇒ Boolean

Takes a Receipt or an Array of them and returns true if the delivery was successful or false if some error raised

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/notification.rb', line 35

def successful_delivery? receipts
  case receipts
  when Receipt
    receipts.valid?
    return receipts.errors.empty?
   when Array
     receipts.each(&:valid?)
     return receipts.all? { |t| t.errors.empty? }
   else
     return false
   end      
end

Instance Method Details

#cleanObject

Sanitizes the body and subject



142
143
144
145
146
147
# File 'app/models/notification.rb', line 142

def clean
  unless self.subject.nil?
    self.subject = sanitize self.subject
  end
  self.body = sanitize self.body
end

#deliver(should_clean = true) ⇒ Object

Delivers a Notification. USE NOT RECOMENDED. Use Mailboxer::Models::Message.notify and Notification.notify_all instead.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/notification.rb', line 51

def deliver(should_clean = true)
  self.clean if should_clean
  temp_receipts = Array.new
  #Receiver receipts
  self.recipients.each do |r|
    msg_receipt = Receipt.new
    msg_receipt.notification = self
    msg_receipt.read = false
    msg_receipt.receiver = r
    temp_receipts << msg_receipt      
  end
  temp_receipts.each(&:valid?)
  if temp_receipts.all? { |t| t.errors.empty? }
    temp_receipts.each(&:save!)   #Save receipts
    self.recipients.each do |r|
      #Should send an email?
      if Mailboxer.uses_emails 
        email_to = r.send(Mailboxer.email_method,self)
        unless email_to.blank?
          NotificationMailer.send_email(self,r).deliver
        end
      end
    end
    self.recipients=nil
  end
  return temp_receipts if temp_receipts.size > 1
  return temp_receipts.first
end

#is_trashed?(participant) ⇒ Boolean

Returns if the participant have trashed the Notification

Returns:

  • (Boolean)


109
110
111
112
# File 'app/models/notification.rb', line 109

def is_trashed?(participant)
  return false if participant.nil?
  return self.receipt_for(participant).first.trashed
end

#is_unread?(participant) ⇒ Boolean

Returns if the participant have read the Notification

Returns:

  • (Boolean)


103
104
105
106
# File 'app/models/notification.rb', line 103

def is_unread?(participant)
  return false if participant.nil?
  return !self.receipt_for(participant).first.read
end

#mark_as_read(participant) ⇒ Object

Mark the notification as read



115
116
117
118
# File 'app/models/notification.rb', line 115

def mark_as_read(participant)
  return if participant.nil?
  return self.receipt_for(participant).mark_as_read
end

#mark_as_unread(participant) ⇒ Object

Mark the notification as unread



121
122
123
124
# File 'app/models/notification.rb', line 121

def mark_as_unread(participant)
  return if participant.nil?
  return self.receipt_for(participant).mark_as_unread
end

#move_to_trash(participant) ⇒ Object

Move the notification to the trash



127
128
129
130
# File 'app/models/notification.rb', line 127

def move_to_trash(participant)
  return if participant.nil?
  return self.receipt_for(participant).move_to_trash
end

#objectObject

Returns notified_object. DEPRECATED



150
151
152
153
# File 'app/models/notification.rb', line 150

def object
  warn "DEPRECATION WARNING: use 'notify_object' instead of 'object' to get the object associated with the Notification"
  notified_object
end

#receipt_for(participant) ⇒ Object

Returns the receipt for the participant



93
94
95
# File 'app/models/notification.rb', line 93

def receipt_for(participant)
  return Receipt.notification(self).recipient(participant)
end

#receipts_for(participant) ⇒ Object

Returns the receipt for the participant. Alias for receipt_for(participant)



98
99
100
# File 'app/models/notification.rb', line 98

def receipts_for(participant)
  return receipt_for(participant)
end

#untrash(participant) ⇒ Object

Takes the notification out of the trash



133
134
135
136
# File 'app/models/notification.rb', line 133

def untrash(participant)
  return if participant.nil?
  return self.receipt_for(participant).untrash
end