adds methods to decide if a user can vote a debate

This commit is contained in:
Juanjo Bazán
2015-08-31 12:58:06 +02:00
parent 916f0bf901
commit 2e5b5bccb5
3 changed files with 50 additions and 2 deletions

View File

@@ -31,7 +31,6 @@ class Debate < ActiveRecord::Base
scope :sort_by_total_votes, -> { reorder(cached_votes_total: :desc) }
scope :sort_by_likes , -> { reorder(cached_votes_up: :desc) }
scope :sort_by_created_at, -> { reorder(created_at: :desc) }
# Ahoy setup
visitable # Ahoy will automatically assign visit_id on create
@@ -56,6 +55,10 @@ class Debate < ActiveRecord::Base
cached_votes_total
end
def total_anonymous_votes
cached_anonymous_votes_total
end
def editable?
total_votes == 0
end
@@ -64,6 +67,14 @@ class Debate < ActiveRecord::Base
editable? && author == user
end
def votable_by?(user)
user.level_three_verified? || user.level_two_verified? || anonymous_votes_ratio < Setting.value_for('max_ratio_anon_votes_on_debates').to_i
end
def anonymous_votes_ratio
(cached_anonymous_votes_total.to_f / cached_votes_total) * 100
end
def description
super.try :html_safe
end

View File

@@ -1 +1 @@
$("#search-result").html("<div class=\"panel column\"><%= j render 'moderator', moderator: @moderator %></div>");
$("#search-result").html("<div class=\"panel column\"><%= j render 'moderator', moderator: @moderator %></div>");

View File

@@ -78,6 +78,36 @@ describe Debate do
end
end
describe "#votable_by?" do
let(:debate) { create(:debate) }
before(:all) do
create(:setting, key: "max_ratio_anon_votes_on_debates", value: "50")
end
it "should be true for level two verified users" do
user = create(:user, residence_verified_at: Time.now, confirmed_phone: "666333111")
expect(debate.votable_by?(user)).to be true
end
it "should be true for level three verified users" do
user = create(:user, verified_at: Time.now)
expect(debate.votable_by?(user)).to be true
end
it "should be true for anonymous users if allowed anonymous votes" do
debate.update(cached_anonymous_votes_total: 42, cached_votes_total: 100)
user = create(:user)
expect(debate.votable_by?(user)).to be true
end
it "should be false for anonymous users if too many anonymous votes" do
debate.update(cached_anonymous_votes_total: 52, cached_votes_total: 100)
user = create(:user)
expect(debate.votable_by?(user)).to be false
end
end
describe "#search" do
let!(:economy) { create(:debate, tag_list: "Economy") }
let!(:health) { create(:debate, tag_list: "Health") }
@@ -102,4 +132,11 @@ describe Debate do
end
end
describe '#anonymous_votes_ratio' do
it "returns the percentage of anonymous votes of the total votes" do
debate = create(:debate, cached_anonymous_votes_total: 25, cached_votes_total: 100)
expect(debate.anonymous_votes_ratio).to eq(25.0)
end
end
end