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 1 module CommentHelper
2 2  
3 3 def article_title(article, args = {})
  4 + title = article.title
4 5 title = article.display_title if article.kind_of?(UploadedFile) && article.image?
5   - title = article.title if title.blank?
6 6 title = content_tag('h1', h(title), :class => 'title')
7 7 if article.belongs_to_blog?
8 8 unless args[:no_link]
... ... @@ -21,7 +21,7 @@ module CommentHelper
21 21 end
22 22 title
23 23 end
24   -
  24 +
25 25 def comment_actions(comment)
26 26 url = url_for(:profile => profile.identifier, :controller => :comment, :action => :check_actions, :id => comment.id)
27 27 links = links_for_comment_actions(comment)
... ... @@ -46,10 +46,12 @@ module CommentHelper
46 46 end
47 47  
48 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 55 end
54 56 end
55 57  
... ...
test/unit/comment_helper_test.rb
... ... @@ -18,7 +18,8 @@ class CommentHelperTest < ActiveSupport::TestCase
18 18 attr_reader :user, :profile
19 19  
20 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 23 menu = comment_actions(comment)
23 24 assert menu
24 25 end
... ... @@ -41,7 +42,8 @@ class CommentHelperTest < ActiveSupport::TestCase
41 42 end
42 43  
43 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 47 plugin_action = {:link => 'plugin_action'}
46 48 @plugins.stubs(:dispatch).returns([plugin_action])
47 49 links = links_for_comment_actions(comment)
... ... @@ -49,7 +51,8 @@ class CommentHelperTest < ActiveSupport::TestCase
49 51 end
50 52  
51 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 56 plugin_action = lambda{[{:link => 'plugin_action'}, {:link => 'plugin_action2'}]}
54 57 @plugins.stubs(:dispatch).returns([plugin_action])
55 58 links = links_for_comment_actions(comment)
... ... @@ -72,17 +75,34 @@ class CommentHelperTest < ActiveSupport::TestCase
72 75  
73 76 should 'return link for mark comment as spam' do
74 77 comment = Comment.new
  78 + comment.stubs(:can_be_marked_as_spam_by?).with(user).returns(true)
75 79 link = link_for_spam(comment)
76 80 assert_match /Mark as SPAM/, link[:link]
77 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 90 should 'return link for mark comment as not spam' do
80 91 comment = Comment.new
81 92 comment.spam = true
  93 + comment.stubs(:can_be_marked_as_spam_by?).with(user).returns(true)
82 94 link = link_for_spam(comment)
83 95 assert_match /Mark as NOT SPAM/, link[:link]
84 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 106 should 'do not return link for edit comment' do
87 107 comment = Comment.new
88 108 link = link_for_edit(comment)
... ...