Sha256: 8d4424f72e908653ffc2e08114bfcfce07a2a7c0e9c346d5a3bbbadac603b36e

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

require 'has_messages'
require 'validates_as_email_address'
require 'has_emails/extensions/action_mailer'

# Adds a generic implementation for sending emails
module HasEmails
  module MacroMethods
    # 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!
    def has_emails
      has_many  :emails,
                  :as => :sender,
                  :class_name => 'Email',
                  :conditions => {:hidden_at => nil},
                  :order => 'messages.created_at ASC'
      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 ASC'
      
      include HasEmails::InstanceMethods
    end
  end
  
  module InstanceMethods
    # Composed emails that have not yet been sent.  These consists of all
    # emails that are currently in the "unsent" state.
    def unsent_emails
      emails.with_state(:unsent)
    end
    
    # Composed emails that have already been sent.  These consist of all emails
    # that are currently in the "queued" or "sent states.
    def sent_emails
      emails.with_states(:queued, :sent)
    end
  end
end

ActiveRecord::Base.class_eval do
  extend HasEmails::MacroMethods
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
has_emails-0.2.1 lib/has_emails.rb