Commit c611bd07f572a8689af13135772ade863742914e
1 parent
06e38dcf
Exists in
master
and in
28 other branches
Move comment notification into a background job
ActionItem2306
Showing
4 changed files
with
41 additions
and
1 deletions
Show diff stats
app/models/comment.rb
... | ... | @@ -85,7 +85,12 @@ class Comment < ActiveRecord::Base |
85 | 85 | end |
86 | 86 | end |
87 | 87 | |
88 | - after_create :notify_by_mail | |
88 | + after_create :schedule_notification | |
89 | + | |
90 | + def schedule_notification | |
91 | + Delayed::Job.enqueue CommentHandler.new(self.id) | |
92 | + end | |
93 | + | |
89 | 94 | def notify_by_mail |
90 | 95 | if source.kind_of?(Article) && article.notify_comments? |
91 | 96 | if !article.profile.notification_emails.empty? | ... | ... |
... | ... | @@ -0,0 +1,24 @@ |
1 | +require File.dirname(__FILE__) + '/../test_helper' | |
2 | + | |
3 | +class CommentHandlerTest < ActiveSupport::TestCase | |
4 | + | |
5 | + should 'receive comment id' do | |
6 | + handler = CommentHandler.new(99) | |
7 | + assert_equal 99, handler.comment_id | |
8 | + end | |
9 | + | |
10 | + should 'not crash with unexisting comment' do | |
11 | + handler = CommentHandler.new(-1) | |
12 | + handler.perform | |
13 | + end | |
14 | + | |
15 | + should 'call Comment#notify_by_mail' do | |
16 | + handler = CommentHandler.new(-1) | |
17 | + comment = Comment.new | |
18 | + Comment.stubs(:find).with(-1).returns(comment) | |
19 | + comment.expects(:notify_by_mail) | |
20 | + | |
21 | + handler.perform | |
22 | + end | |
23 | + | |
24 | +end | ... | ... |
test/unit/comment_notifier_test.rb