Skip to content

Instantly share code, notes, and snippets.

@avescodes
Created July 2, 2009 18:35
Show Gist options
  • Save avescodes/139630 to your computer and use it in GitHub Desktop.
Save avescodes/139630 to your computer and use it in GitHub Desktop.
class FreemailerCampaign < ActiveRecord::Base
belongs_to :sender, :class_name => 'User'
has_many :freemailer_campaign_contacts, :dependent => :destroy
has_many :contacts, :through => :freemailer_campaign_contacts
validates_uniqueness_of :title, :scope => :sender_id, :on => :create, :message => "must be unique" #JS me
attr_accessible :title, :subject
before_destroy :remove_active_campaign
def contact_names
contacts.map(&:name_for_display).join(', ').squeeze(' ')
end
def contact_emails
self[:contacts].map(&:primary_email)
end
def status
if sent
'Sent'
else
'Unsent'
end
end
def make_active_for_sender
sender.active_campaign = self
sender.save
end
def preview
fill_template(preview_user)
end
def send
if not sent
contacts.each do |contact|
begin
Freemailer.deliver_from_template(self,contact)
rescue
# Rescue errors and record them
end
end
end
end
private
def preview_user
{
'first name' => 'John',
'last name' => 'Doe',
'middle name' => 'H',
'name' => 'John Doe',
'email' => 'john@doe.com',
'address' => "123 Some Pl.\nWhere, Ever 90210\nCanada"
}
end
def fill_template_for_contact(person)
fill_template ({
'first name' => person.first_name,
'last name' => person.last_name,
'middle name' => person.middle_name,
'middle initial' => person.middle_name.first.upcase,
'name' => person.name_for_display,
'email' => person.primary_email,
'address' => person.primary_address
})
end
def fill_template(user_hash)
user_hash.default = ''
body_template.gsub(/\{(.*)\}/) do |item|
user_hash[$1].to_s
end
end
def remove_active_campaign
debugger
if sender.active_campaign == self
sender.active_campaign = nil
sender.save
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment