Class: Message

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#reply_toObject

Returns the value of attribute reply_to.



3
4
5
# File 'app/models/message.rb', line 3

def reply_to
  @reply_to
end

#toObject

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, message_thread = nil, params = {})
  message = new(params[:message])
  message.to ||= params[:to] if params[:to]

  if message_thread
    message.parent = message_thread.parent_message
    message.reply_to = message_thread.message
    message.to = message_thread.sender.
    message.subject = message_thread.parent_message.subject
    message.sender = sender
  end

  message
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)
  message = self.reader(reader).find(id)
  if message.read_at.nil? && reader == message.recipient
    message.read_at = Time.now
    message.save!
  end
  message
end

Instance Method Details

#ensure_not_sending_to_selfObject



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_recipientObject



54
55
56
# File 'app/models/message.rb', line 54

def notify_recipient
  UserNotifier.message_notification(self).deliver
end

#read?Boolean

Returns true or false value based on whether the a message has been read by its recipient.

Returns:

  • (Boolean)


37
38
39
# File 'app/models/message.rb', line 37

def read?
  self.read_at.nil? ? false : true
end

#update_message_threadsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/message.rb', line 58

def update_message_threads
  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.message = self
  recipients_thread.parent_message = (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.message = self
    senders_thread.save
  end
end