Class: Message

Inherits:
Notification show all
Defined in:
app/models/message.rb

Instance Attribute Summary

Attributes inherited from Notification

#recipients

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Notification

#clean, #is_trashed?, #is_unread?, #mark_as_read, #mark_as_unread, #move_to_trash, notify_all, #object, #receipt_for, #receipts_for, successful_delivery?, #untrash

Class Method Details

.on_deliver(callback_method) ⇒ Object

Sets the on deliver callback method.



16
17
18
# File 'app/models/message.rb', line 16

def on_deliver(callback_method)
  self.on_deliver_callback = callback_method
end

Instance Method Details

#deliver(reply = false, should_clean = true) ⇒ Object

Delivers a Message. USE NOT RECOMENDED. Use Mailboxer::Models::Message.send_message instead.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/message.rb', line 23

def deliver(reply = false, 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
    msg_receipt.mailbox_type = "inbox"
    temp_receipts << msg_receipt
  end
  #Sender receipt
  sender_receipt = Receipt.new
  sender_receipt.notification = self
  sender_receipt.read = true
  sender_receipt.receiver = self.sender
  sender_receipt.mailbox_type = "sentbox"
  temp_receipts << sender_receipt

  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?
          MessageMailer.send_email(self,r).deliver
        end
      end
    end
    if reply
      self.conversation.update_attribute(:updated_at, Time.now)
    end
    self.recipients=nil
  self.on_deliver_callback.call(self) unless self.on_deliver_callback.nil?
  end
  return sender_receipt
end