From 4955760191c2e8adc7488a0ceb2ce28b08672b63 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Fri, 12 Jul 2013 16:39:23 -0300 Subject: [PATCH] [comments-refactor-review] Encapsulating comment update check --- app/models/comment.rb | 4 ++++ test/unit/comment_test.rb | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 0 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index 22c9d1f..d8461bb 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -269,4 +269,8 @@ class Comment < ActiveRecord::Base user == profile || user.has_permission?(:moderate_comments, profile) end + def can_be_updated_by?(user) + user.present? && user == author + end + end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 4c3cbf1..aac949f 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -670,6 +670,26 @@ class CommentTest < ActiveSupport::TestCase assert comment.can_be_marked_as_spam_by?(user) end + should 'not be able to update comment without user' do + comment = Comment.new + + assert !comment.can_be_updated_by?(nil) + end + + should 'not be able to update comment' do + user = Person.new + comment = Comment.new + + assert !comment.can_be_updated_by?(user) + end + + should 'be able to update comment if is the author' do + user = Person.new + comment = Comment.new(:author => user) + + assert comment.can_be_updated_by?(user) + end + private def create_comment(args = {}) -- libgit2 0.21.2