Class: PostmanPat::PmMail
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- PostmanPat::PmMail
- Includes:
- AASM
- Defined in:
- app/models/postman_pat/pm_mail.rb
Class Method Summary collapse
-
.compose(author, recipients, subject, message) ⇒ PmMessage
Creates and sends a new Private Message with subject and message to all recipients.
-
.get_all_mail(user_id) ⇒ Array
Gets all PmMail’s for a user.
-
.get_mail(user_id, mail_id) ⇒ PmMail
Gets a specific pm mail by the user_id and mail_id.
-
.num_deleted_mail(user_id) ⇒ Object
Returns the number of deleted private mail for a particular user.
-
.num_mail(user_id) ⇒ Object
Returns the total number of private mails for a particular user.
-
.num_read_mail(user_id) ⇒ Object
Returns the number of read private mail for a particular user.
-
.num_unread_mail(user_id) ⇒ Object
Returns the number of unread private mail for a particular user.
Instance Method Summary collapse
-
#all_collaborators ⇒ Array
Returns all the collaborators(users) of the PmMail/Message thread.
-
#is_deleted? ⇒ Boolean
Return true or false if the mail has been deleted.
-
#is_read? ⇒ Boolean
Return true or false if the mail has been read or not.
-
#is_sent? ⇒ Boolean
Return true or false if the mail is in a state of sent.
-
#is_unread? ⇒ Boolean
Return true or false if the mail is unread or not.
-
#reply(message) ⇒ Object
Reply to the mail message thread.
-
#who_last_replied? ⇒ User
Tell us who the last user was to reply to the PmMail message thread.
Class Method Details
.compose(author, recipients, subject, message) ⇒ PmMessage
Creates and sends a new Private Message with subject and message to all recipients
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/models/postman_pat/pm_mail.rb', line 62 def self.compose(, recipients, subject, ) recipients.map {|r| r.to_i} # Add default subject if non is provided subject = "[No Subject]" if subject.blank? msg = PostmanPat::PmMessage.new msg. = msg.subject = subject msg. = transaction do msg.save! # send a copy of the mail to the creator of the message and mark it as sent mail = PostmanPat::PmMail.new mail.recipient = mail. = msg mail.mark_sent! mail.save! # create a new piece of mail for each collaborator in the message recipients.each do |recipient| mail = PostmanPat::PmMail.new mail.recipient_id = recipient mail. = msg mail.save! end # each loop end # transaction msg end |
.get_all_mail(user_id) ⇒ Array
Gets all PmMail’s for a user
106 107 108 |
# File 'app/models/postman_pat/pm_mail.rb', line 106 def self.get_all_mail(user_id) where(:recipient_id => user_id) end |
.get_mail(user_id, mail_id) ⇒ PmMail
Gets a specific pm mail by the user_id and mail_id
98 99 100 |
# File 'app/models/postman_pat/pm_mail.rb', line 98 def self.get_mail(user_id, mail_id) where(:id => mail_id).where(:recipient_id => user_id).first end |
.num_deleted_mail(user_id) ⇒ Object
Returns the number of deleted private mail for a particular user
140 141 142 |
# File 'app/models/postman_pat/pm_mail.rb', line 140 def self.num_deleted_mail(user_id) where(:recipient_id => user_id).where(:aasm_state => 'deleted').count end |
.num_mail(user_id) ⇒ Object
Returns the total number of private mails for a particular user
115 116 117 |
# File 'app/models/postman_pat/pm_mail.rb', line 115 def self.num_mail(user_id) where(:recipient_id => user_id).count end |
.num_read_mail(user_id) ⇒ Object
Returns the number of read private mail for a particular user
132 133 134 |
# File 'app/models/postman_pat/pm_mail.rb', line 132 def self.num_read_mail(user_id) where(:recipient_id => user_id).where(:aasm_state => 'read').count end |
.num_unread_mail(user_id) ⇒ Object
Returns the number of unread private mail for a particular user
124 125 126 |
# File 'app/models/postman_pat/pm_mail.rb', line 124 def self.num_unread_mail(user_id) where(:recipient_id => user_id).where(:aasm_state => 'unread').count end |
Instance Method Details
#all_collaborators ⇒ Array
Returns all the collaborators(users) of the PmMail/Message thread
218 219 220 221 222 223 224 225 |
# File 'app/models/postman_pat/pm_mail.rb', line 218 def all_collaborators collaborators = PostmanPat::PmMail.where(:pm_message_id => self.) ids = [] collaborators.each do |c| ids << c.recipient_id end User.where(:id => ids) end |
#is_deleted? ⇒ Boolean
Return true or false if the mail has been deleted
173 174 175 |
# File 'app/models/postman_pat/pm_mail.rb', line 173 def is_deleted? self.aasm_state == "deleted" end |
#is_read? ⇒ Boolean
Return true or false if the mail has been read or not
159 160 161 |
# File 'app/models/postman_pat/pm_mail.rb', line 159 def is_read? self.aasm_state == "read" end |
#is_sent? ⇒ Boolean
Return true or false if the mail is in a state of sent
166 167 168 |
# File 'app/models/postman_pat/pm_mail.rb', line 166 def is_sent? self.aasm_state == "sent" end |
#is_unread? ⇒ Boolean
Return true or false if the mail is unread or not
152 153 154 |
# File 'app/models/postman_pat/pm_mail.rb', line 152 def is_unread? self.aasm_state == "unread" end |
#reply(message) ⇒ Object
Reply to the mail message thread.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'app/models/postman_pat/pm_mail.rb', line 180 def reply() reply = PostmanPat::PmMessage.new reply.parent_id = self. reply. = self.recipient_id reply. = if reply.save! # Set the message as unread again for all collaborators of the mail thread because # one of them replied, thus making the mail "new" or "unseen" by all collaborators set_all_to_unread # Set the senders message state to 'sent' self.mark_sent! end end |
#who_last_replied? ⇒ User
Tell us who the last user was to reply to the PmMail message thread. If nobody has replied, the author will be returned.
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'app/models/postman_pat/pm_mail.rb', line 201 def who_last_replied? = PostmanPat::PmMessage.find(self.) # [TODO] - raise exception if pm_message is nil if .children.count > 0 # [TODO] - link up the account/user model to be dynamic on whatever model called acts_as_postman_pat User.find(.children.last.) else # if nobody has replied, return creator User.find(.) end end |