Browse files

Completing basic mail merge flow from the back end (test passing)

  • Loading branch information...
1 parent e0d108f commit 705739d4e28c37e8f9820545374bfc1129436947 @nahurst committed Mar 17, 2013
View
3 Gemfile
@@ -17,4 +17,5 @@ gem "zurb-foundation", ">= 3.2.5", :group => :assets
gem "quiet_assets", ">= 1.0.1", :group => :development
gem "better_errors", ">= 0.6.0", :group => :development
gem "binding_of_caller", ">= 0.7.1", :group => :development, :platforms => [:mri_19, :rbx]
-gem "hub", ">= 1.10.2", :require => nil, :group => [:development]
+gem "hub", ">= 1.10.2", :require => nil, :group => [:development]
+gem "mustache"
View
2 Gemfile.lock
@@ -92,6 +92,7 @@ GEM
treetop (~> 1.4.8)
mime-types (1.21)
multi_json (1.7.0)
+ mustache (0.99.4)
nokogiri (1.5.6)
polyglot (0.3.3)
quiet_assets (1.0.2)
@@ -178,6 +179,7 @@ DEPENDENCIES
factory_girl_rails (>= 4.2.0)
hub (>= 1.10.2)
jquery-rails
+ mustache
quiet_assets (>= 1.0.1)
rails (= 3.2.12)
rspec-rails (>= 2.12.2)
View
10 app/mailers/bulk_mailer.rb
@@ -0,0 +1,10 @@
+class BulkMailer < ActionMailer::Base
+
+ def basic(email)
+ @body = email.body
+ mail(:to => email.to,
+ :from => email.from,
+ :subject => email.subject)
+ end
+
+end
View
4 app/models/email.rb
@@ -0,0 +1,4 @@
+class Email < ActiveRecord::Base
+ belongs_to :mail_merge
+ attr_accessible :body, :from, :subject, :to
+end
View
17 app/models/mail_merge.rb
@@ -1,3 +1,20 @@
+require 'mustache'
+
class MailMerge < ActiveRecord::Base
attr_accessible :body_template, :data, :subject_template
+ has_many :emails
+
+ def generate_emails(from)
+ ActiveSupport::JSON.decode(data).each do |context|
+ email = self.emails.build(
+ :subject => Mustache.render(subject_template, context),
+ :body => Mustache.render(body_template, context),
+ :to => context['email'],
+ :from => from)
+
+ BulkMailer.basic(email).deliver
+ email.save!
+ end
+ self.emails
+ end
end
View
9 app/views/bulk_mailer/basic.html.erb
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
+ </head>
+ <body>
+ <%= @body.html_safe %>
+ </body>
+</html>
View
2 app/views/mail_merge/new.html.erb
@@ -1,2 +0,0 @@
-<h1>MailMerge#new</h1>
-<p>Find me in app/views/mail_merge/new.html.erb</p>
View
2 app/views/mail_merge/show.html.erb
@@ -1,2 +0,0 @@
-<h1>MailMerge#show</h1>
-<p>Find me in app/views/mail_merge/show.html.erb</p>
View
14 db/migrate/20130317233716_create_emails.rb
@@ -0,0 +1,14 @@
+class CreateEmails < ActiveRecord::Migration
+ def change
+ create_table :emails do |t|
+ t.string :to
+ t.string :from
+ t.string :subject
+ t.text :body
+ t.references :mail_merge
+
+ t.timestamps
+ end
+ add_index :emails, :mail_merge_id
+ end
+end
View
14 db/schema.rb
@@ -11,7 +11,19 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130317223920) do
+ActiveRecord::Schema.define(:version => 20130317233716) do
+
+ create_table "emails", :force => true do |t|
+ t.string "to"
+ t.string "from"
+ t.string "subject"
+ t.text "body"
+ t.integer "mail_merge_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "emails", ["mail_merge_id"], :name => "index_emails_on_mail_merge_id"
create_table "mail_merges", :force => true do |t|
t.text "data"
View
8 spec/factories/mail_merges.rb
@@ -1,10 +1,10 @@
FactoryGirl.define do
factory :mail_merge do
data [
- {:email => 'leonardo@example.com', :name => 'Leonardo', :company => 'TMNT'},
- {:email => 'donatello@example.com', :name => 'Donatello', :company => 'TMNT'},
- {:email => 'michaelangello@example.com', :name => 'Michaelangello', :company => 'TMNT'},
- {:email => 'raphael@example.com', :name => 'Raphael', :company => 'TMNT'}
+ {'email' => 'leonardo@example.com', 'name' => 'Leonardo', 'company' => 'TMNT'},
+ {'email' => 'donatello@example.com', 'name' => 'Donatello', 'company' => 'TMNT'},
+ {'email' => 'michaelangello@example.com', 'name' => 'Michaelangello', 'company' => 'TMNT'},
+ {'email' => 'raphael@example.com', 'name' => 'Raphael', 'company' => 'TMNT'}
].to_json
subject_template "Great to see you {{name}}"
body_template "Hi {{name}}\nIt was great to see you at the {{company}} party!"
View
6 spec/models/mail_merge_spec.rb
@@ -5,13 +5,15 @@
it 'should generate emails for a complete mail merge' do
mm = create(:mail_merge)
- mm.generate_emails
+ mm.generate_emails("splinter@example.com")
mm.emails.length.should == 4
- email = mm.first
+ email = mm.emails.first
email.to.should == "leonardo@example.com"
email.subject.should == "Great to see you Leonardo"
email.body.should == "Hi Leonardo\nIt was great to see you at the TMNT party!"
+
+ ActionMailer::Base.deliveries.count.should == 4
end
end
end

0 comments on commit 705739d

Please sign in to comment.