Commit 15e4262a4d36ee68f63ce368fb2a1bea39fd3ff7
1 parent
4d9ad46b
Exists in
master
and in
27 other branches
rails3: fix comment_notifier
Showing
5 changed files
with
66 additions
and
60 deletions
Show diff stats
... | ... | @@ -0,0 +1,40 @@ |
1 | +class Comment::Notifier < ActionMailer::Base | |
2 | + def notification(comment) | |
3 | + profile = comment.article.profile | |
4 | + @recipient = profile.nickname || profile.name | |
5 | + @sender = comment.author_name | |
6 | + @sender_link = comment.author_link | |
7 | + @article_title = comment.article.name | |
8 | + @comment_url = comment.url | |
9 | + @comment_title = comment.title | |
10 | + @comment_body = comment.body | |
11 | + @environment = profile.environment.name | |
12 | + @url = profile.environment.top_url | |
13 | + | |
14 | + mail( | |
15 | + to: comment.notification_emails, | |
16 | + from: "#{profile.environment.name} <#{profile.environment.contact_email}>", | |
17 | + subject: _("[%s] you got a new comment!") % [profile.environment.name] | |
18 | + ) | |
19 | + end | |
20 | + | |
21 | + def mail_to_followers(comment, emails) | |
22 | + profile = comment.article.profile | |
23 | + @recipient = profile.nickname || profile.name | |
24 | + @sender = comment.author_name | |
25 | + @sender_link = comment.author_link | |
26 | + @article_title = comment.article.name | |
27 | + @comment_url = comment.url | |
28 | + @unsubscribe_url = comment.article.view_url.merge({:unfollow => true}) | |
29 | + @comment_title = comment.title | |
30 | + @comment_body = comment.body | |
31 | + @environment = profile.environment.name | |
32 | + @url = profile.environment.top_url | |
33 | + | |
34 | + mail( | |
35 | + bcc: emails, | |
36 | + from: "#{profile.environment.name} <#{profile.environment.contact_email}>", | |
37 | + subject: _("[%s] %s commented on a content of %s") % [profile.environment.name, comment.author_name, profile.short_name] | |
38 | + ) | |
39 | + end | |
40 | +end | ... | ... |
app/models/comment.rb
... | ... | @@ -122,11 +122,11 @@ class Comment < ActiveRecord::Base |
122 | 122 | def notify_by_mail |
123 | 123 | if source.kind_of?(Article) && article.notify_comments? |
124 | 124 | if !notification_emails.empty? |
125 | - Comment::Notifier.deliver_mail(self) | |
125 | + Comment::Notifier.notification(self).deliver | |
126 | 126 | end |
127 | 127 | emails = article.followers - [author_email] |
128 | 128 | if !emails.empty? |
129 | - Comment::Notifier.deliver_mail_to_followers(self, emails) | |
129 | + Comment::Notifier.mail_to_followers(self, emails).deliver | |
130 | 130 | end |
131 | 131 | end |
132 | 132 | end |
... | ... | @@ -163,40 +163,6 @@ class Comment < ActiveRecord::Base |
163 | 163 | body || '' |
164 | 164 | end |
165 | 165 | |
166 | - class Notifier < ActionMailer::Base | |
167 | - def mail(comment) | |
168 | - profile = comment.article.profile | |
169 | - recipients comment.notification_emails | |
170 | - from "#{profile.environment.name} <#{profile.environment.contact_email}>" | |
171 | - subject _("[%s] you got a new comment!") % [profile.environment.name] | |
172 | - body :recipient => profile.nickname || profile.name, | |
173 | - :sender => comment.author_name, | |
174 | - :sender_link => comment.author_link, | |
175 | - :article_title => comment.article.name, | |
176 | - :comment_url => comment.url, | |
177 | - :comment_title => comment.title, | |
178 | - :comment_body => comment.body, | |
179 | - :environment => profile.environment.name, | |
180 | - :url => profile.environment.top_url | |
181 | - end | |
182 | - def mail_to_followers(comment, emails) | |
183 | - profile = comment.article.profile | |
184 | - bcc emails | |
185 | - from "#{profile.environment.name} <#{profile.environment.contact_email}>" | |
186 | - subject _("[%s] %s commented on a content of %s") % [profile.environment.name, comment.author_name, profile.short_name] | |
187 | - body :recipient => profile.nickname || profile.name, | |
188 | - :sender => comment.author_name, | |
189 | - :sender_link => comment.author_link, | |
190 | - :article_title => comment.article.name, | |
191 | - :comment_url => comment.url, | |
192 | - :unsubscribe_url => comment.article.view_url.merge({:unfollow => true}), | |
193 | - :comment_title => comment.title, | |
194 | - :comment_body => comment.body, | |
195 | - :environment => profile.environment.name, | |
196 | - :url => profile.environment.top_url | |
197 | - end | |
198 | - end | |
199 | - | |
200 | 166 | def rejected? |
201 | 167 | @rejected |
202 | 168 | end | ... | ... |
app/views/comment/notifier/mail.html.erb
... | ... | @@ -1,19 +0,0 @@ |
1 | -<%= _('Hi, %{recipient}!') % { :recipient => @recipient } %> | |
2 | - | |
3 | -<%= 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 }) %> | |
4 | - | |
5 | -<%= word_wrap(_('Title: %s') % @comment_title) %> | |
6 | - | |
7 | -<%= _("Comment:") %> | |
8 | -------------------------------------------------------------------------------- | |
9 | -<%= word_wrap(@comment_body) %> | |
10 | -------------------------------------------------------------------------------- | |
11 | - | |
12 | -<%= _('Access the address below to view this comment:') %> | |
13 | -<%= url_for @comment_url %> | |
14 | - | |
15 | -<%= _("Greetings,") %> | |
16 | - | |
17 | --- | |
18 | -<%= _('%s team.') % @environment %> | |
19 | -<%= url_for @url %> |
... | ... | @@ -0,0 +1,19 @@ |
1 | +<%= _('Hi, %{recipient}!') % { :recipient => @recipient } %> | |
2 | + | |
3 | +<%= 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 }) %> | |
4 | + | |
5 | +<%= word_wrap(_('Title: %s') % @comment_title) %> | |
6 | + | |
7 | +<%= _("Comment:") %> | |
8 | +------------------------------------------------------------------------------- | |
9 | +<%= word_wrap(@comment_body) %> | |
10 | +------------------------------------------------------------------------------- | |
11 | + | |
12 | +<%= _('Access the address below to view this comment:') %> | |
13 | +<%= url_for @comment_url %> | |
14 | + | |
15 | +<%= _("Greetings,") %> | |
16 | + | |
17 | +-- | |
18 | +<%= _('%s team.') % @environment %> | |
19 | +<%= url_for @url %> | ... | ... |
test/unit/comment_notifier_test.rb
... | ... | @@ -28,14 +28,14 @@ class CommentNotifierTest < ActiveSupport::TestCase |
28 | 28 | should 'display author name in delivered mail' do |
29 | 29 | create_comment_and_notify(:author => @author, :title => 'test comment', :body => 'you suck!', :source => @article) |
30 | 30 | sent = ActionMailer::Base.deliveries.first |
31 | - assert_match /#{@author.name}/, sent.body | |
31 | + assert_match /#{@author.name}/, sent.body.to_s | |
32 | 32 | end |
33 | 33 | |
34 | 34 | should 'display unauthenticated author name and email in delivered mail' do |
35 | 35 | create_comment_and_notify(:name => 'flatline', :email => 'flatline@invalid.com', :title => 'test comment', :body => 'you suck!', :source => @article ) |
36 | 36 | sent = ActionMailer::Base.deliveries.first |
37 | - assert_match /flatline/, sent.body | |
38 | - assert_match /flatline@invalid.com/, sent.body | |
37 | + assert_match /flatline/, sent.body.to_s | |
38 | + assert_match /flatline@invalid.com/, sent.body.to_s | |
39 | 39 | end |
40 | 40 | |
41 | 41 | should 'not deliver mail if notify comments is false' do |
... | ... | @@ -48,13 +48,13 @@ class CommentNotifierTest < ActiveSupport::TestCase |
48 | 48 | should 'include comment title in the e-mail' do |
49 | 49 | create_comment_and_notify(:author => @author, :title => 'comment title', :body => 'comment body', :source => @article) |
50 | 50 | sent = ActionMailer::Base.deliveries.first |
51 | - assert_match /comment title/, sent.body | |
51 | + assert_match /comment title/, sent.body.to_s | |
52 | 52 | end |
53 | 53 | |
54 | 54 | should 'include comment text in the e-mail' do |
55 | 55 | create_comment_and_notify(:author => @author, :title => 'comment title', :body => 'comment body', :source => @article) |
56 | 56 | sent = ActionMailer::Base.deliveries.first |
57 | - assert_match /comment body/, sent.body | |
57 | + assert_match /comment body/, sent.body.to_s | |
58 | 58 | end |
59 | 59 | |
60 | 60 | should 'not deliver mail if has no notification emails' do | ... | ... |