Commit c611bd07f572a8689af13135772ade863742914e
1 parent
06e38dcf
Exists in
master
and in
22 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,7 +85,12 @@ class Comment < ActiveRecord::Base | ||
| 85 | end | 85 | end |
| 86 | end | 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 | def notify_by_mail | 94 | def notify_by_mail |
| 90 | if source.kind_of?(Article) && article.notify_comments? | 95 | if source.kind_of?(Article) && article.notify_comments? |
| 91 | if !article.profile.notification_emails.empty? | 96 | if !article.profile.notification_emails.empty? |
| @@ -0,0 +1,24 @@ | @@ -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
| @@ -84,6 +84,7 @@ class CommentNotifierTest < ActiveSupport::TestCase | @@ -84,6 +84,7 @@ class CommentNotifierTest < ActiveSupport::TestCase | ||
| 84 | 84 | ||
| 85 | def create_comment_and_notify(args) | 85 | def create_comment_and_notify(args) |
| 86 | Comment.create!(args) | 86 | Comment.create!(args) |
| 87 | + process_delayed_job_queue | ||
| 87 | end | 88 | end |
| 88 | 89 | ||
| 89 | def read_fixture(action) | 90 | def read_fixture(action) |