Class: Campagne::SenderJob

Inherits:
Object
  • Object
show all
Defined in:
app/models/campagne/sender_job.rb

Class Method Summary collapse

Class Method Details

.generate_tokenObject



6
7
8
9
10
11
12
13
# File 'app/models/campagne/sender_job.rb', line 6

def self.generate_token
  token = rand(36**18).to_s(36)
  token = token.gsub('+', '')
  while CampagneDelivery.where(:token => token).first do
    token = rand(36**18).to_s(36)
  end
  token
end

.perform(campaign_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/campagne/sender_job.rb', line 15

def self.perform(campaign_id)
  ActiveSupport::BufferedLogger.new(Rails.root.join('log/resque.log')).info([Time.now.iso8601, $$, "I", "---PERFORM---", campaign_id].join("\t"))
  campaign = Campaign.find(campaign_id)
  contacts = campaign.lists.map(&:contacts).flatten
  contacts = contacts.sort_by {rand} # shuffle
  contacts.each do |contact|
    next if campaign.deliveries.where(:contact_id => contact.id).first
    token = generate_token
    Sender.deliver_email(contact.email, campaign.subject, campaign.formated_body(token), token)
    campaign.deliveries.create(:contact_id => contact.id, :email => contact.email, :token => token)
  end
end