From 15720094d082994f46253c5eb0b8728bfe5c3844 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Fri, 12 Jul 2013 19:11:55 -0300 Subject: [PATCH] [comments-refactor-review] Checking permissions to show remove link --- app/helpers/comment_helper.rb | 2 +- test/unit/comment_helper_test.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/helpers/comment_helper.rb b/app/helpers/comment_helper.rb index c1b7f13..e6095a7 100644 --- a/app/helpers/comment_helper.rb +++ b/app/helpers/comment_helper.rb @@ -62,7 +62,7 @@ module CommentHelper end def link_for_remove(comment) - if logged_in? && (user == profile || user == comment.author || user.has_permission?(:moderate_comments, profile)) + if comment.can_be_destroyed_by?(user) {:link => link_to_function(_('Remove'), 'remove_comment(this, %s, %s); return false ;' % [url_for(:profile => profile.identifier, :controller => 'comment', :action => :destroy, :id => comment.id).to_json, _('Are you sure you want to remove this comment and all its replies?').to_json], :class => 'comment-footer comment-footer-link comment-footer-hide remove-children')} end end diff --git a/test/unit/comment_helper_test.rb b/test/unit/comment_helper_test.rb index ddc1c54..a2b1b2f 100644 --- a/test/unit/comment_helper_test.rb +++ b/test/unit/comment_helper_test.rb @@ -117,6 +117,20 @@ class CommentHelperTest < ActiveSupport::TestCase assert link end + should 'do not return link for remove comment' do + comment = Comment.new + comment.stubs(:can_be_destroyed_by?).with(user).returns(false) + link = link_for_remove(comment) + assert_nil link + end + + should 'return link for remove comment' do + comment = Comment.new + comment.stubs(:can_be_destroyed_by?).with(user).returns(true) + link = link_for_remove(comment) + assert link + end + def link_to_function(content, url, options = {}) link_to(content, url, options) end -- libgit2 0.21.2