Module: Mailboxer::Models::Messageable

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

Defined Under Namespace

Modules: ActiveRecordExtension

Instance Method Summary collapse

Instance Method Details

#mailboxObject

Gets the mailbox of the messageable



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

def mailbox
  @mailbox ||= Mailboxer::Mailbox.new(self)
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



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/mailboxer/models/messageable.rb', line 175

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



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/mailboxer/models/messageable.rb', line 133

def mark_as_read(obj)
  case obj
  when Mailboxer::Receipt
    obj.mark_as_read if obj.receiver == self
  when Mailboxer::Message, Mailboxer::Notification
    obj.mark_as_read(self)
  when Mailboxer::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



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mailboxer/models/messageable.rb', line 154

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

#notify(subject, body, obj = nil, sanitize_text = true, notification_code = nil, send_mail = true) ⇒ Object

Sends a notification to the messageable



59
60
61
# File 'lib/mailboxer/models/messageable.rb', line 59

def notify(subject,body,obj = nil,sanitize_text=true,notification_code=nil,send_mail=true)
  Mailboxer::Notification.notify_all([self],subject,body,obj,sanitize_text,notification_code,send_mail)
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.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mailboxer/models/messageable.rb', line 88

def reply(conversation, recipients, reply_body, subject=nil, sanitize_text=true, attachment=nil)
  subject = subject || "#{conversation.subject}"
  response = Mailboxer::MessageBuilder.new({
    :sender       => self,
    :conversation => conversation,
    :recipients   => recipients,
    :body         => reply_body,
    :subject      => subject,
    :attachment   => attachment
  }).build

  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



109
110
111
# File 'lib/mailboxer/models/messageable.rb', line 109

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)



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

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



104
105
106
# File 'lib/mailboxer/models/messageable.rb', line 104

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



232
233
234
235
236
237
238
239
# File 'lib/mailboxer/models/messageable.rb', line 232

def search_messages(query)
  @search = Mailboxer::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, sanitize_text = true, attachment = nil, message_timestamp = Time.now) ⇒ Object

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mailboxer/models/messageable.rb', line 65

def send_message(recipients, msg_body, subject, sanitize_text=true, attachment=nil, message_timestamp = Time.now)
  convo = Mailboxer::ConversationBuilder.new({
    :subject    => subject,
    :created_at => message_timestamp,
    :updated_at => message_timestamp
  }).build

  message = Mailboxer::MessageBuilder.new({
    :sender       => self,
    :conversation => convo,
    :recipients   => recipients,
    :body         => msg_body,
    :subject      => subject,
    :attachment   => attachment,
    :created_at   => message_timestamp,
    :updated_at   => message_timestamp
  }).build

  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



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/mailboxer/models/messageable.rb', line 198

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

#unread_inbox_countObject

Get number of unread messages



54
55
56
# File 'lib/mailboxer/models/messageable.rb', line 54

def unread_inbox_count
  mailbox.inbox(unread: true).count
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



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/mailboxer/models/messageable.rb', line 219

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