Class: ContactEmail

Inherits:
Email
  • Object
show all
Includes:
ScheduledEmail
Defined in:
app/models/contact_email.rb

Overview

A ContactEmail is a one time mail generated from an EmailTemplate, sent not to a list, but to a set of contact_ids.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_from_template(template, attrs = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'app/models/contact_email.rb', line 14

def new_from_template(template, attrs = {})
  new({
    :name       => "#{template.name} - #{DateTime.now.to_i}",
    :subject    => template.subject,
    :from_email => template.from_email,
    :html_body  => template.html_body,
    :text_body  => template.text_body
  }.merge(attrs))
end

Instance Method Details

#contact_idsObject



40
41
42
# File 'app/models/contact_email.rb', line 40

def contact_ids
  (@contact_ids ||= []).map(&:to_i)
end

#contact_ids=(val) ⇒ Object

contact_ids only gets set if it’s an array or a properly formatted string



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/contact_email.rb', line 26

def contact_ids=(val)
  @contact_ids = case val
                 when /^\[?((\d+,?\s?)+)\]?$/
                   $1.split(',')
                 when Array
                   val
                 else
                   []
                 end

  # clear user_ids cache
  @user_ids = nil
end

#user_idsObject



45
46
47
48
49
50
51
52
53
# File 'app/models/contact_email.rb', line 45

def user_ids 
  @user_ids ||= if @contact_ids.present?
    user_scope  = (User.primary.joins(:contact) & Contact.where(:id => @contact_ids))
    user_id_sql = user_scope.select('users.id').to_sql
    User.connection.send(:select_values, user_id_sql, 'User ID Load')
  else
    []
  end
end