From 57228636c4e40e1dbbf59fb130aa1e2431fabff1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 24 Aug 2015 17:47:09 +0200 Subject: [PATCH] adds as_moderator/administrator methods to comment --- app/models/comment.rb | 11 ++++++++++- spec/models/comment_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index 7c18efc6b..fc05349fa 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,10 +1,11 @@ class Comment < ActiveRecord::Base include ActsAsParanoidAliases acts_as_nested_set scope: [:commentable_id, :commentable_type], counter_cache: :children_count - acts_as_paranoid column: :hidden_at acts_as_votable + attr_accessor :comment_as_moderator, :comment_as_administrator + validates :body, presence: true validates :user, presence: true @@ -59,6 +60,14 @@ class Comment < ActiveRecord::Base reviewed_at.present? end + def as_administrator? + administrator_id.present? + end + + def as_moderator? + moderator_id.present? + end + def mark_as_reviewed update(reviewed_at: Time.now) end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 5af7afc1c..98420c000 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -38,4 +38,24 @@ describe Comment do expect { new_comment.destroy }.to change { comment.children_count }.from(1).to(0) end end + + describe "#as_administrator?" do + it "should be true if comment has administrator_id, false otherway" do + expect(comment).not_to be_as_administrator + + comment.administrator_id = 33 + + expect(comment).to be_as_administrator + end + end + + describe "#as_moderator?" do + it "should be true if comment has moderator_id, false otherway" do + expect(comment).not_to be_as_moderator + + comment.moderator_id = 21 + + expect(comment).to be_as_moderator + end + end end