Skip to content

Instantly share code, notes, and snippets.

@vderyagin
Forked from TXDynamics/email_que.rb
Created May 27, 2013 15:23
Show Gist options
  • Save vderyagin/5657616 to your computer and use it in GitHub Desktop.
Save vderyagin/5657616 to your computer and use it in GitHub Desktop.
# == Schema Information
#
# Table name: email_ques
#
# id :integer not null, primary key
# job_que_id :integer
# to_field :string(255)
# subject_field :string(255)
# body_field :text
# send_status :text
# created_at :datetime not null
# updated_at :datetime not null
#
class EmailQue < ActiveRecord::Base
attr_accessible :body_field, :job_que_id, :send_status, :subject_field, :to_field
# Generate Que is used with an argument of the Job ID
def self.generate_que(job_que_id)
# Loads the details of the email job
job_info = JobQue.find(job_que_id)
template_info = EmailTemplate.find(job_info.email_template_id)
# To include our items within the loop I had to make them instance variables
@template_subject = template_info.template_subject
@template_body = template_info.template_body
@email_count = 0
# Now we begin to process and create the emails we will be sending
contacts = Contact.with_mailinglist.where("INF_MAILINGS_EVENTSID = '#{job_info.mailing_list_id}'")
contacts.each do |contact|
EmailQue.create(
to_field: contact.email,
subject_field: ShortCode.process(@template_subject, contact.id),
body_field: ShortCode.process(@template_body, contact.id),
send_status: 'NOT SENT',
job_que_id: job_info.id
)
@email_count += 1
end
# Now we update our Job Que with the following status "EMAILS READY"
job_info.update_attributes(
:job_status => 'EMAILS READY'
)
# Return the number of emails created
@email_count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment