Skip to content

Commit

Permalink
changes rules for allowing debate votes
Browse files Browse the repository at this point in the history
first 100 votes are allowed for unverified users
  • Loading branch information
xuanxu committed Sep 2, 2015
1 parent 31a8c15 commit 274ddb2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 3 additions & 1 deletion app/models/debate.rb
Expand Up @@ -75,7 +75,9 @@ def register_vote(user, vote_value)
end

def votable_by?(user)
!user.unverified? ||
total_votes <= 100 ||
!user.unverified? ||
Setting.value_for('max_ratio_anon_votes_on_debates').to_i == 100 ||
anonymous_votes_ratio < Setting.value_for('max_ratio_anon_votes_on_debates').to_i ||
user.voted_for?(self)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/debates_controller_spec.rb
Expand Up @@ -35,7 +35,7 @@

it 'should not allow vote if user is not allowed' do
Setting.find_by(key: "max_ratio_anon_votes_on_debates").update(value: 0)
debate = create(:debate)
debate = create(:debate, cached_votes_total: 1000)
sign_in create(:user)

expect do
Expand Down
12 changes: 9 additions & 3 deletions spec/models/debate_spec.rb
Expand Up @@ -124,7 +124,7 @@
end

describe "from anonymous users when there are too many anonymous votes" do
before(:each) {debate.update(cached_anonymous_votes_total: 52, cached_votes_total: 100)}
before(:each) {debate.update(cached_anonymous_votes_total: 520, cached_votes_total: 1000)}

it "should not register vote " do
user = create(:user)
Expand Down Expand Up @@ -156,13 +156,19 @@
end

it "should be true for anonymous users if allowed anonymous votes" do
debate.update(cached_anonymous_votes_total: 42, cached_votes_total: 100)
debate.update(cached_anonymous_votes_total: 420, cached_votes_total: 1000)
user = create(:user)
expect(debate.votable_by?(user)).to be true
end

it "should be true for anonymous users if less than 100 votes" do
debate.update(cached_anonymous_votes_total: 90, cached_votes_total: 92)
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)
debate.update(cached_anonymous_votes_total: 520, cached_votes_total: 1000)
user = create(:user)
expect(debate.votable_by?(user)).to be false
end
Expand Down

0 comments on commit 274ddb2

Please sign in to comment.