Module: Mailboxer::Models::Messageable

Defined in:
lib/mailboxer/models/messageable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



4
5
6
# File 'lib/mailboxer/models/messageable.rb', line 4

def self.included(mod)
  mod.extend(ClassMethods)
end

Instance Method Details

#mailboxObject

Gets the mailbox of the messageable



42
43
44
45
46
# File 'lib/mailboxer/models/messageable.rb', line 42

def mailbox
  @mailbox = Mailbox.new(self) if @mailbox.nil?
  @mailbox.type = :all
  return @mailbox
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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mailboxer/models/messageable.rb', line 104

def mark_as_read(obj)
  case obj
  when Receipt
    return 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) }
  else
    return nil
  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



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

def mark_as_unread(obj)
  case obj
  when Receipt
    return 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) }
  else
  return nil
  end
end

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

Sends a notification to the messageable



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

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



66
67
68
69
70
71
72
73
74
# File 'lib/mailboxer/models/messageable.rb', line 66

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)
  return 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



82
83
84
# File 'lib/mailboxer/models/messageable.rb', line 82

def reply_to_all(receipt, reply_body, subject=nil, sanitize_text=true, attachment=nil)
  return 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)



88
89
90
91
92
93
94
# File 'lib/mailboxer/models/messageable.rb', line 88

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
  end
  return 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



77
78
79
# File 'lib/mailboxer/models/messageable.rb', line 77

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

#search_messages(query) ⇒ Object



188
189
190
191
192
193
194
195
# File 'lib/mailboxer/models/messageable.rb', line 188

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, sanitize_text = true, attachment = nil) ⇒ Object

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



55
56
57
58
59
60
61
62
# File 'lib/mailboxer/models/messageable.rb', line 55

def send_message(recipients, msg_body, subject, sanitize_text=true, attachment=nil)
  convo = Conversation.new({:subject => subject})
  message = messages.new({:body => msg_body, :subject => subject, :attachment => attachment})
  message.conversation = convo
  message.recipients = recipients.is_a?(Array) ? recipients : [recipients]
  message.recipients = message.recipients.uniq
  return 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



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

def trash(obj)
  case obj
  when Receipt
    return 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) }
  else
  return nil
  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



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

def untrash(obj)
  case obj
  when Receipt
    return 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) }
  else
  return nil
  end
end