From 64b957db2099197ca07ee5d2f2ebfa430c1794ef Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Wed, 27 Mar 2013 11:58:05 +0000 Subject: [PATCH] Actually send email on creating notifications. --- app/models/notification.rb | 6 ++++++ spec/controllers/notifications_controller_spec.rb | 10 +++++----- spec/models/notification_spec.rb | 11 +++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/app/models/notification.rb b/app/models/notification.rb index 789f773ca..9e61cd1ca 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -9,8 +9,14 @@ class Notification < ActiveRecord::Base default_scope order('created_at DESC') scope :unread, where(:read => false) + after_create :send_email + def self.unread_count self.unread.count end + def send_email + Notifier.notify(self).deliver + end + end diff --git a/spec/controllers/notifications_controller_spec.rb b/spec/controllers/notifications_controller_spec.rb index 939ddb443..ded781e01 100644 --- a/spec/controllers/notifications_controller_spec.rb +++ b/spec/controllers/notifications_controller_spec.rb @@ -14,7 +14,7 @@ describe NotificationsController do describe "GET index" do it "assigns all notifications as @notifications" do - notification = Notification.create! valid_attributes + notification = FactoryGirl.create(:notification, :recipient_id => subject.current_member.id) get :index, {} assigns(:notifications).should eq([notification]) end @@ -22,13 +22,13 @@ describe NotificationsController do describe "GET show" do it "assigns the requested notification as @notification" do - notification = Notification.create! valid_attributes + notification = FactoryGirl.create(:notification, :recipient_id => subject.current_member.id) get :show, {:id => notification.to_param} assigns(:notification).should eq(notification) end it "marks notifications as read" do - notification = Notification.create! valid_attributes + notification = FactoryGirl.create(:notification, :recipient_id => subject.current_member.id) get :show, {:id => notification.to_param} # we need to fetch it from the db again, can't test against the old one n = Notification.find(notification.id) @@ -38,14 +38,14 @@ describe NotificationsController do describe "DELETE destroy" do it "destroys the requested notification" do - notification = Notification.create! valid_attributes + notification = FactoryGirl.create(:notification, :recipient_id => subject.current_member.id) expect { delete :destroy, {:id => notification.to_param} }.to change(Notification, :count).by(-1) end it "redirects to the notifications page" do - notification = Notification.create! valid_attributes + notification = FactoryGirl.create(:notification, :recipient_id => subject.current_member.id) delete :destroy, {:id => notification.to_param} response.should redirect_to(notifications_url) end diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index f561f0457..720f905d3 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -31,4 +31,15 @@ describe Notification do @who.notifications.unread_count.should eq 2 end + it "sends email if asked" do + @notification2 = FactoryGirl.create(:notification) + @notification2.send_email + ActionMailer::Base.deliveries.last.to.should == [@notification2.recipient.email] + end + + it "sends email on creation" do + @notification2 = FactoryGirl.create(:notification) + ActionMailer::Base.deliveries.last.to.should == [@notification2.recipient.email] + end + end