Class: PrivateMail::Mailbox
- Inherits:
-
Object
- Object
- PrivateMail::Mailbox
- Defined in:
- lib/mailbox.rb
Instance Attribute Summary collapse
-
#type ⇒ Object
this is used to filter mail by mailbox type, use the [] method rather than setting this directly.
-
#user ⇒ Object
readonly
the user/owner of this mailbox, set when initialized.
Instance Method Summary collapse
-
#<<(msg) ⇒ Object
alias for add.
-
#[](mailbox_type) ⇒ Object
sets the mailbox type and returns itself.
-
#add(msg) ⇒ Object
adds a mail message to the user’s mailbox specified by type.
-
#count(filter = :all, options = {}) ⇒ Object
returns a count of mail messages filtered by type and filter, if set.
-
#delete(options = {}) ⇒ Object
permanantly deletes all the mail messages matched by the options.
-
#empty_trash(options = {}) ⇒ Object
deletes all messages that have been trashed and match the options if passed.
-
#has_conversation?(conversation) ⇒ Boolean
return true if the user is involved in the given conversation.
-
#initialize(user, type = :all) ⇒ Mailbox
constructor
creates a new Mailbox instance with the given user and optional type.
-
#latest_mail(options = {}) ⇒ Object
returns an array of the latest Mail message for each conversation the user is involved in filtered by type, if set.
-
#mail(options = {}) ⇒ Object
returns an array of all Mail for the user filtered by type, if set.
-
#mark_as_read(options = {}) ⇒ Object
marks all the mail messages matched by the options and type as read.
-
#mark_as_unread(options = {}) ⇒ Object
marks all the mail messages matched by the options and type as unread, except for sent messages.
-
#move_to(mailbox, options = {}) ⇒ Object
moves all mail matched by the options to the given mailbox.
-
#read_mail(options = {}) ⇒ Object
returns an array of read Mail for the user filtered by type, if set.
-
#unread_mail(options = {}) ⇒ Object
returns an array of unread Mail for the user filtered by type, if set.
Constructor Details
#initialize(user, type = :all) ⇒ Mailbox
creates a new Mailbox instance with the given user and optional type.
10 11 12 13 |
# File 'lib/mailbox.rb', line 10 def initialize(user, type = :all) @user = user @type = type end |
Instance Attribute Details
#type ⇒ Object
this is used to filter mail by mailbox type, use the [] method rather than setting this directly.
5 6 7 |
# File 'lib/mailbox.rb', line 5 def type @type end |
#user ⇒ Object (readonly)
the user/owner of this mailbox, set when initialized.
7 8 9 |
# File 'lib/mailbox.rb', line 7 def user @user end |
Instance Method Details
#<<(msg) ⇒ Object
alias for add
230 231 232 |
# File 'lib/mailbox.rb', line 230 def <<(msg) return self.add(msg) end |
#[](mailbox_type) ⇒ Object
sets the mailbox type and returns itself.
params:
- mailbox_type
-
type of mailbox to filter mail by, this can be anything, but the three most likely values for this will be the received, sent, and trash values set within the acts_as_messageable method.
returns:
self
example:
phil = User.find(3123)
#all mails in the user's inbox
phil.mailbox[:inbox].mail
265 266 267 268 |
# File 'lib/mailbox.rb', line 265 def [](mailbox_type) self.type = mailbox_type return self end |
#add(msg) ⇒ Object
adds a mail message to the user’s mailbox specified by type.
*this is used when sending a new message, all the recipients get a mail added to their inbox and the sender gets a mail in their sentbox.
params:
- msg
-
Message object from which a new mail is created from.
returns:
new Mail.
147 148 149 150 151 152 153 154 |
# File 'lib/mailbox.rb', line 147 def add(msg) attributes = {:message => msg, :conversation => msg.conversation} attributes[:mailbox] = @type.to_s unless @type == :all attributes[:read] = msg.sender.id == @user.id mail_msg = MailItem.new(attributes) @user.mail_items << mail_msg return mail_msg end |
#count(filter = :all, options = {}) ⇒ Object
returns a count of mail messages filtered by type and filter, if set.
*this performs an actual sql count rather than selecting all mail and then gettin a length on the array… not a big deal but this could be something that is checked often to notify the user when they receive a new mail.
params:
- filter
-
filters the count by the ‘read’ attribute.
-
:all - count of both read and unread mail.
-
:read - count of read mail.
-
:unread - count of unread mail.
- options
-
see mail for acceptable options.
returns:
number of mail messages
example:
phil = User.find(3123)
#get number of unread mail messages in phil's inbox
phil.mailbox[:inbox].mail_count(:unread)
40 41 42 43 44 45 |
# File 'lib/mailbox.rb', line 40 def count(filter = :all, = {}) = {:conditions => ["mail_items.user_id = ?", @user.id]} add_mailbox_condition!(, @type) add_conditions!(, "mail_items.read = ?", filter == :read) unless filter == :all return count_mail(, ) end |
#delete(options = {}) ⇒ Object
permanantly deletes all the mail messages matched by the options. Use move_to(:trash) instead if you want to send to user’s trash without deleting.
params:
- options
-
see mail for acceptable options.
223 224 225 226 227 |
# File 'lib/mailbox.rb', line 223 def delete( = {}) = {:conditions => ["mail_items.user_id = ?", @user.id]} add_mailbox_condition!(, @type) return delete_mail(, ) end |
#empty_trash(options = {}) ⇒ Object
deletes all messages that have been trashed and match the options if passed.
params:
- options
-
see mail for acceptable options.
239 240 241 242 243 |
# File 'lib/mailbox.rb', line 239 def empty_trash( = {}) = {:conditions => ["mail_items.user_id = ? AND mail_items.trashed = ?", @user.id, true]} add_mailbox_condition!(, @type) return delete_mail(, ) end |
#has_conversation?(conversation) ⇒ Boolean
return true if the user is involved in the given conversation.
246 247 248 |
# File 'lib/mailbox.rb', line 246 def has_conversation?(conversation) return mail_count(:all, :conversation => conversation) != 0 end |
#latest_mail(options = {}) ⇒ Object
returns an array of the latest Mail message for each conversation the user is involved in filtered by type, if set.
*possible use for this would be an inbox view of your mail so you can easily see the status of all the convos your involved in.
params:
- options
-
see mail for acceptable options.
returns:
array of Mail.
example:
phil = User.find(3123)
#get a list of the latest received mail for each conversation
phil.mailbox[:inbox].latest_mail
132 133 134 |
# File 'lib/mailbox.rb', line 132 def latest_mail( = {}) return only_latest(mail()) end |
#mail(options = {}) ⇒ Object
returns an array of all Mail for the user filtered by type, if set.
params:
- options
-
all valid find options are accepted as well as an additional conversation option.
-
:conversation - Conversation object to filter mail only belonging to this conversation.
-
:conditions - same as find conditions however the array version of conditions will not work, i.e., :conditions => [‘mail_items.read = ?’, false] will not work here.
-
all other find options will work as expected.
returns:
array of Mail.
example:
phil = User.find(3123)
#get all mail messages belonging to phil
phil.mailbox.mail
#get all mail messages in phil's inbox associated with conversation 23
phil.mailbox[:inbox].mail(:conversation => Conversation.find(23))
#get all unread mail messages belonging to phil associated with conversation 23
phil.mailbox.mail(:conversation => Conversation.find(23), :conditions => 'mail_items.read = false')
70 71 72 73 74 |
# File 'lib/mailbox.rb', line 70 def mail( = {}) = {} add_mailbox_condition!(, @type) return get_mail(, ) end |
#mark_as_read(options = {}) ⇒ Object
marks all the mail messages matched by the options and type as read.
params:
- options
-
see mail for acceptable options.
returns:
array of Mail.
example:
phil = User.find(3123)
#mark all inbox messages as read
phil.mailbox[:inbox].mark_as_read()
170 171 172 173 174 |
# File 'lib/mailbox.rb', line 170 def mark_as_read( = {}) = {} add_mailbox_condition!(, @type) return update_mail("mail_items.read = true", , ) end |
#mark_as_unread(options = {}) ⇒ Object
marks all the mail messages matched by the options and type as unread, except for sent messages.
params:
- options
-
see mail for acceptable options.
returns:
array of Mail.
example:
phil = User.find(3123)
#mark all inbox messages as unread
phil.mailbox[:inbox].mark_as_unread()
190 191 192 193 194 |
# File 'lib/mailbox.rb', line 190 def mark_as_unread( = {}) = {:conditions => ["mail_items.mailbox != ?",@user.mailbox_types[:sent].to_s]} add_mailbox_condition!(, @type) return update_mail("mail_items.read = false", , ) end |
#move_to(mailbox, options = {}) ⇒ Object
moves all mail matched by the options to the given mailbox. sent messages stay in the sentbox.
params:
- mailbox
-
the mailbox_type to move the mail messages to. (ex. :inbox, :trash)
- options
-
see mail for acceptable options.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/mailbox.rb', line 202 def move_to(mailbox, = {}) mailbox = mailbox.to_sym trash = mailbox == @user.mailbox_types[:deleted].to_sym = {} add_mailbox_condition!(, @type) if(!trash) #conditional update because sentmail is always sentmail - I believe case if the most widely supported conditional, mysql also has an if which would work as well but i think mysql is the only one to support it return update_mail("mail_items.trashed = false, mail_items.mailbox = CASE mail_items.mailbox WHEN '#{@user.mailbox_types[:sent].to_s}' THEN mail_items.mailbox ELSE '#{mailbox.to_s}' END", , ) end return update_mail("mail_items.trashed = true", , ) end |
#read_mail(options = {}) ⇒ Object
returns an array of read Mail for the user filtered by type, if set.
params:
- options
-
see mail for acceptable options.
returns:
array of Mail.
example:
phil = User.find(3123)
#get all read mail in phil's inbox
phil.mailbox[:inbox].read_mail
110 111 112 113 114 |
# File 'lib/mailbox.rb', line 110 def read_mail( = {}) = {:conditions => ["mail_items.read = ?", true]} add_mailbox_condition!(, @type) return get_mail(, ) end |
#unread_mail(options = {}) ⇒ Object
returns an array of unread Mail for the user filtered by type, if set.
params:
- options
-
see mail for acceptable options.
returns:
array of Mail.
example:
phil = User.find(3123)
#get all unread mail in phil's inbox
phil.mailbox[:inbox].unread_mail
90 91 92 93 94 |
# File 'lib/mailbox.rb', line 90 def unread_mail( = {}) = {:conditions => ["mail_items.read = ?", false]} add_mailbox_condition!(, @type) return get_mail(, ) end |