Module: HasMessages::MacroMethods

Defined in:
lib/has_messages.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) has_messages

Creates the following message associations:

  • messages - Messages that were composed and are visible to the owner. Mesages may have been sent or unsent.

  • +received_messages - Messages that have been received from others and are visible. Messages may have been read or unread.

Creating new messages

To create a new message, the messages association should be used, for example:

user = User.find(123)
message = user.messages.build
message.subject = 'Hello'
message.body = 'How are you?'
message.to User.find(456)
message.save
message.deliver

Drafts

You can get the drafts for a particular user by using the unsent_messages helper method. This will find all messages in the “unsent” state. For example,

user = User.find(123)
user.unsent_messages

You can also get at the messages that have been sent, using the sent_messages helper method. For example,

user = User.find(123)
user.sent_messages


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/has_messages.rb', line 38

def has_messages
  has_many  :messages,
              :as => :sender,
              :class_name => 'Message',
              :conditions => {:hidden_at => nil},
              :order => 'messages.created_at DESC'
  has_many  :received_messages,
              :as => :receiver,
              :class_name => 'MessageRecipient',
              :include => :message,
              :conditions => ['message_recipients.hidden_at IS NULL AND messages.state = ?', 'sent'],
              :order => 'messages.created_at DESC'
  
  include HasMessages::InstanceMethods
end