Module: Mailboxer::Models::Messageable::InstanceMethods
- Defined in:
- lib/mailboxer/models/messageable.rb
Instance Method Summary collapse
-
#email ⇒ Object
Returning the email address of the model.
-
#mailbox ⇒ Object
Gets the mailbox of the messageable.
-
#name ⇒ Object
Returning any kind of indentification you want for the model.
-
#notify(subject, body, object = nil) ⇒ Object
Sends a notification to the messageable.
-
#read(obj) ⇒ Object
Mark the object as read for messageable.
-
#reply(conversation, recipients, reply_body, subject = nil) ⇒ Object
Basic reply method.
-
#reply_to_all(receipt, reply_body, subject = nil) ⇒ Object
Replies to all the recipients of the message in the conversation.
-
#reply_to_conversation(conversation, reply_body, subject = nil, should_untrash = true) ⇒ 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).
-
#reply_to_sender(receipt, reply_body, subject = nil) ⇒ Object
Replies to the sender of the message in the conversation.
-
#send_message(recipients, msg_body, subject) ⇒ Object
Sends a messages, starting a new conversation, with the messageable as originator.
-
#should_email?(object) ⇒ Boolean
Returning whether an email should be sent for this object (Message or Notification).
-
#trash(obj) ⇒ Object
Mark the object as trashed for messageable.
-
#unread(obj) ⇒ Object
Mark the object as unread for messageable.
-
#untrash(obj) ⇒ Object
Mark the object as not trashed for messageable.
Instance Method Details
#email ⇒ Object
Returning the email address of the model
28 29 30 31 32 |
# File 'lib/mailboxer/models/messageable.rb', line 28 def email super rescue NameError return "define_email@on_your.model" end |
#mailbox ⇒ Object
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 |
#name ⇒ Object
Returning any kind of indentification you want for the model
21 22 23 24 25 |
# File 'lib/mailboxer/models/messageable.rb', line 21 def name super rescue NameError return "You should add method :name in your Messageable model" end |
#notify(subject, body, object = nil) ⇒ Object
Sends a notification to the messageable
49 50 51 52 53 54 |
# File 'lib/mailboxer/models/messageable.rb', line 49 def notify(subject,body,object = nil) notification = Notification.new({:body => body, :subject => subject}) notification.recipients = [self] notification.object = object if object.present? return notification.deliver end |
#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
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mailboxer/models/messageable.rb', line 105 def 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| read(sub_obj) } else return nil end end |
#reply(conversation, recipients, reply_body, subject = nil) ⇒ Object
Basic reply method. USE NOT RECOMENDED. Use reply_to_sender, reply_to_all and reply_to_conversation instead.
68 69 70 71 72 73 74 75 |
# File 'lib/mailboxer/models/messageable.rb', line 68 def reply(conversation, recipients, reply_body, subject = nil) subject = subject || "RE: #{conversation.subject}" response = Message.new({:sender => self, :conversation => conversation, :body => reply_body, :subject => subject}) response.recipients = recipients.is_a?(Array) ? recipients : [recipients] response.recipients = response.recipients.uniq response.recipients.delete(self) return response.deliver(true) end |
#reply_to_all(receipt, reply_body, subject = nil) ⇒ Object
Replies to all the recipients of the message in the conversation
83 84 85 |
# File 'lib/mailboxer/models/messageable.rb', line 83 def reply_to_all(receipt, reply_body, subject = nil) return reply(receipt.conversation, receipt..recipients, reply_body, subject) end |
#reply_to_conversation(conversation, reply_body, subject = nil, should_untrash = true) ⇒ 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)
89 90 91 92 93 94 95 |
# File 'lib/mailboxer/models/messageable.rb', line 89 def reply_to_conversation(conversation, reply_body, subject = nil, should_untrash = true) #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..recipients, reply_body, subject) end |
#reply_to_sender(receipt, reply_body, subject = nil) ⇒ Object
Replies to the sender of the message in the conversation
78 79 80 |
# File 'lib/mailboxer/models/messageable.rb', line 78 def reply_to_sender(receipt, reply_body, subject = nil) return reply(receipt.conversation, receipt..sender, reply_body, subject) end |
#send_message(recipients, msg_body, subject) ⇒ Object
Sends a messages, starting a new conversation, with the messageable as originator
58 59 60 61 62 63 64 |
# File 'lib/mailboxer/models/messageable.rb', line 58 def (recipients, msg_body, subject) convo = Conversation.new({:subject => subject}) = Message.new({:sender => self, :conversation => convo, :body => msg_body, :subject => subject}) .recipients = recipients.is_a?(Array) ? recipients : [recipients] .recipients = .recipients.uniq return .deliver end |
#should_email?(object) ⇒ Boolean
Returning whether an email should be sent for this object (Message or Notification)
35 36 37 38 39 |
# File 'lib/mailboxer/models/messageable.rb', line 35 def should_email?(object) super rescue NameError return true 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
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/mailboxer/models/messageable.rb', line 152 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 |
#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
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/mailboxer/models/messageable.rb', line 128 def 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| unread(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
175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/mailboxer/models/messageable.rb', line 175 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 |