Commit 4955760191c2e8adc7488a0ceb2ce28b08672b63

Authored by Rodrigo Souto
1 parent cff3d5b7

[comments-refactor-review] Encapsulating comment update check

app/models/comment.rb
... ... @@ -269,4 +269,8 @@ class Comment < ActiveRecord::Base
269 269 user == profile || user.has_permission?(:moderate_comments, profile)
270 270 end
271 271  
  272 + def can_be_updated_by?(user)
  273 + user.present? && user == author
  274 + end
  275 +
272 276 end
... ...
test/unit/comment_test.rb
... ... @@ -670,6 +670,26 @@ class CommentTest < ActiveSupport::TestCase
670 670 assert comment.can_be_marked_as_spam_by?(user)
671 671 end
672 672  
  673 + should 'not be able to update comment without user' do
  674 + comment = Comment.new
  675 +
  676 + assert !comment.can_be_updated_by?(nil)
  677 + end
  678 +
  679 + should 'not be able to update comment' do
  680 + user = Person.new
  681 + comment = Comment.new
  682 +
  683 + assert !comment.can_be_updated_by?(user)
  684 + end
  685 +
  686 + should 'be able to update comment if is the author' do
  687 + user = Person.new
  688 + comment = Comment.new(:author => user)
  689 +
  690 + assert comment.can_be_updated_by?(user)
  691 + end
  692 +
673 693 private
674 694  
675 695 def create_comment(args = {})
... ...