Class: EmailCampaign::Campaign

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/email_campaign/campaign.rb

Instance Method Summary collapse

Instance Method Details

#add_recipients(new_recipients, options = {}) ⇒ Object

new_recipients should be an Array of objects that respond to #email, #name, and #subscriber_id (falls back to #id if #subscriber_id doesn’t exist; either way, id should be unique within campaign)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/email_campaign/campaign.rb', line 12

def add_recipients(new_recipients, options = {})
  options.stringify_keys!
  new_recipients = [ new_recipients ] unless new_recipients.respond_to?(:each)
  
  processed = 0
  skipped = 0
  valid = 0
  invalid = 0
  duplicate = 0
  unsubscribed = 0
  
  new_recipients.each do |rcpt|
    subscriber_id = rcpt.respond_to?(:subscriber_id) ? rcpt.subscriber_id : rcpt.id
    
    if subscriber_id && r = recipients.find_by_subscriber_id(subscriber_id)
      if options['force']
        r.requeue
        valid += 1
      else
        skipped += 1
      end
      
      next
    end
    
    processed += 1
    r = recipients.build(:name => rcpt.name, :email_address => rcpt.email_address,
                         :subscriber_class_name => rcpt.class.name, :subscriber_id => subscriber_id)
    if r.save
      case
        when r.unsubscribed then unsubscribed += 1
        when r.duplicate then duplicate += 1
        when r.invalid_email then invalid += 1
        else valid += 1
      end
    else
      invalid += 1
    end
    
    r.queue
  end
  
  { :processed => processed, :skipped => skipped,
    :valid => valid, :invalid => invalid,
    :duplicate => duplicate, :unsubscribed => unsubscribed,
    :total => recipients.where(:ready => true).count }
end

#deliver!(unsanitary = false) ⇒ Object

delivers campaign NOW, ignoring deliver_at setting



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/email_campaign/campaign.rb', line 66

def deliver!(unsanitary = false)
  sent = []
  error = []
  
  update_attributes(:delivery_started_at => Time.now.utc) unless delivery_started_at
  
  if unsanitary
    SanitizeEmail.unsanitary { process_delivery }
  else
    process_delivery
  end
  
  update_attributes(:queued => false, :delivered => true, :delivery_finished_at => Time.now.utc)
end

#process_deliveryObject



81
82
83
# File 'app/models/email_campaign/campaign.rb', line 81

def process_delivery
  recipients.where(:ready => true).each { |r| r.delay.deliver }
end

#queue(deliver_at = Time.now.utc) ⇒ Object



60
61
62
63
# File 'app/models/email_campaign/campaign.rb', line 60

def queue(deliver_at = Time.now.utc)
  # update_attributes(:deliver_at => deliver_at, :queued => true, :queued_at => Time.now.utc)
  update_attributes(:deliver_at => deliver_at, :queued => true)
end

#queued_recipientsObject



85
86
87
# File 'app/models/email_campaign/campaign.rb', line 85

def queued_recipients
  recipients.where(:ready => true)
end