Commit 59bb951ff6847c3e89f24c4658a6a78ce20ca8ff

Authored by Pius Uzamere
1 parent 7e22d490

add an explicit loss count

Showing 2 changed files with 10 additions and 1 deletions   Show diff stats
app/models/choice.rb
@@ -16,6 +16,11 @@ class Choice < ActiveRecord::Base @@ -16,6 +16,11 @@ class Choice < ActiveRecord::Base
16 item.data 16 item.data
17 end 17 end
18 18
  19 + def lose!
  20 + self.loss_count += 1
  21 + save!
  22 + end
  23 +
19 def wins_plus_losses 24 def wins_plus_losses
20 #(prompts_on_the_left.collect(&:votes_count).sum + prompts_on_the_right.collect(&:votes_count).sum) 25 #(prompts_on_the_left.collect(&:votes_count).sum + prompts_on_the_right.collect(&:votes_count).sum)
21 Prompt.sum('votes_count', :conditions => "left_choice_id = #{id} OR right_choice_id = #{id}") 26 Prompt.sum('votes_count', :conditions => "left_choice_id = #{id} OR right_choice_id = #{id}")
app/models/visitor.rb
@@ -13,12 +13,16 @@ class Visitor < ActiveRecord::Base @@ -13,12 +13,16 @@ class Visitor < ActiveRecord::Base
13 end 13 end
14 14
15 def vote_for!(prompt, ordinality) 15 def vote_for!(prompt, ordinality)
16 - choice = prompt.choices[ordinality] #we need to guarantee that the choices are in the right order (by position) 16 + choices = prompt.choices
  17 + choice = choices[ordinality] #we need to guarantee that the choices are in the right order (by position)
17 prompt_vote = votes.create!(:voteable => prompt) 18 prompt_vote = votes.create!(:voteable => prompt)
18 choice_vote = votes.create!(:voteable => choice) 19 choice_vote = votes.create!(:voteable => choice)
19 choice.save! 20 choice.save!
20 choice.score = choice.compute_score 21 choice.score = choice.compute_score
21 choice.save! 22 choice.save!
  23 +
  24 + other_choices = choices - [choice]
  25 + other_choices.each {|c| c.lose! }
22 end 26 end
23 27
24 def skip!(prompt) 28 def skip!(prompt)