From 246145e4cbc5f8301a01ffbd2b3116ed968b7f71 Mon Sep 17 00:00:00 2001 From: kikito Date: Thu, 20 Aug 2015 18:45:32 +0200 Subject: [PATCH] adds abilities for regular users --- app/models/ability.rb | 16 ++++++++++++++++ app/models/comment.rb | 4 ++++ spec/models/ability_spec.rb | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/app/models/ability.rb b/app/models/ability.rb index 9cce129a2..7c63c032f 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -16,6 +16,22 @@ class Ability can :create, Comment can :create, Debate + can :flag_as_inappropiate, Comment do |comment| + comment.author != user && !InappropiateFlag.flagged?(user, comment) + end + + can :undo_flag_as_inappropiate, Comment do |comment| + comment.author != user && InappropiateFlag.flagged?(user, comment) + end + + can :flag_as_inappropiate, Debate do |debate| + debate.author != user && !InappropiateFlag.flagged?(user, debate) + end + + can :undo_flag_as_inappropiate, Debate do |debate| + debate.author != user && InappropiateFlag.flagged?(user, debate) + end + unless user.organization? can :vote, Debate can :vote, Comment diff --git a/app/models/comment.rb b/app/models/comment.rb index 5cfe5a2bd..fd838d192 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -34,6 +34,10 @@ class Comment < ActiveRecord::Base user end + def author=(author) + self.user= author + end + def total_votes votes_for.size end diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 20a3456d4..a044c244e 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -22,12 +22,48 @@ describe Ability do it { should be_able_to(:show, debate) } it { should be_able_to(:vote, debate) } + it { should be_able_to(:show, user) } it { should be_able_to(:edit, user) } it { should be_able_to(:create, Comment) } it { should be_able_to(:vote, Comment) } + describe 'flagging content as inappropiate' do + it { should be_able_to(:flag_as_inappropiate, debate) } + it { should_not be_able_to(:undo_flag_as_inappropiate, debate) } + it { should be_able_to(:flag_as_inappropiate, comment) } + it { should_not be_able_to(:undo_flag_as_inappropiate, comment) } + + describe "own comments" do + let(:own_comment) { create(:comment, author: user) } + + it { should_not be_able_to(:flag_as_inappropiate, own_comment) } + it { should_not be_able_to(:undo_flag_as_inappropiate, own_comment) } + end + + describe "own debates" do + let(:own_debate) { create(:debate, author: user) } + + it { should_not be_able_to(:flag_as_inappropiate, own_debate) } + it { should_not be_able_to(:undo_flag_as_inappropiate, own_debate) } + end + + describe "already-flagged comments" do + before(:each) { InappropiateFlag.flag!(user, comment) } + + it { should_not be_able_to(:flag_as_inappropiate, comment) } + it { should be_able_to(:undo_flag_as_inappropiate, comment) } + end + + describe "already-flagged debates" do + before(:each) { InappropiateFlag.flag!(user, debate) } + + it { should_not be_able_to(:flag_as_inappropiate, debate) } + it { should be_able_to(:undo_flag_as_inappropiate, debate) } + end + end + describe "other users" do let(:other_user) { create(:user) } it { should_not be_able_to(:show, other_user) }