Commit 1b94cf38fbda42ea5f85061059fd855d99a5a787

Authored by Antonio Terceiro
1 parent 149b1d38

Move marking as spam and ham into the background

ActionItem2306
app/models/comment.rb
... ... @@ -91,7 +91,7 @@ class Comment < ActiveRecord::Base
91 91 after_create :schedule_notification
92 92  
93 93 def schedule_notification
94   - Delayed::Job.enqueue CommentHandler.new(self.id)
  94 + Delayed::Job.enqueue CommentHandler.new(self.id, :verify_and_notify)
95 95 end
96 96  
97 97 delegate :environment, :to => :profile
... ... @@ -214,15 +214,23 @@ class Comment < ActiveRecord::Base
214 214 def spam!
215 215 self.spam = true
216 216 self.save!
217   - plugins.dispatch(:comment_marked_as_spam, self)
  217 + Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_spam))
218 218 self
219 219 end
220 220  
221 221 def ham!
222 222 self.spam = false
223 223 self.save!
224   - plugins.dispatch(:comment_marked_as_ham, self)
  224 + Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_ham))
225 225 self
226 226 end
227 227  
  228 + def marked_as_spam
  229 + plugins.dispatch(:comment_marked_as_spam, self)
  230 + end
  231 +
  232 + def marked_as_ham
  233 + plugins.dispatch(:comment_marked_as_ham, self)
  234 + end
  235 +
228 236 end
... ...
app/models/comment_handler.rb
1   -class CommentHandler < Struct.new(:comment_id)
  1 +class CommentHandler < Struct.new(:comment_id, :method)
2 2  
3 3 def perform
4 4 comment = Comment.find(comment_id)
5   - comment.verify_and_notify
  5 + comment.send(method)
6 6 rescue ActiveRecord::RecordNotFound
7 7 # just ignore non-existing comments
8 8 end
... ...
test/unit/comment_handler_test.rb
... ... @@ -12,11 +12,11 @@ class CommentHandlerTest &lt; ActiveSupport::TestCase
12 12 handler.perform
13 13 end
14 14  
15   - should 'call Comment#notify_by_mail' do
16   - handler = CommentHandler.new(-1)
  15 + should 'call Comment#whatever_method' do
  16 + handler = CommentHandler.new(-1, :whatever_method)
17 17 comment = Comment.new
18 18 Comment.stubs(:find).with(-1).returns(comment)
19   - comment.expects(:verify_and_notify)
  19 + comment.expects(:whatever_method)
20 20  
21 21 handler.perform
22 22 end
... ...
test/unit/comment_test.rb
... ... @@ -517,6 +517,7 @@ class CommentTest &lt; ActiveSupport::TestCase
517 517 c = create_comment
518 518  
519 519 c.spam!
  520 + process_delayed_job_queue
520 521  
521 522 assert_equal c, SpamNotification.marked_as_spam
522 523 end
... ... @@ -527,6 +528,7 @@ class CommentTest &lt; ActiveSupport::TestCase
527 528 c = create_comment
528 529  
529 530 c.ham!
  531 + process_delayed_job_queue
530 532  
531 533 assert_equal c, SpamNotification.marked_as_ham
532 534 end
... ...