module Mailboxer::Models::Messageable

Public Instance Methods

mailbox() click to toggle source

Gets the mailbox of the messageable

# File lib/mailboxer/models/messageable.rb, line 49
def mailbox
  @mailbox ||= Mailboxer::Mailbox.new(self)
end
mark_as_deleted(obj) click to toggle source

Mark the object as deleted for messageable.

Object can be:

  • A Receipt

  • A Notification

  • A Message

  • A Conversation

  • An array with any of them

# 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) click to toggle source

Mark the object as read for messageable.

Object can be:

  • A Receipt

  • A Message

  • A Notification

  • A Conversation

  • An array with any of them

# 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) click to toggle source

Mark the object as unread for messageable.

Object can be:

  • A Receipt

  • A Message

  • A Notification

  • A Conversation

  • An array with any of them

# 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) click to toggle source

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

# 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) click to toggle source

Replies to all the recipients of the message in the conversation

# 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) click to toggle source

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)

# 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) click to toggle source

Replies to the sender of the message in the conversation

# 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) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source

Mark the object as trashed for messageable.

Object can be:

  • A Receipt

  • A Message

  • A Notification

  • A Conversation

  • An array with any of them

# 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_count() click to toggle source

Get number of unread messages

# File lib/mailboxer/models/messageable.rb, line 54
def unread_inbox_count
  mailbox.inbox(unread: true).count
end
untrash(obj) click to toggle source

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

# 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