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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/mailboxer/models/messageable.rb', line 170

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



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/mailboxer/models/messageable.rb', line 128

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



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/mailboxer/models/messageable.rb', line 149

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

#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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mailboxer/models/messageable.rb', line 83

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



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

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)



110
111
112
113
114
115
116
117
118
# File 'lib/mailboxer/models/messageable.rb', line 110

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



99
100
101
# File 'lib/mailboxer/models/messageable.rb', line 99

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



227
228
229
230
231
232
233
234
235
236
237
# File 'lib/mailboxer/models/messageable.rb', line 227

def search_messages(query)
  if Mailboxer.search_engine == :pg_search
    Mailboxer::Receipt.search(query).where(receiver_id: self.id).map(&:conversation).uniq
  else
    @search = Mailboxer::Receipt.search do
      fulltext query
      with :receiver_id, self.id
    end
    @search.results.map { |r| r.conversation }.uniq
  end
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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mailboxer/models/messageable.rb', line 60

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



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/mailboxer/models/messageable.rb', line 193

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



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

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