Module: HasEmails::MacroMethods

Defined in:
lib/has_emails.rb

Instance Method Summary collapse

Instance Method Details

#has_emailsObject

Creates the following email associations:

  • emails - Emails that were composed and are visible to the owner. Emails may have been sent or unsent.

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

Creating new emails

To create a new email, the emails association should be used. For example:

address = EmailAddress.find(123)
email = user.emails.build
email.subject = 'Hello'
email.body = 'How are you?'
email.to EmailAddress.find(456)
email.save!
email.deliver!

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/has_emails.rb', line 26

def has_emails
  has_many  :emails,
              :as => :sender,
              :class_name => 'Email',
              :conditions => {:hidden_at => nil},
              :order => 'messages.created_at DESC'
  has_many  :received_emails,
              :as => :receiver,
              :class_name => 'MessageRecipient',
              :include => :message,
              :conditions => ['message_recipients.hidden_at IS NULL AND messages.state = ?', 'sent'],
              :order => 'messages.created_at DESC'
  
  include HasEmails::InstanceMethods
end