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



84
85
86
# File 'app/models/notification.rb', line 84

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



27
28
29
30
31
32
33
34
# File 'app/models/notification.rb', line 27

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)


38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/notification.rb', line 38

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



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

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.



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
79
80
81
# File 'app/models/notification.rb', line 54

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

#is_read?(participant) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#is_trashed?(participant) ⇒ Boolean

Returns if the participant have trashed the Notification

Returns:

  • (Boolean)


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

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)


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

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



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

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



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

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



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

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

#objectObject

Returns notified_object. DEPRECATED



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

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



96
97
98
# File 'app/models/notification.rb', line 96

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)



101
102
103
# File 'app/models/notification.rb', line 101

def receipts_for(participant)
  return receipt_for(participant)
end

#untrash(participant) ⇒ Object

Takes the notification out of the trash



140
141
142
143
# File 'app/models/notification.rb', line 140

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