Commit 4955760191c2e8adc7488a0ceb2ce28b08672b63
1 parent
cff3d5b7
Exists in
master
and in
22 other branches
[comments-refactor-review] Encapsulating comment update check
Showing
2 changed files
with
24 additions
and
0 deletions
Show diff stats
app/models/comment.rb
| @@ -269,4 +269,8 @@ class Comment < ActiveRecord::Base | @@ -269,4 +269,8 @@ class Comment < ActiveRecord::Base | ||
| 269 | user == profile || user.has_permission?(:moderate_comments, profile) | 269 | user == profile || user.has_permission?(:moderate_comments, profile) |
| 270 | end | 270 | end |
| 271 | 271 | ||
| 272 | + def can_be_updated_by?(user) | ||
| 273 | + user.present? && user == author | ||
| 274 | + end | ||
| 275 | + | ||
| 272 | end | 276 | end |
test/unit/comment_test.rb
| @@ -670,6 +670,26 @@ class CommentTest < ActiveSupport::TestCase | @@ -670,6 +670,26 @@ class CommentTest < ActiveSupport::TestCase | ||
| 670 | assert comment.can_be_marked_as_spam_by?(user) | 670 | assert comment.can_be_marked_as_spam_by?(user) |
| 671 | end | 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 | private | 693 | private |
| 674 | 694 | ||
| 675 | def create_comment(args = {}) | 695 | def create_comment(args = {}) |