Commit 08b5be5ba7e325f95a20518a65d9c6e07d5cdb1e
1 parent
5ae2a0b6
Exists in
master
and in
29 other branches
[comments-refactor-review] Encapsulating comment mark_as_spam check
Showing
3 changed files
with
41 additions
and
3 deletions
Show diff stats
app/controllers/public/comment_controller.rb
@@ -96,9 +96,7 @@ class CommentController < ApplicationController | @@ -96,9 +96,7 @@ class CommentController < ApplicationController | ||
96 | 96 | ||
97 | def mark_as_spam | 97 | def mark_as_spam |
98 | comment = profile.comments_received.find(params[:id]) | 98 | comment = profile.comments_received.find(params[:id]) |
99 | - could_mark_as_spam = (user == comment.profile || user.has_permission?(:moderate_comments, comment.profile)) | ||
100 | - | ||
101 | - if logged_in? && could_mark_as_spam | 99 | + if comment.can_be_marked_as_spam_by?(user) |
102 | comment.spam! | 100 | comment.spam! |
103 | render :text => {'ok' => true}.to_json, :content_type => 'application/json' | 101 | render :text => {'ok' => true}.to_json, :content_type => 'application/json' |
104 | else | 102 | else |
app/models/comment.rb
@@ -264,4 +264,9 @@ class Comment < ActiveRecord::Base | @@ -264,4 +264,9 @@ class Comment < ActiveRecord::Base | ||
264 | user == author || user == profile || user.has_permission?(:moderate_comments, profile) | 264 | user == author || user == profile || user.has_permission?(:moderate_comments, profile) |
265 | end | 265 | end |
266 | 266 | ||
267 | + def can_be_marked_as_spam_by?(user) | ||
268 | + return if user.nil? | ||
269 | + user == profile || user.has_permission?(:moderate_comments, profile) | ||
270 | + end | ||
271 | + | ||
267 | end | 272 | end |
test/unit/comment_test.rb
@@ -635,6 +635,41 @@ class CommentTest < ActiveSupport::TestCase | @@ -635,6 +635,41 @@ class CommentTest < ActiveSupport::TestCase | ||
635 | assert comment.can_be_destroyed_by?(user) | 635 | assert comment.can_be_destroyed_by?(user) |
636 | end | 636 | end |
637 | 637 | ||
638 | + should 'not be able to mark comment as spam without user' do | ||
639 | + comment = Comment.new | ||
640 | + | ||
641 | + assert !comment.can_be_marked_as_spam_by?(nil) | ||
642 | + end | ||
643 | + | ||
644 | + should 'not be able to mark comment as spam' do | ||
645 | + user = Person.new | ||
646 | + profile = Profile.new | ||
647 | + article = Article.new(:profile => profile) | ||
648 | + comment = Comment.new(:article => article) | ||
649 | + user.expects(:has_permission?).with(:moderate_comments, profile).returns(false) | ||
650 | + | ||
651 | + assert !comment.can_be_marked_as_spam_by?(user) | ||
652 | + end | ||
653 | + | ||
654 | + should 'be able to mark comment as spam if is the profile' do | ||
655 | + user = Person.new | ||
656 | + article = Article.new(:profile => user) | ||
657 | + comment = Comment.new(:article => article) | ||
658 | + | ||
659 | + assert comment.can_be_marked_as_spam_by?(user) | ||
660 | + end | ||
661 | + | ||
662 | + should 'be able to mark comment as spam if can moderate_comments on the profile' do | ||
663 | + user = Person.new | ||
664 | + profile = Profile.new | ||
665 | + article = Article.new(:profile => profile) | ||
666 | + comment = Comment.new(:article => article) | ||
667 | + | ||
668 | + user.expects(:has_permission?).with(:moderate_comments, profile).returns(true) | ||
669 | + | ||
670 | + assert comment.can_be_marked_as_spam_by?(user) | ||
671 | + end | ||
672 | + | ||
638 | private | 673 | private |
639 | 674 | ||
640 | def create_comment(args = {}) | 675 | def create_comment(args = {}) |