From 15e4262a4d36ee68f63ce368fb2a1bea39fd3ff7 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Fri, 6 Sep 2013 02:43:22 +0000 Subject: [PATCH] rails3: fix comment_notifier --- app/mailers/comment_notifier.rb | 40 ++++++++++++++++++++++++++++++++++++++++ app/models/comment.rb | 38 ++------------------------------------ app/views/comment/notifier/mail.html.erb | 19 ------------------- app/views/comment/notifier/notification.html.erb | 19 +++++++++++++++++++ test/unit/comment_notifier_test.rb | 10 +++++----- 5 files changed, 66 insertions(+), 60 deletions(-) create mode 100644 app/mailers/comment_notifier.rb delete mode 100644 app/views/comment/notifier/mail.html.erb create mode 100644 app/views/comment/notifier/notification.html.erb diff --git a/app/mailers/comment_notifier.rb b/app/mailers/comment_notifier.rb new file mode 100644 index 0000000..8067d48 --- /dev/null +++ b/app/mailers/comment_notifier.rb @@ -0,0 +1,40 @@ +class Comment::Notifier < ActionMailer::Base + def notification(comment) + profile = comment.article.profile + @recipient = profile.nickname || profile.name + @sender = comment.author_name + @sender_link = comment.author_link + @article_title = comment.article.name + @comment_url = comment.url + @comment_title = comment.title + @comment_body = comment.body + @environment = profile.environment.name + @url = profile.environment.top_url + + mail( + to: comment.notification_emails, + from: "#{profile.environment.name} <#{profile.environment.contact_email}>", + subject: _("[%s] you got a new comment!") % [profile.environment.name] + ) + end + + def mail_to_followers(comment, emails) + profile = comment.article.profile + @recipient = profile.nickname || profile.name + @sender = comment.author_name + @sender_link = comment.author_link + @article_title = comment.article.name + @comment_url = comment.url + @unsubscribe_url = comment.article.view_url.merge({:unfollow => true}) + @comment_title = comment.title + @comment_body = comment.body + @environment = profile.environment.name + @url = profile.environment.top_url + + mail( + bcc: emails, + from: "#{profile.environment.name} <#{profile.environment.contact_email}>", + subject: _("[%s] %s commented on a content of %s") % [profile.environment.name, comment.author_name, profile.short_name] + ) + end +end diff --git a/app/models/comment.rb b/app/models/comment.rb index e2ee525..2917376 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -122,11 +122,11 @@ class Comment < ActiveRecord::Base def notify_by_mail if source.kind_of?(Article) && article.notify_comments? if !notification_emails.empty? - Comment::Notifier.deliver_mail(self) + Comment::Notifier.notification(self).deliver end emails = article.followers - [author_email] if !emails.empty? - Comment::Notifier.deliver_mail_to_followers(self, emails) + Comment::Notifier.mail_to_followers(self, emails).deliver end end end @@ -163,40 +163,6 @@ class Comment < ActiveRecord::Base body || '' end - class Notifier < ActionMailer::Base - def mail(comment) - profile = comment.article.profile - 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, - :sender => comment.author_name, - :sender_link => comment.author_link, - :article_title => comment.article.name, - :comment_url => comment.url, - :comment_title => comment.title, - :comment_body => comment.body, - :environment => profile.environment.name, - :url => profile.environment.top_url - end - def mail_to_followers(comment, emails) - profile = comment.article.profile - bcc emails - from "#{profile.environment.name} <#{profile.environment.contact_email}>" - subject _("[%s] %s commented on a content of %s") % [profile.environment.name, comment.author_name, profile.short_name] - body :recipient => profile.nickname || profile.name, - :sender => comment.author_name, - :sender_link => comment.author_link, - :article_title => comment.article.name, - :comment_url => comment.url, - :unsubscribe_url => comment.article.view_url.merge({:unfollow => true}), - :comment_title => comment.title, - :comment_body => comment.body, - :environment => profile.environment.name, - :url => profile.environment.top_url - end - end - def rejected? @rejected end diff --git a/app/views/comment/notifier/mail.html.erb b/app/views/comment/notifier/mail.html.erb deleted file mode 100644 index 5fd6d71..0000000 --- a/app/views/comment/notifier/mail.html.erb +++ /dev/null @@ -1,19 +0,0 @@ -<%= _('Hi, %{recipient}!') % { :recipient => @recipient } %> - -<%= word_wrap(_('%{sender} (%{sender_link}) created a new comment on your article "%{article_title}".') % { :sender => @sender, :sender_link => url_for(@sender_link), :article_title => @article_title }) %> - -<%= word_wrap(_('Title: %s') % @comment_title) %> - -<%= _("Comment:") %> -------------------------------------------------------------------------------- -<%= word_wrap(@comment_body) %> -------------------------------------------------------------------------------- - -<%= _('Access the address below to view this comment:') %> -<%= url_for @comment_url %> - -<%= _("Greetings,") %> - --- -<%= _('%s team.') % @environment %> -<%= url_for @url %> diff --git a/app/views/comment/notifier/notification.html.erb b/app/views/comment/notifier/notification.html.erb new file mode 100644 index 0000000..5fd6d71 --- /dev/null +++ b/app/views/comment/notifier/notification.html.erb @@ -0,0 +1,19 @@ +<%= _('Hi, %{recipient}!') % { :recipient => @recipient } %> + +<%= word_wrap(_('%{sender} (%{sender_link}) created a new comment on your article "%{article_title}".') % { :sender => @sender, :sender_link => url_for(@sender_link), :article_title => @article_title }) %> + +<%= word_wrap(_('Title: %s') % @comment_title) %> + +<%= _("Comment:") %> +------------------------------------------------------------------------------- +<%= word_wrap(@comment_body) %> +------------------------------------------------------------------------------- + +<%= _('Access the address below to view this comment:') %> +<%= url_for @comment_url %> + +<%= _("Greetings,") %> + +-- +<%= _('%s team.') % @environment %> +<%= url_for @url %> diff --git a/test/unit/comment_notifier_test.rb b/test/unit/comment_notifier_test.rb index e7a31ff..128672b 100644 --- a/test/unit/comment_notifier_test.rb +++ b/test/unit/comment_notifier_test.rb @@ -28,14 +28,14 @@ class CommentNotifierTest < ActiveSupport::TestCase should 'display author name in delivered mail' do create_comment_and_notify(:author => @author, :title => 'test comment', :body => 'you suck!', :source => @article) sent = ActionMailer::Base.deliveries.first - assert_match /#{@author.name}/, sent.body + assert_match /#{@author.name}/, sent.body.to_s end should 'display unauthenticated author name and email in delivered mail' do create_comment_and_notify(:name => 'flatline', :email => 'flatline@invalid.com', :title => 'test comment', :body => 'you suck!', :source => @article ) sent = ActionMailer::Base.deliveries.first - assert_match /flatline/, sent.body - assert_match /flatline@invalid.com/, sent.body + assert_match /flatline/, sent.body.to_s + assert_match /flatline@invalid.com/, sent.body.to_s end should 'not deliver mail if notify comments is false' do @@ -48,13 +48,13 @@ class CommentNotifierTest < ActiveSupport::TestCase should 'include comment title in the e-mail' do create_comment_and_notify(:author => @author, :title => 'comment title', :body => 'comment body', :source => @article) sent = ActionMailer::Base.deliveries.first - assert_match /comment title/, sent.body + assert_match /comment title/, sent.body.to_s end should 'include comment text in the e-mail' do create_comment_and_notify(:author => @author, :title => 'comment title', :body => 'comment body', :source => @article) sent = ActionMailer::Base.deliveries.first - assert_match /comment body/, sent.body + assert_match /comment body/, sent.body.to_s end should 'not deliver mail if has no notification emails' do -- libgit2 0.21.2