diff --git a/app/models/comment.rb b/app/models/comment.rb index 09d4af9..a202fd5 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -14,6 +14,14 @@ class Comment validates_presence_of :body + def notification_recipients + app.notification_recipients - [user.email] + end + + def emailable? + app.emailable? && notification_recipients.any? + end + protected def increase_counter_cache err.inc(:comments_count, 1) @@ -24,4 +32,3 @@ class Comment end end - diff --git a/app/models/comment_observer.rb b/app/models/comment_observer.rb index a9beadc..f665de8 100644 --- a/app/models/comment_observer.rb +++ b/app/models/comment_observer.rb @@ -2,7 +2,7 @@ class CommentObserver < Mongoid::Observer observe :comment def after_create(comment) - Mailer.comment_notification(comment).deliver if comment.app.emailable? + Mailer.comment_notification(comment).deliver if comment.emailable? end end diff --git a/spec/models/app_spec.rb b/spec/models/app_spec.rb index 7ce2717..7716349 100644 --- a/spec/models/app_spec.rb +++ b/spec/models/app_spec.rb @@ -124,6 +124,25 @@ describe App do end end + context "emailable?" do + it "should be true if notify on errs and there are notification recipients" do + app = Fabricate(:app, :notify_on_errs => true, :notify_all_users => false) + 2.times { Fabricate(:watcher, :app => app) } + app.emailable?.should be_true + end + + it "should be false if notify on errs is disabled" do + app = Fabricate(:app, :notify_on_errs => false, :notify_all_users => false) + 2.times { Fabricate(:watcher, :app => app) } + app.emailable?.should be_false + end + + it "should be false if there are no notification recipients" do + app = Fabricate(:app, :notify_on_errs => true, :notify_all_users => false) + app.watchers.should be_empty + app.emailable?.should be_false + end + end context "copying attributes from existing app" do it "should only copy the necessary fields" do diff --git a/spec/models/comment_observer_spec.rb b/spec/models/comment_observer_spec.rb new file mode 100644 index 0000000..b2ec963 --- /dev/null +++ b/spec/models/comment_observer_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe CommentObserver do + context 'when a Comment is saved' do + let(:comment) { Fabricate.build(:comment) } + + context 'and it is emailable?' do + before { comment.stub(:emailable?).and_return(true) } + + it 'should send an email notification' do + Mailer.should_receive(:comment_notification). + with(comment). + and_return(mock('email', :deliver => true)) + comment.save + end + end + + context 'and it is not emailable?' do + before { comment.stub(:emailable?).and_return(false) } + + it 'should not send an email notification' do + Mailer.should_not_receive(:comment_notification) + comment.save + end + end + end +end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 9709019..cab9f3b 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -8,5 +8,48 @@ describe Comment do comment.errors[:body].should include("can't be blank") end end -end + context 'notification_recipients' do + let(:app) { Fabricate(:app) } + let!(:watcher) { Fabricate(:watcher, :app => app) } + let(:err) { Fabricate(:problem, :app => app) } + let(:comment_user) { Fabricate(:user, :email => 'author@example.com') } + let(:comment) { Fabricate.build(:comment, :err => err, :user => comment_user) } + + before do + Fabricate(:user_watcher, :app => app, :user => comment_user) + end + + it 'includes app notification_recipients except user email' do + comment.notification_recipients.should == [watcher.address] + end + end + + context 'emailable?' do + let(:app) { Fabricate(:app, :notify_on_errs => true) } + let!(:watcher) { Fabricate(:watcher, :app => app) } + let(:err) { Fabricate(:problem, :app => app) } + let(:comment_user) { Fabricate(:user, :email => 'author@example.com') } + let(:comment) { Fabricate.build(:comment, :err => err, :user => comment_user) } + + before do + Fabricate(:user_watcher, :app => app, :user => comment_user) + end + + it 'should be true if app is emailable? and there are notification recipients' do + comment.emailable?.should be_true + end + + it 'should be false if app is not emailable?' do + app.update_attribute(:notify_on_errs, false) + comment.notification_recipients.should be_any + comment.emailable?.should be_false + end + + it 'should be false if there are no notification recipients' do + watcher.destroy + app.emailable?.should be_true + comment.emailable?.should be_false + end + end +end -- libgit2 0.21.2