Commit c611bd07f572a8689af13135772ade863742914e

Authored by Antonio Terceiro
1 parent 06e38dcf

Move comment notification into a background job

ActionItem2306
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?
... ...
app/models/comment_handler.rb 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +class CommentHandler < Struct.new(:comment_id)
  2 +
  3 + def perform
  4 + comment = Comment.find(comment_id)
  5 + comment.notify_by_mail
  6 + rescue ActiveRecord::RecordNotFound
  7 + # just ignore non-existing comments
  8 + end
  9 +
  10 +end
... ...
test/unit/comment_handler_test.rb 0 → 100644
... ... @@ -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 &lt; ActiveSupport::TestCase
84 84  
85 85 def create_comment_and_notify(args)
86 86 Comment.create!(args)
  87 + process_delayed_job_queue
87 88 end
88 89  
89 90 def read_fixture(action)
... ...