Commit f81c2116ae7c77fc4641dae3a16f535443ced16a

Authored by Rodrigo Souto
1 parent 63ea40d3

[comments-refactor-review] Checking permissions to show mark/unmark as spam link

Also a small logic fix.
app/helpers/comment_helper.rb
1 module CommentHelper 1 module CommentHelper
2 2
3 def article_title(article, args = {}) 3 def article_title(article, args = {})
  4 + title = article.title
4 title = article.display_title if article.kind_of?(UploadedFile) && article.image? 5 title = article.display_title if article.kind_of?(UploadedFile) && article.image?
5 - title = article.title if title.blank?  
6 title = content_tag('h1', h(title), :class => 'title') 6 title = content_tag('h1', h(title), :class => 'title')
7 if article.belongs_to_blog? 7 if article.belongs_to_blog?
8 unless args[:no_link] 8 unless args[:no_link]
@@ -21,7 +21,7 @@ module CommentHelper @@ -21,7 +21,7 @@ module CommentHelper
21 end 21 end
22 title 22 title
23 end 23 end
24 - 24 +
25 def comment_actions(comment) 25 def comment_actions(comment)
26 url = url_for(:profile => profile.identifier, :controller => :comment, :action => :check_actions, :id => comment.id) 26 url = url_for(:profile => profile.identifier, :controller => :comment, :action => :check_actions, :id => comment.id)
27 links = links_for_comment_actions(comment) 27 links = links_for_comment_actions(comment)
@@ -46,10 +46,12 @@ module CommentHelper @@ -46,10 +46,12 @@ module CommentHelper
46 end 46 end
47 47
48 def link_for_spam(comment) 48 def link_for_spam(comment)
49 - if comment.spam?  
50 - {:link => link_to_function(_('Mark as NOT SPAM'), 'remove_comment(this, %s); return false;' % url_for(:profile => profile.identifier, :mark_comment_as_ham => comment.id).to_json, :class => 'comment-footer comment-footer-link comment-footer-hide')}  
51 - elsif (logged_in? && (user == profile || user.has_permission?(:moderate_comments, profile)))  
52 - {:link => link_to_function(_('Mark as SPAM'), 'remove_comment(this, %s, %s); return false;' % [url_for(:profile => profile.identifier, :controller => 'comment', :action => :mark_as_spam, :id => comment.id).to_json, _('Are you sure you want to mark this comment as SPAM?').to_json], :class => 'comment-footer comment-footer-link comment-footer-hide')} 49 + if comment.can_be_marked_as_spam_by?(user)
  50 + if comment.spam?
  51 + {:link => link_to_function(_('Mark as NOT SPAM'), 'remove_comment(this, %s); return false;' % url_for(:profile => profile.identifier, :mark_comment_as_ham => comment.id).to_json, :class => 'comment-footer comment-footer-link comment-footer-hide')}
  52 + else
  53 + {:link => link_to_function(_('Mark as SPAM'), 'remove_comment(this, %s, %s); return false;' % [url_for(:profile => profile.identifier, :controller => 'comment', :action => :mark_as_spam, :id => comment.id).to_json, _('Are you sure you want to mark this comment as SPAM?').to_json], :class => 'comment-footer comment-footer-link comment-footer-hide')}
  54 + end
53 end 55 end
54 end 56 end
55 57
test/unit/comment_helper_test.rb
@@ -18,7 +18,8 @@ class CommentHelperTest < ActiveSupport::TestCase @@ -18,7 +18,8 @@ class CommentHelperTest < ActiveSupport::TestCase
18 attr_reader :user, :profile 18 attr_reader :user, :profile
19 19
20 should 'show menu if it has links for actions' do 20 should 'show menu if it has links for actions' do
21 - comment = Comment.new 21 + article = Article.new(:profile => profile)
  22 + comment = Comment.new(:article => article)
22 menu = comment_actions(comment) 23 menu = comment_actions(comment)
23 assert menu 24 assert menu
24 end 25 end
@@ -41,7 +42,8 @@ class CommentHelperTest < ActiveSupport::TestCase @@ -41,7 +42,8 @@ class CommentHelperTest < ActiveSupport::TestCase
41 end 42 end
42 43
43 should 'include actions of plugins in menu' do 44 should 'include actions of plugins in menu' do
44 - comment = Comment.new 45 + article = Article.new(:profile => profile)
  46 + comment = Comment.new(:article => article)
45 plugin_action = {:link => 'plugin_action'} 47 plugin_action = {:link => 'plugin_action'}
46 @plugins.stubs(:dispatch).returns([plugin_action]) 48 @plugins.stubs(:dispatch).returns([plugin_action])
47 links = links_for_comment_actions(comment) 49 links = links_for_comment_actions(comment)
@@ -49,7 +51,8 @@ class CommentHelperTest < ActiveSupport::TestCase @@ -49,7 +51,8 @@ class CommentHelperTest < ActiveSupport::TestCase
49 end 51 end
50 52
51 should 'include lambda actions of plugins in menu' do 53 should 'include lambda actions of plugins in menu' do
52 - comment = Comment.new 54 + article = Article.new(:profile => profile)
  55 + comment = Comment.new(:article => article)
53 plugin_action = lambda{[{:link => 'plugin_action'}, {:link => 'plugin_action2'}]} 56 plugin_action = lambda{[{:link => 'plugin_action'}, {:link => 'plugin_action2'}]}
54 @plugins.stubs(:dispatch).returns([plugin_action]) 57 @plugins.stubs(:dispatch).returns([plugin_action])
55 links = links_for_comment_actions(comment) 58 links = links_for_comment_actions(comment)
@@ -72,17 +75,34 @@ class CommentHelperTest < ActiveSupport::TestCase @@ -72,17 +75,34 @@ class CommentHelperTest < ActiveSupport::TestCase
72 75
73 should 'return link for mark comment as spam' do 76 should 'return link for mark comment as spam' do
74 comment = Comment.new 77 comment = Comment.new
  78 + comment.stubs(:can_be_marked_as_spam_by?).with(user).returns(true)
75 link = link_for_spam(comment) 79 link = link_for_spam(comment)
76 assert_match /Mark as SPAM/, link[:link] 80 assert_match /Mark as SPAM/, link[:link]
77 end 81 end
78 82
  83 + should 'not return link for mark comment as spam if user does not have the permissions' do
  84 + comment = Comment.new
  85 + comment.stubs(:can_be_marked_as_spam_by?).with(user).returns(false)
  86 + link = link_for_spam(comment)
  87 + assert_nil link
  88 + end
  89 +
79 should 'return link for mark comment as not spam' do 90 should 'return link for mark comment as not spam' do
80 comment = Comment.new 91 comment = Comment.new
81 comment.spam = true 92 comment.spam = true
  93 + comment.stubs(:can_be_marked_as_spam_by?).with(user).returns(true)
82 link = link_for_spam(comment) 94 link = link_for_spam(comment)
83 assert_match /Mark as NOT SPAM/, link[:link] 95 assert_match /Mark as NOT SPAM/, link[:link]
84 end 96 end
85 97
  98 + should 'not return link for mark comment as not spam if user does not have the permissions' do
  99 + comment = Comment.new
  100 + comment.spam = true
  101 + comment.stubs(:can_be_marked_as_spam_by?).with(user).returns(false)
  102 + link = link_for_spam(comment)
  103 + assert_nil link
  104 + end
  105 +
86 should 'do not return link for edit comment' do 106 should 'do not return link for edit comment' do
87 comment = Comment.new 107 comment = Comment.new
88 link = link_for_edit(comment) 108 link = link_for_edit(comment)