From 0ce6572c3aa877aff3b51ce30fbdd05b307f96ae Mon Sep 17 00:00:00 2001 From: Alan Freihof Tygel Date: Thu, 8 Dec 2011 19:21:17 -0200 Subject: [PATCH] Do not send email to comment author --- app/models/comment.rb | 8 ++++++-- test/unit/comment_notifier_test.rb | 29 ++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index 9fb59e7..0047dc0 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -67,6 +67,10 @@ class Comment < ActiveRecord::Base self.find(:all, :order => 'created_at desc, id desc', :limit => limit) end + def notification_emails + self.article.profile.notification_emails - [self.author_email || self.email] + end + after_save :notify_article after_destroy :notify_article def notify_article @@ -74,7 +78,7 @@ class Comment < ActiveRecord::Base end after_create do |comment| - if comment.article.notify_comments? && !comment.article.profile.notification_emails.empty? + if comment.article.notify_comments? && !comment.notification_emails.empty? Comment::Notifier.deliver_mail(comment) end end @@ -111,7 +115,7 @@ class Comment < ActiveRecord::Base class Notifier < ActionMailer::Base def mail(comment) profile = comment.article.profile - recipients profile.notification_emails + recipients comment.notification_emails from "#{profile.environment.name} <#{profile.environment.contact_email}>" subject _("[%s] you got a new comment!") % [profile.environment.name] body :recipient => profile.nickname || profile.name, diff --git a/test/unit/comment_notifier_test.rb b/test/unit/comment_notifier_test.rb index 3b9c38e..1da4fa0 100644 --- a/test/unit/comment_notifier_test.rb +++ b/test/unit/comment_notifier_test.rb @@ -9,23 +9,24 @@ class CommentNotifierTest < Test::Unit::TestCase ActionMailer::Base.perform_deliveries = true ActionMailer::Base.deliveries = [] @profile = create_user('user_comment_test').person + @profile2 = create_user('user_comment_test2').person @article = fast_create(Article, :name => 'Article test', :profile_id => @profile.id, :notify_comments => true) end should 'deliver mail after make aarticle commment' do assert_difference ActionMailer::Base.deliveries, :size do - @article.comments << Comment.new(:author => @profile, :title => 'test comment', :body => 'you suck!') + @article.comments << Comment.new(:author => @profile2, :title => 'test comment', :body => 'you suck!') end end - should 'deliver mail to owner of article' do - @article.comments << Comment.new(:author => @profile, :title => 'test comment', :body => 'you suck!') + should 'deliver mail to owner of article with a another person' do + @article.comments << Comment.new(:author => @profile2, :title => 'test comment', :body => 'you suck!') sent = ActionMailer::Base.deliveries.first assert_equal [@profile.email], sent.to end should 'display author name in delivered mail' do - @article.comments << Comment.new(:author => @profile, :title => 'test comment', :body => 'you suck!') + @article.comments << Comment.new(:author => @profile2, :title => 'test comment', :body => 'you suck!') sent = ActionMailer::Base.deliveries.first assert_match /user_comment_test/, sent.body end @@ -40,18 +41,18 @@ class CommentNotifierTest < Test::Unit::TestCase should 'not deliver mail if notify comments is false' do @article.update_attribute(:notify_comments, false) assert_no_difference ActionMailer::Base.deliveries, :size do - @article.comments << Comment.new(:author => @profile, :title => 'test comment', :body => 'you suck!') + @article.comments << Comment.new(:author => @profile2, :title => 'test comment', :body => 'you suck!') end end should 'include comment title in the e-mail' do - @article.comments << Comment.new(:author => @profile, :title => 'comment title', :body => 'comment title') + @article.comments << Comment.new(:author => @profile2, :title => 'comment title', :body => 'comment title') sent = ActionMailer::Base.deliveries.first assert_match /comment title/, sent.body end should 'include comment text in the e-mail' do - @article.comments << Comment.new(:author => @profile, :title => 'comment title', :body => 'comment body') + @article.comments << Comment.new(:author => @profile2, :title => 'comment title', :body => 'comment body') sent = ActionMailer::Base.deliveries.first assert_match /comment body/, sent.body end @@ -65,12 +66,22 @@ class CommentNotifierTest < Test::Unit::TestCase end end - private + should 'not deliver mail to comments author' do + community = fast_create(Community) + community.add_admin @profile + community.add_admin @profile2 + + article = fast_create(Article, :name => 'Article test', :profile_id => community.id, :notify_comments => true) + article.comments << Comment.new(:author => @profile, :title => 'test comment', :body => 'there is no addresses to send notification') + sent = ActionMailer::Base.deliveries.first + assert_not_includes sent.to, @profile.email + end + + private def read_fixture(action) IO.readlines("#{FIXTURES_PATH}/mail_sender/#{action}") end - def encode(subject) quoted_printable(subject, CHARSET) end -- libgit2 0.21.2