Class: Notification

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

Direct Known Subclasses

Message

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::ConfigurableMailer

#get_mailer

Instance Attribute Details

#recipientsObject

Returns the recipients of the Notification



106
107
108
# File 'app/models/notification.rb', line 106

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



32
33
34
35
36
37
38
39
# File 'app/models/notification.rb', line 32

def notify_all(recipients,subject,body,obj = nil,sanitize_text = true,notification_code=nil)
  notification = Notification.new({:body => body, :subject => subject})
  notification.recipients = recipients.respond_to?(:each) ? recipients : [recipients]
  notification.recipients = notification.recipients.uniq if recipients.respond_to?(: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)


43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/notification.rb', line 43

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



171
172
173
174
175
176
# File 'app/models/notification.rb', line 171

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.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/notification.rb', line 76

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.is_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?
          get_mailer.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

#expireObject



68
69
70
71
72
# File 'app/models/notification.rb', line 68

def expire
  unless self.expired?
    self.expires = Time.now - 1.second
  end
end

#expire!Object



61
62
63
64
65
66
# File 'app/models/notification.rb', line 61

def expire!
  unless self.expired?
    self.expire
    self.save
  end
end

#expired?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/notification.rb', line 57

def expired?
  return self.expires.present? && (self.expires < Time.now)
end

#is_read?(participant) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_read?(participant)
  !self.is_unread?(participant)
end

#is_trashed?(participant) ⇒ Boolean

Returns if the participant have trashed the Notification

Returns:

  • (Boolean)


138
139
140
141
# File 'app/models/notification.rb', line 138

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)


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

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

#mark_as_read(participant) ⇒ Object

Mark the notification as read



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

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



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

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



156
157
158
159
# File 'app/models/notification.rb', line 156

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

#objectObject

Returns notified_object. DEPRECATED



179
180
181
182
# File 'app/models/notification.rb', line 179

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



118
119
120
# File 'app/models/notification.rb', line 118

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)



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

def receipts_for(participant)
  return receipt_for(participant)
end

#untrash(participant) ⇒ Object

Takes the notification out of the trash



162
163
164
165
# File 'app/models/notification.rb', line 162

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