Class: Message

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::ConfigurableMailer

#get_mailer

Instance Attribute Details

#recipientsObject

AJOUT DEPUIS NOTIFICATION Returns the recipients of the message



3
4
5
# File 'app/models/message.rb', line 3

def recipients
  @recipients
end

Class Method Details

.on_deliver(callback_method) ⇒ Object

Sets the on deliver callback method.



48
49
50
# File 'app/models/message.rb', line 48

def on_deliver(callback_method)
  self.on_deliver_callback = callback_method
end

Instance Method Details

#cleanObject

Sanitizes the body and subject



193
194
195
196
197
198
# File 'app/models/message.rb', line 193

def clean
  unless self.subject.nil?
    self.subject = sanitize self.subject
  end
  self.body = sanitize self.body
end

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

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/message.rb', line 80

def deliver(reply = false, should_clean = true)
  self.clean if should_clean

  #Receiver receipts
  temp_receipts = recipients.map { |r| build_receipt(r, 'inbox') }

  #Sender receipt
  sender_receipt = build_receipt(sender, 'sentbox', true)
  temp_receipts << sender_receipt

  temp_receipts.each(&:valid?)
  if temp_receipts.all? { |t| t.errors.empty? }
    temp_receipts.each(&:save!) 	#Save receipts
    #Should send an email?
    if Mailboxer.uses_emails
      if Mailboxer.mailer_wants_array
        get_mailer.send_email(self, recipients).deliver
      else
        recipients.each do |recipient|
          email_to = recipient.send(Mailboxer.email_method, self)
          get_mailer.send_email(self, recipient).deliver if email_to.present?
        end
      end
    end
    if reply
      self.conversation.touch
    end
    self.recipients=nil
    self.on_deliver_callback.call(self) unless self.on_deliver_callback.nil?
  end
  sender_receipt
end

#is_deleted?(participant) ⇒ Boolean

Returns if the participant have deleted the message

Returns:

  • (Boolean)


155
156
157
158
# File 'app/models/message.rb', line 155

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

#is_read?(participant) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#is_trashed?(participant) ⇒ Boolean

Returns if the participant have trashed the message

Returns:

  • (Boolean)


149
150
151
152
# File 'app/models/message.rb', line 149

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

#is_unread?(participant) ⇒ Boolean

Returns if the participant have read the message

Returns:

  • (Boolean)


139
140
141
142
# File 'app/models/message.rb', line 139

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

#mark_as_deleted(participant) ⇒ Object

Mark the message as deleted for one of the participant



185
186
187
188
# File 'app/models/message.rb', line 185

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

#mark_as_read(participant) ⇒ Object

Mark the message as read



161
162
163
164
# File 'app/models/message.rb', line 161

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

#mark_as_unread(participant) ⇒ Object

Mark the message as unread



167
168
169
170
# File 'app/models/message.rb', line 167

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

#move_to_trash(participant) ⇒ Object

Move the message to the trash



173
174
175
176
# File 'app/models/message.rb', line 173

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

#objectObject

Returns notified_object. DEPRECATED



201
202
203
204
# File 'app/models/message.rb', line 201

def object
  warn "DEPRECATION WARNING: use 'notify_object' instead of 'object' to get the object associated with the Message"
  notified_object
end

#receipt_for(participant) ⇒ Object

Returns the receipt for the participant



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

def receipt_for(participant)
  Receipt.message(self).recipient(participant)
end

#receipts_for(participant) ⇒ Object

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



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

def receipts_for(participant)
  receipt_for(participant)
end

#untrash(participant) ⇒ Object

Takes the message out of the trash



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

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