From 5e32fd2fa404a55d9a33fea665d6738674d590bb Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 21 Aug 2015 20:39:40 +0200 Subject: [PATCH] Adds new abilities for moderators extends hiding (i.e. you can not hide your own content or yourself) and adds marking as reviewed --- app/models/ability.rb | 23 +++++++++++++++++++-- spec/models/ability_spec.rb | 41 +++++++++++++++++++++++++++++++------ 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index 6093a373b..9032038a1 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -2,9 +2,15 @@ class Ability include CanCan::Ability def initialize(user) + + # If someone can hide something, he can also hide it + # from the moderation screen + alias_action :hide_in_moderation_screen, to: :hide + # Not logged in users can :read, Debate + if user # logged-in users can [:read, :update], User, id: user.id @@ -42,9 +48,22 @@ class Ability can(:verify, Organization){ |o| !o.verified? } can(:reject, Organization){ |o| !o.rejected? } - can :hide, Comment - can :hide, Debate + can :read, Comment + + can :hide, Comment, hidden_at: nil + cannot :hide, Comment, user_id: user.id + + can :mark_as_reviewed, Comment, reviewed_at: nil, hidden_at: nil + cannot :mark_as_reviewed, Comment, user_id: user.id + + can :hide, Debate, hidden_at: nil + cannot :hide, Debate, author_id: user.id + + can :mark_as_reviewed, Debate, reviewed_at: nil, hidden_at: nil + cannot :mark_as_reviewed, Debate, author_id: user.id + can :hide, User + cannot :hide, User, id: user.id end if user.administrator? diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 337b52507..cc458857c 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -102,6 +102,7 @@ describe Ability do before { create(:moderator, user: user) } let(:other_user) { create(:user) } + it { should be_able_to(:index, Debate) } it { should be_able_to(:show, debate) } it { should be_able_to(:vote, debate) } @@ -123,13 +124,41 @@ describe Ability do it { should be_able_to( :verify, rejected_organization) } end - it { should be_able_to(:hide, comment) } - it { should be_able_to(:hide, debate) } - it { should be_able_to(:hide, other_user) } + describe "hiding, reviewing and restoring" do + let(:own_comment) { create(:comment, author: user) } + let(:own_debate) { create(:debate, author: user) } + let(:hidden_comment) { create(:comment, hidden_at: Time.now) } + let(:hidden_debate) { create(:debate, hidden_at: Time.now) } + let(:reviewed_comment) { create(:comment, reviewed_at: Time.now) } + let(:reviewed_debate) { create(:debate, reviewed_at: Time.now) } - it { should_not be_able_to(:restore, comment) } - it { should_not be_able_to(:restore, debate) } - it { should_not be_able_to(:restore, other_user) } + it { should be_able_to(:hide, comment) } + it { should be_able_to(:hide_in_moderation_screen, comment) } + it { should_not be_able_to(:hide, hidden_comment) } + it { should_not be_able_to(:hide, own_comment) } + + it { should be_able_to(:hide, debate) } + it { should be_able_to(:hide_in_moderation_screen, debate) } + it { should_not be_able_to(:hide, hidden_debate) } + it { should_not be_able_to(:hide, own_debate) } + + it { should be_able_to(:mark_as_reviewed, comment) } + it { should_not be_able_to(:mark_as_reviewed, hidden_comment) } + it { should_not be_able_to(:mark_as_reviewed, reviewed_comment) } + it { should_not be_able_to(:mark_as_reviewed, own_comment) } + + it { should be_able_to(:mark_as_reviewed, debate) } + it { should_not be_able_to(:mark_as_reviewed, hidden_debate) } + it { should_not be_able_to(:mark_as_reviewed, reviewed_debate) } + it { should_not be_able_to(:mark_as_reviewed, own_debate) } + + it { should_not be_able_to(:hide, user) } + it { should be_able_to(:hide, other_user) } + + it { should_not be_able_to(:restore, comment) } + it { should_not be_able_to(:restore, debate) } + it { should_not be_able_to(:restore, other_user) } + end end describe "Administrator" do