Module: Mailboxer::Models::Messageable

Extended by:
ActiveSupport::Concern
Defined in:
lib/mailboxer/models/messageable.rb

Defined Under Namespace

Modules: ActiveRecord

Instance Method Summary collapse

Instance Method Details

#mailboxObject

Gets the mailbox of the messageable



48
49
50
51
52
# File 'lib/mailboxer/models/messageable.rb', line 48

def mailbox
  @mailbox = Mailbox.new(self) if @mailbox.nil?
  @mailbox.type = :all
  @mailbox
end

#mark_as_deleted(obj) ⇒ Object

Mark the object as deleted for messageable.

Object can be:

  • A Receipt

  • A Notification

  • A Message

  • A Conversation

  • An array with any of them



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/mailboxer/models/messageable.rb', line 158

def mark_as_deleted(obj)
  case obj
    when Receipt
      return obj.mark_as_deleted if obj.receiver == self
    when Message#, Notification
      obj.mark_as_deleted(self)
    when Conversation
      obj.mark_as_deleted(self)
    when Array
      obj.map{ |sub_obj| mark_as_deleted(sub_obj) }
    else
      return nil
  end
end

#mark_as_read(obj) ⇒ Object

Mark the object as read for messageable.

Object can be:

  • A Receipt

  • A Message

  • A Notification

  • A Conversation

  • An array with any of them



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mailboxer/models/messageable.rb', line 116

def mark_as_read(obj)
  case obj
  when Receipt
    obj.mark_as_read if obj.receiver == self
  when Message#, Notification
    obj.mark_as_read(self)
  when Conversation
    obj.mark_as_read(self)
  when Array
    obj.map{ |sub_obj| mark_as_read(sub_obj) }
  end
end

#mark_as_unread(obj) ⇒ Object

Mark the object as unread for messageable.

Object can be:

  • A Receipt

  • A Message

  • A Notification

  • A Conversation

  • An array with any of them



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mailboxer/models/messageable.rb', line 137

def mark_as_unread(obj)
  case obj
  when Receipt
    obj.mark_as_unread if obj.receiver == self
  when Message#, Notification
    obj.mark_as_unread(self)
  when Conversation
    obj.mark_as_unread(self)
  when Array
    obj.map{ |sub_obj| mark_as_unread(sub_obj) }
  end
end

#reply(conversation, recipients, reply_body, subject = nil, sanitize_text = true, attachment = nil) ⇒ Object

Basic reply method. USE NOT RECOMENDED. Use reply_to_sender, reply_to_all and reply_to_conversation instead.



76
77
78
79
80
81
82
83
84
# File 'lib/mailboxer/models/messageable.rb', line 76

def reply(conversation, recipients, reply_body, subject=nil, sanitize_text=true, attachment=nil)
  subject = subject || "RE: #{conversation.subject}"
  response = messages.new({:body => reply_body, :subject => subject, :attachment => attachment})
  response.conversation = conversation
  response.recipients = recipients.is_a?(Array) ? recipients : [recipients]
  response.recipients = response.recipients.uniq
  response.recipients.delete(self)
  response.deliver true, sanitize_text
end

#reply_to_all(receipt, reply_body, subject = nil, sanitize_text = true, attachment = nil) ⇒ Object

Replies to all the recipients of the message in the conversation



92
93
94
# File 'lib/mailboxer/models/messageable.rb', line 92

def reply_to_all(receipt, reply_body, subject=nil, sanitize_text=true, attachment=nil)
  reply(receipt.conversation, receipt.message.recipients, reply_body, subject, sanitize_text, attachment)
end

#reply_to_conversation(conversation, reply_body, subject = nil, should_untrash = true, sanitize_text = true, attachment = nil) ⇒ Object

Replies to all the recipients of the last message in the conversation and untrash any trashed message by messageable if should_untrash is set to true (this is so by default)



98
99
100
101
102
103
104
105
106
# File 'lib/mailboxer/models/messageable.rb', line 98

def reply_to_conversation(conversation, reply_body, subject=nil, should_untrash=true, sanitize_text=true, attachment=nil)
  #move conversation to inbox if it is currently in the trash and should_untrash parameter is true.
  if should_untrash && mailbox.is_trashed?(conversation)
    mailbox.receipts_for(conversation).untrash
    mailbox.receipts_for(conversation).mark_as_not_deleted
  end

  reply(conversation, conversation.last_message.recipients, reply_body, subject, sanitize_text, attachment)
end

#reply_to_sender(receipt, reply_body, subject = nil, sanitize_text = true, attachment = nil) ⇒ Object

Replies to the sender of the message in the conversation



87
88
89
# File 'lib/mailboxer/models/messageable.rb', line 87

def reply_to_sender(receipt, reply_body, subject=nil, sanitize_text=true, attachment=nil)
  reply(receipt.conversation, receipt.message.sender, reply_body, subject, sanitize_text, attachment)
end

#search_messages(query) ⇒ Object



215
216
217
218
219
220
221
222
# File 'lib/mailboxer/models/messageable.rb', line 215

def search_messages(query)
  @search = Receipt.search do
    fulltext query
    with :receiver_id, self.id
  end

  @search.results.map { |r| r.conversation }.uniq
end

#send_message(recipients, msg_body, subject = nil, sanitize_text = true, attachment = nil, message_timestamp = Time.now) ⇒ Object

Sends a messages, starting a new conversation, with the messageable as originator



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mailboxer/models/messageable.rb', line 61

def send_message(recipients, msg_body, subject=nil, sanitize_text=true, attachment=nil, message_timestamp = Time.now)
  convo = Conversation.new({:subject => subject})
  convo.created_at = message_timestamp
  convo.updated_at = message_timestamp
  message = messages.new({:body => msg_body, :subject => subject, :attachment => attachment})
  message.created_at = message_timestamp
  message.updated_at = message_timestamp
  message.conversation = convo
  message.recipients = recipients.is_a?(Array) ? recipients : [recipients]
  message.recipients = message.recipients.uniq
  message.deliver false, sanitize_text
end

#trash(obj) ⇒ Object

Mark the object as trashed for messageable.

Object can be:

  • A Receipt

  • A Message

  • A Notification

  • A Conversation

  • An array with any of them



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mailboxer/models/messageable.rb', line 181

def trash(obj)
  case obj
  when Receipt
    obj.move_to_trash if obj.receiver == self
  when Message#, Notification
    obj.move_to_trash(self)
  when Conversation
    obj.move_to_trash(self)
  when Array
    obj.map{ |sub_obj| trash(sub_obj) }
  end
end

#untrash(obj) ⇒ Object

Mark the object as not trashed for messageable.

Object can be:

  • A Receipt

  • A Message

  • A Notification

  • A Conversation

  • An array with any of them



202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/mailboxer/models/messageable.rb', line 202

def untrash(obj)
  case obj
  when Receipt
    obj.untrash if obj.receiver == self
  when Message#, Notification
    obj.untrash(self)
  when Conversation
    obj.untrash(self)
  when Array
    obj.map{ |sub_obj| untrash(sub_obj) }
  end
end