Commit aa6be22b2af8fec732f9ae4d46d33fba55aced79

Authored by Rodrigo Souto
1 parent 09e8041b

[comments-refactor-review] Encapsulating comment moderation check

app/controllers/public/comment_controller.rb
... ... @@ -57,7 +57,7 @@ class CommentController < ApplicationController
57 57 return
58 58 end
59 59  
60   - if @comment.article.moderate_comments? && !(@comment.author && @comment.author_id == @comment.article.author_id)
  60 + if @comment.need_moderation?
61 61 @comment.created_at = Time.now
62 62 ApproveComment.create!(:requestor => @comment.author, :target => profile, :comment_attributes => @comment.attributes.to_json)
63 63  
... ...
app/models/comment.rb
... ... @@ -255,4 +255,8 @@ class Comment < ActiveRecord::Base
255 255 plugins.dispatch(:comment_marked_as_ham, self)
256 256 end
257 257  
  258 + def need_moderation?
  259 + article.moderate_comments? && (author.nil? || article.author != author)
  260 + end
  261 +
258 262 end
... ...
test/unit/comment_test.rb
... ... @@ -550,6 +550,49 @@ class CommentTest < ActiveSupport::TestCase
550 550 SpammerLogger.clean_log
551 551 end
552 552  
  553 + should 'not need moderation if article is not moderated' do
  554 + article = Article.new
  555 + comment = Comment.new(:article => article)
  556 +
  557 + assert !comment.need_moderation?
  558 + end
  559 +
  560 + should 'not need moderation if the comment author is the article author' do
  561 + author = Person.new
  562 + article = Article.new
  563 +
  564 + article.stubs(:author).returns(author)
  565 + article.moderate_comments = true
  566 +
  567 + comment = Comment.new(:article => article)
  568 + comment.stubs(:author).returns(author)
  569 +
  570 + assert !comment.need_moderation?
  571 + end
  572 +
  573 + should 'need moderation if article is moderated and the comment has no author' do
  574 + article = Article.new
  575 + article.stubs(:moderate_comments?).returns(true)
  576 +
  577 + comment = Comment.new(:article => article)
  578 +
  579 + assert comment.need_moderation?
  580 + end
  581 +
  582 + should 'need moderation if article is moderated and the comment author is different from article author' do
  583 + article_author = Person.new
  584 + comment_author = Person.new
  585 +
  586 + article = Article.new
  587 + article.stubs(:author).returns(article_author)
  588 + article.stubs(:moderate_comments?).returns(true)
  589 +
  590 + comment = Comment.new(:article => article)
  591 + comment.stubs(:author).returns(comment_author)
  592 +
  593 + assert comment.need_moderation?
  594 + end
  595 +
553 596 private
554 597  
555 598 def create_comment(args = {})
... ...