Commit 1b94cf38fbda42ea5f85061059fd855d99a5a787
1 parent
149b1d38
Exists in
master
and in
23 other branches
Move marking as spam and ham into the background
ActionItem2306
Showing
4 changed files
with
18 additions
and
8 deletions
Show diff stats
app/models/comment.rb
| @@ -91,7 +91,7 @@ class Comment < ActiveRecord::Base | @@ -91,7 +91,7 @@ class Comment < ActiveRecord::Base | ||
| 91 | after_create :schedule_notification | 91 | after_create :schedule_notification |
| 92 | 92 | ||
| 93 | def schedule_notification | 93 | def schedule_notification |
| 94 | - Delayed::Job.enqueue CommentHandler.new(self.id) | 94 | + Delayed::Job.enqueue CommentHandler.new(self.id, :verify_and_notify) |
| 95 | end | 95 | end |
| 96 | 96 | ||
| 97 | delegate :environment, :to => :profile | 97 | delegate :environment, :to => :profile |
| @@ -214,15 +214,23 @@ class Comment < ActiveRecord::Base | @@ -214,15 +214,23 @@ class Comment < ActiveRecord::Base | ||
| 214 | def spam! | 214 | def spam! |
| 215 | self.spam = true | 215 | self.spam = true |
| 216 | self.save! | 216 | self.save! |
| 217 | - plugins.dispatch(:comment_marked_as_spam, self) | 217 | + Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_spam)) |
| 218 | self | 218 | self |
| 219 | end | 219 | end |
| 220 | 220 | ||
| 221 | def ham! | 221 | def ham! |
| 222 | self.spam = false | 222 | self.spam = false |
| 223 | self.save! | 223 | self.save! |
| 224 | - plugins.dispatch(:comment_marked_as_ham, self) | 224 | + Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_ham)) |
| 225 | self | 225 | self |
| 226 | end | 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 | end | 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 | def perform | 3 | def perform |
| 4 | comment = Comment.find(comment_id) | 4 | comment = Comment.find(comment_id) |
| 5 | - comment.verify_and_notify | 5 | + comment.send(method) |
| 6 | rescue ActiveRecord::RecordNotFound | 6 | rescue ActiveRecord::RecordNotFound |
| 7 | # just ignore non-existing comments | 7 | # just ignore non-existing comments |
| 8 | end | 8 | end |
test/unit/comment_handler_test.rb
| @@ -12,11 +12,11 @@ class CommentHandlerTest < ActiveSupport::TestCase | @@ -12,11 +12,11 @@ class CommentHandlerTest < ActiveSupport::TestCase | ||
| 12 | handler.perform | 12 | handler.perform |
| 13 | end | 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 | comment = Comment.new | 17 | comment = Comment.new |
| 18 | Comment.stubs(:find).with(-1).returns(comment) | 18 | Comment.stubs(:find).with(-1).returns(comment) |
| 19 | - comment.expects(:verify_and_notify) | 19 | + comment.expects(:whatever_method) |
| 20 | 20 | ||
| 21 | handler.perform | 21 | handler.perform |
| 22 | end | 22 | end |
test/unit/comment_test.rb
| @@ -517,6 +517,7 @@ class CommentTest < ActiveSupport::TestCase | @@ -517,6 +517,7 @@ class CommentTest < ActiveSupport::TestCase | ||
| 517 | c = create_comment | 517 | c = create_comment |
| 518 | 518 | ||
| 519 | c.spam! | 519 | c.spam! |
| 520 | + process_delayed_job_queue | ||
| 520 | 521 | ||
| 521 | assert_equal c, SpamNotification.marked_as_spam | 522 | assert_equal c, SpamNotification.marked_as_spam |
| 522 | end | 523 | end |
| @@ -527,6 +528,7 @@ class CommentTest < ActiveSupport::TestCase | @@ -527,6 +528,7 @@ class CommentTest < ActiveSupport::TestCase | ||
| 527 | c = create_comment | 528 | c = create_comment |
| 528 | 529 | ||
| 529 | c.ham! | 530 | c.ham! |
| 531 | + process_delayed_job_queue | ||
| 530 | 532 | ||
| 531 | assert_equal c, SpamNotification.marked_as_ham | 533 | assert_equal c, SpamNotification.marked_as_ham |
| 532 | end | 534 | end |