Class: Notification
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Notification
- Includes:
- ActionView::Helpers::SanitizeHelper, Concerns::ConfigurableMailer
- Defined in:
- app/models/notification.rb
Direct Known Subclasses
Instance Attribute Summary (collapse)
-
- (Object) recipients
Returns the recipients of the Notification.
Class Method Summary (collapse)
-
+ (Object) notify_all(recipients, subject, body, obj = nil, sanitize_text = true, notification_code = nil)
Sends a Notification to all the recipients.
-
+ (Boolean) successful_delivery?(receipts)
Takes a Receipt or an Array of them and returns true if the delivery was successful or false if some error raised.
Instance Method Summary (collapse)
-
- (Object) clean
Sanitizes the body and subject.
-
- (Object) deliver(should_clean = true)
Delivers a Notification.
- - (Object) expire
- - (Object) expire!
- - (Boolean) expired?
- - (Boolean) is_read?(participant)
-
- (Boolean) is_trashed?(participant)
Returns if the participant have trashed the Notification.
-
- (Boolean) is_unread?(participant)
Returns if the participant have read the Notification.
-
- (Object) mark_as_read(participant)
Mark the notification as read.
-
- (Object) mark_as_unread(participant)
Mark the notification as unread.
-
- (Object) move_to_trash(participant)
Move the notification to the trash.
-
- (Object) object
Returns notified_object.
-
- (Object) receipt_for(participant)
Returns the receipt for the participant.
-
- (Object) receipts_for(participant)
Returns the receipt for the participant.
-
- (Object) untrash(participant)
Takes the notification out of the trash.
Methods included from Concerns::ConfigurableMailer
Instance Attribute Details
- (Object) recipients
Returns the recipients of the Notification
106 107 108 |
# File 'app/models/notification.rb', line 106 def recipients @recipients end |
Class Method Details
+ (Object) notify_all(recipients, subject, body, obj = nil, sanitize_text = true, notification_code = nil)
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 |
+ (Boolean) successful_delivery?(receipts)
Takes a Receipt or an Array of them and returns true if the delivery was successful or false if some error raised
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
- (Object) clean
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 |
- (Object) deliver(should_clean = true)
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 |
- (Object) expire
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 |
- (Object) expire!
61 62 63 64 65 66 |
# File 'app/models/notification.rb', line 61 def expire! unless self.expired? self.expire self.save end end |
- (Boolean) expired?
57 58 59 |
# File 'app/models/notification.rb', line 57 def expired? return self.expires.present? && (self.expires < Time.now) end |
- (Boolean) is_read?(participant)
133 134 135 |
# File 'app/models/notification.rb', line 133 def is_read?(participant) !self.is_unread?(participant) end |
- (Boolean) is_trashed?(participant)
Returns if the participant have trashed the Notification
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 |
- (Boolean) is_unread?(participant)
Returns if the participant have read the Notification
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 |
- (Object) mark_as_read(participant)
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 |
- (Object) mark_as_unread(participant)
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 |
- (Object) move_to_trash(participant)
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 |
- (Object) object
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 |
- (Object) receipt_for(participant)
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 |
- (Object) receipts_for(participant)
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 |
- (Object) untrash(participant)
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 |