-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ryan Neufeld
committed
Jul 2, 2009
1 parent
2bb4cbd
commit f1c678a
Showing
2 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ index/ | |
tmp | ||
*~ | ||
*.log | ||
tmp | ||
tmp | ||
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
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_campaign | ||
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' => '[email protected]', | ||
'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 |