Commit e5b0d48c

mo khan <mo@mokhan.ca>
2014-01-18 06:50:39
create mailer to deliver notifications when a new activity is created.
1 parent 34aac45
app/mailers/notification_mailer.rb
@@ -0,0 +1,9 @@
+class NotificationMailer < ActionMailer::Base
+  default from: "noreply@cakeside.com"
+
+  def notification_email(activity)
+    @user = activity.user
+    @activity = activity
+   mail(to: @user.email, subject: "New Activity on CakeSide")
+  end
+end
app/mailers/user_mailer.rb
@@ -4,6 +4,6 @@ class UserMailer < ActionMailer::Base
   def welcome_email(user)
     @url = 'https://www.cakeside.com/login'
     @user = user
-    mail(to: @user.email, subject: "Welcome to CakeSide")
+   mail(to: @user.email, subject: "Welcome to CakeSide")
   end
 end
app/models/activity.rb
@@ -1,4 +1,9 @@
 class Activity < ActiveRecord::Base
   belongs_to :subject, polymorphic: true
   belongs_to :user
+  after_create :send_notification_email
+
+  def send_notification_email
+    NotificationMailer.delay.notification_email(self)
+  end
 end
app/views/notification_mailer/notification_email.html.erb
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
+  </head>
+  <body>
+    <h1>Hi <%= @user.name %>,</h1>
+    <p>
+    You have new activity. Click <%= link_to "here", dashboard_url %>
+    </p>
+    <p>Have a wonderful day!</p>
+  </body>
+</html>
+
app/views/notification_mailer/notification_email.text.erb
@@ -0,0 +1,6 @@
+
+Hi <%= @user.name %>,
+
+You have new activity. Click <%= link_to "here", dashboard_url %>
+
+Have a wonderful day!
spec/mailers/notification_mailer_spec.rb
@@ -0,0 +1,24 @@
+require "spec_helper"
+
+describe NotificationMailer do
+  context "send welcome email" do
+    let(:activity) { build(:activity) }
+    let(:mail) { NotificationMailer.notification_email(activity) }
+
+    it "adds a subject" do
+      mail.subject.should == "New Activity on CakeSide"
+    end
+
+    it "sends to the users email" do
+      mail.to.should include activity.user.email
+    end
+
+    it "should send from the correct address" do
+      mail.from.should include 'noreply@cakeside.com'
+    end
+
+    it "includes their name" do
+      mail.body.encoded.should match(activity.user.name)
+    end
+  end
+end