Class: Mailboxer::Notification

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/mailboxer/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



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

def recipients
  @recipients
end

Class Method Details

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

Sends a Notification to all the recipients



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

def notify_all(recipients, subject, body, obj = nil, sanitize_text = true, notification_code=nil, send_mail=true, sender=nil)
  notification = Mailboxer::NotificationBuilder.new({
    :recipients        => recipients,
    :subject           => subject,
    :body              => body,
    :notified_object   => obj,
    :notification_code => notification_code,
    :sender            => sender
  }).build

  notification.deliver sanitize_text, send_mail
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)


50
51
52
53
54
55
56
57
58
59
# File 'app/models/mailboxer/notification.rb', line 50

def successful_delivery? receipts
  case receipts
  when Mailboxer::Receipt
    receipts.valid?
  when Array
    receipts.all?(&:valid?)
  else
    false
  end
end

Instance Method Details

#cleanObject

Sanitizes the body and subject



168
169
170
171
# File 'app/models/mailboxer/notification.rb', line 168

def clean
  self.subject = sanitize(subject) if subject
  self.body    = sanitize(body)
end

#deliver(should_clean = true, send_mail = true) ⇒ Object

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/mailboxer/notification.rb', line 81

def deliver(should_clean = true, send_mail = true)
  clean if should_clean
  temp_receipts = recipients.map do |r|
    receipts.build(receiver: r, mailbox_type: nil, is_read: false)
  end

  if valid?
    Mailboxer::MailDispatcher.new(self, temp_receipts).call if send_mail
    save!

    self.recipients = nil
  end

  return temp_receipts if temp_receipts.size > 1
  temp_receipts.first
end

#expireObject



73
74
75
76
77
# File 'app/models/mailboxer/notification.rb', line 73

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

#expire!Object



66
67
68
69
70
71
# File 'app/models/mailboxer/notification.rb', line 66

def expire!
  unless expired?
    expire
    save
  end
end

#expired?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/models/mailboxer/notification.rb', line 62

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

#is_deleted?(participant) ⇒ Boolean

Returns if the participant have deleted the Notification

Returns:

  • (Boolean)


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

def is_deleted?(participant)
  return false if participant.nil?
  return receipt_for(participant).first.deleted
end

#is_read?(participant) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#is_trashed?(participant) ⇒ Boolean

Returns if the participant have trashed the Notification

Returns:

  • (Boolean)


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

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

#is_unread?(participant) ⇒ Boolean

Returns if the participant have read the Notification

Returns:

  • (Boolean)


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

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

#mark_as_deleted(participant) ⇒ Object

Mark the notification as deleted for one of the participant



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

def mark_as_deleted(participant)
  return if participant.nil?
  return receipt_for(participant).mark_as_deleted
end

#mark_as_read(participant) ⇒ Object

Mark the notification as read



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

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

#mark_as_unread(participant) ⇒ Object

Mark the notification as unread



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

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

#move_to_trash(participant) ⇒ Object

Move the notification to the trash



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

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

#objectObject

Returns notified_object. DEPRECATED



174
175
176
177
# File 'app/models/mailboxer/notification.rb', line 174

def object
  warn "DEPRECATION WARNING: use 'notified_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



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

def receipt_for(participant)
  Mailboxer::Receipt.notification(self).recipient(participant)
end

#receipts_for(participant) ⇒ Object

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



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

def receipts_for(participant)
  receipt_for(participant)
end

#sanitize(text) ⇒ Object



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

def sanitize(text)
  ::Mailboxer::Cleaner.instance.sanitize(text)
end

#untrash(participant) ⇒ Object

Takes the notification out of the trash



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

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