Class: Message

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

Overview

Represents a message sent from one user to one or more others.

States

Messages can be in 1 of 3 states:

  • unsent - The message has not yet been sent. This is the initial state.

  • queued - The message has been queued for future delivery.

  • sent - The message has been sent.

Interacting with the message

In order to perform actions on the message, such as queueing or delivering, you should always use the associated event action:

  • queue - Queues the message so that you can send it in a separate process

  • deliver - Sends the message to all of the recipients

Message visibility

Although you can delete a message, it will also delete it from the inbox of all the message’s recipients. Instead, you can change the visibility of the message. Messages have 1 of 2 states that define its visibility:

  • visible - The message is visible to the sender

  • hidden - The message is hidden from the sender

The visibility of a message can be changed by running the associated action:

  • hide -Hides the message from the sender

  • unhide - Makes the message visible again

Instance Method Summary collapse

Instance Method Details

#bcc(*receivers) ⇒ Object Also known as: bcc=

Blind carbon copies the receivers on the message



93
94
95
# File 'lib/has_messages/models/message.rb', line 93

def bcc(*receivers)
  receivers(receivers, 'bcc')
end

#cc(*receivers) ⇒ Object Also known as: cc=

Carbon copies the receivers on the message



87
88
89
# File 'lib/has_messages/models/message.rb', line 87

def cc(*receivers)
  receivers(receivers, 'cc')
end

#forwardObject

Forwards this message, including the original subject and body in the new message



100
101
102
103
104
# File 'lib/has_messages/models/message.rb', line 100

def forward
  message = self.class.new(:subject => subject, :body => body)
  message.sender = sender
  message
end

#latest_messageObject



76
77
78
# File 'lib/has_messages/models/message.rb', line 76

def latest_message
  follow_up_messages.last || self
end

#replyObject

Replies to this message, including the original subject and body in the new message. Only the original direct receivers are added to the reply.



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/has_messages/models/message.rb', line 108

def reply
  message = self.class.new(:subject => subject, :body => body)
  message.sender = sender
  message.to(to)
  # use the very first message to anchor all replies
  if self.original_message
    message.original_message = self.original_message
  else
    message.original_message = self
  end
  message
end

#reply_to_allObject

Replies to all recipients on this message, including the original subject and body in the new message. All receivers (direct, cc, and bcc) are added to the reply.



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/has_messages/models/message.rb', line 124

def reply_to_all
  message = reply
  message.cc(cc)
  message.bcc(bcc)
  # use the very first message to anchor all replies
  if self.original_message
    message.original_message = self.original_message
  else
    message.original_message = self
  end
  message
end

#threadObject



72
73
74
# File 'lib/has_messages/models/message.rb', line 72

def thread
  [self] + follow_up_messages
end

#to(*receivers) ⇒ Object Also known as: to=

Directly adds the receivers on the message (i.e. they are visible to all recipients)



81
82
83
# File 'lib/has_messages/models/message.rb', line 81

def to(*receivers)
  receivers(receivers, 'to')
end