diff --git a/app/models/freemailer.rb b/app/models/freemailer.rb index ce7d20b..8fbc519 100644 --- a/app/models/freemailer.rb +++ b/app/models/freemailer.rb @@ -1,13 +1,13 @@ class Freemailer < ActionMailer::Base - def from_template(sent_at = Time.now) - subject 'Freemailer#from_template' - recipients '' - from '' + def from_template(campaign, contact, sent_at = Time.now) + subject campaign.subject + recipients contact.primary_email + from campaign.from sent_on sent_at - - body :greeting => 'Hi,' + + body campaign.fill_template_for_contact(contact) end end diff --git a/app/models/freemailer_campaign.rb b/app/models/freemailer_campaign.rb index e8e7bef..6b09302 100644 --- a/app/models/freemailer_campaign.rb +++ b/app/models/freemailer_campaign.rb @@ -11,6 +11,10 @@ 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' @@ -25,9 +29,9 @@ def make_active_for_sender end def preview - + fill_template(preview_user) end - + private def preview_user @@ -41,9 +45,23 @@ def preview_user } 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 /\{(.*)\}/, user_hash[$1].to_s + body_template.gsub(/\{(.*)\}/) do |item| + user_hash[$1].to_s + end end def remove_active_campaign diff --git a/app/views/freemailer_campaigns/edit.html.haml b/app/views/freemailer_campaigns/edit.html.haml index 45c5473..e229e30 100644 --- a/app/views/freemailer_campaigns/edit.html.haml +++ b/app/views/freemailer_campaigns/edit.html.haml @@ -10,6 +10,10 @@ Contacts: %br = @freemailer_campaign.contact_names + %p + = f.label :from + %br + = f.text_field :from %p = f.label :subject %br diff --git a/app/views/freemailer_campaigns/index.html.haml b/app/views/freemailer_campaigns/index.html.haml index 5348444..5222361 100644 --- a/app/views/freemailer_campaigns/index.html.haml +++ b/app/views/freemailer_campaigns/index.html.haml @@ -2,11 +2,13 @@ Your Mailing Campaigns %h2 Active Campaign +%p Lorem ipsum dolor sit amet, consectetur magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum %table %tr %th Subject %th Title %th Contacts + %th From %th Body template %th Status - @freemailer_campaigns.each do |freemailer_campaign| @@ -16,11 +18,13 @@ %td = h freemailer_campaign.title %td - == (#{freemailer_campaign.contacts.count}) #{freemailer_campaign.contact_names} + == (#{freemailer_campaign.contacts.count}) #{h freemailer_campaign.contact_names} + %td + = h freemailer_campaign.from %td - if freemailer_campaign.body_template = h freemailer_campaign.body_template[0..75] - %td + %td - if freemailer_campaign.sent Sent - else diff --git a/app/views/freemailer_campaigns/show.html.haml b/app/views/freemailer_campaigns/show.html.haml index e355ee3..a01f507 100644 --- a/app/views/freemailer_campaigns/show.html.haml +++ b/app/views/freemailer_campaigns/show.html.haml @@ -4,6 +4,10 @@ %b Contacts: = h @freemailer_campaign.contact_names +%p + %b + From: + = h @freemailer_campaign.from %p %b Subject: diff --git a/db/migrate/20090701134557_add_from_to_freemailer_campaign.rb b/db/migrate/20090701134557_add_from_to_freemailer_campaign.rb new file mode 100644 index 0000000..c0f39cb --- /dev/null +++ b/db/migrate/20090701134557_add_from_to_freemailer_campaign.rb @@ -0,0 +1,9 @@ +class AddFromToFreemailerCampaign < ActiveRecord::Migration + def self.up + add_column :freemailer_campaigns, :from, :string + end + + def self.down + remove_column :freemailer_campaigns, :from + end +end diff --git a/db/schema.rb b/db/schema.rb index 363e1c1..be831c0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20090625174247) do +ActiveRecord::Schema.define(:version => 20090701134557) do create_table "contact_addresses", :force => true do |t| t.integer "contact_id", :null => false @@ -103,6 +103,7 @@ t.datetime "created_at" t.datetime "updated_at" t.boolean "sent", :default => false + t.string "from" end create_table "log_items", :force => true do |t| diff --git a/spec/models/freemailer_campaign_spec.rb b/spec/models/freemailer_campaign_spec.rb index 4729416..f54c795 100644 --- a/spec/models/freemailer_campaign_spec.rb +++ b/spec/models/freemailer_campaign_spec.rb @@ -35,7 +35,6 @@ campaign.stub!(:contacts).and_return do [ Contact.new( :first_name => 'that', :last_name => 'guy' ), Contact.new( :first_name => 'this', :last_name => 'guy' )] - end campaign.contact_names.should == "that guy, this guy" end @@ -50,7 +49,7 @@ Doe! John Doe 123 Some Pl. -Where, Ever 90120 +Where, Ever 90210 Canada EOS end