Class: Message
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Message
- Defined in:
- app/models/message.rb
Instance Attribute Summary collapse
-
#reply_to ⇒ Object
Returns the value of attribute reply_to.
-
#to ⇒ Object
Returns the value of attribute to.
Class Method Summary collapse
- .new_reply(sender, message_thread = nil, params = {}) ⇒ Object
-
.read(id, reader) ⇒ Object
Ensures the passed user is either the sender or the recipient then returns the message.
Instance Method Summary collapse
- #ensure_not_sending_to_self ⇒ Object
-
#mark_deleted(user) ⇒ Object
Marks a message as deleted by either the sender or the recipient, which ever the user that was passed is.
- #notify_recipient ⇒ Object
-
#read? ⇒ Boolean
Returns true or false value based on whether the a message has been read by its recipient.
- #update_message_threads ⇒ Object
Instance Attribute Details
#reply_to ⇒ Object
Returns the value of attribute reply_to.
3 4 5 |
# File 'app/models/message.rb', line 3 def reply_to @reply_to end |
#to ⇒ Object
Returns the value of attribute to.
2 3 4 |
# File 'app/models/message.rb', line 2 def to @to end |
Class Method Details
.new_reply(sender, message_thread = nil, params = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/models/message.rb', line 73 def self.new_reply(sender, = nil, params = {}) = new(params[:message]) .to ||= params[:to] if params[:to] if .parent = . .reply_to = . .to = .sender.login .subject = ..subject .sender = sender end end |
.read(id, reader) ⇒ Object
Ensures the passed user is either the sender or the recipient then returns the message. If the reader is the recipient and the message has yet not been read, it marks the read_at timestamp.
27 28 29 30 31 32 33 34 |
# File 'app/models/message.rb', line 27 def self.read(id, reader) = self.reader(reader).find(id) if .read_at.nil? && reader == .recipient .read_at = Time.now .save! end end |
Instance Method Details
#ensure_not_sending_to_self ⇒ Object
50 51 52 |
# File 'app/models/message.rb', line 50 def ensure_not_sending_to_self errors.add(:base, "You may not send a message to yourself.") if self.recipient && self.recipient.eql?(self.sender) end |
#mark_deleted(user) ⇒ Object
Marks a message as deleted by either the sender or the recipient, which ever the user that was passed is. Once both have marked it deleted, it is destroyed.
43 44 45 46 47 |
# File 'app/models/message.rb', line 43 def mark_deleted(user) self.sender_deleted = true if self.sender == user self.recipient_deleted = true if self.recipient == user self.sender_deleted && self.recipient_deleted ? self.destroy : save! end |
#notify_recipient ⇒ Object
54 55 56 |
# File 'app/models/message.rb', line 54 def notify_recipient UserNotifier.(self).deliver end |
#read? ⇒ Boolean
Returns true or false value based on whether the a message has been read by its recipient.
37 38 39 |
# File 'app/models/message.rb', line 37 def read? self.read_at.nil? ? false : true end |
#update_message_threads ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/models/message.rb', line 58 def recipients_thread = MessageThread.find_or_create_by(:recipient_id => self.recipient_id, :parent_message_id => (self.parent_id || self.id)) recipients_thread.sender = sender recipients_thread.recipient = recipient recipients_thread. = self recipients_thread. = (self.parent || self) recipients_thread.save if parent senders_thread = MessageThread.find_or_create_by(:recipient_id => self.sender_id, :parent_message_id => self.parent_id) senders_thread. = self senders_thread.save end end |