Commit fdc546eb1792df6371c2bb8f16f8ec1221b8c8cd

Authored by Dhruv Kapadia
1 parent 60ada0c5

Choice scores no longer calculated every index or show request

app/controllers/choices_controller.rb
@@ -9,7 +9,9 @@ class ChoicesController < InheritedResources::Base @@ -9,7 +9,9 @@ class ChoicesController < InheritedResources::Base
9 if params[:limit] 9 if params[:limit]
10 @question = Question.find(params[:question_id])#, :include => :choices) 10 @question = Question.find(params[:question_id])#, :include => :choices)
11 @question.reload 11 @question.reload
12 - @question.choices.each(&:compute_score!) 12 +
  13 + #should compute score after each win/loss
  14 + #@question.choices.each(&:compute_score!)
13 unless params[:include_inactive] 15 unless params[:include_inactive]
14 @choices = Choice.find(:all, :conditions => {:question_id => @question.id, :active => true}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item) 16 @choices = Choice.find(:all, :conditions => {:question_id => @question.id, :active => true}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item)
15 else 17 else
@@ -17,7 +19,7 @@ class ChoicesController < InheritedResources::Base @@ -17,7 +19,7 @@ class ChoicesController < InheritedResources::Base
17 end 19 end
18 else 20 else
19 @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices 21 @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices
20 - @question.choices.each(&:compute_score!) 22 + #@question.choices.each(&:compute_score!)
21 unless params[:include_inactive] 23 unless params[:include_inactive]
22 @choices = @question.choices(true).active.find(:all, :include => :item) 24 @choices = @question.choices(true).active.find(:all, :include => :item)
23 else 25 else
@@ -34,8 +36,8 @@ class ChoicesController < InheritedResources::Base @@ -34,8 +36,8 @@ class ChoicesController < InheritedResources::Base
34 show! do |format| 36 show! do |format|
35 format.xml { 37 format.xml {
36 @choice.reload 38 @choice.reload
37 - @choice.compute_score!  
38 - @choice.reload 39 + # @choice.compute_score!
  40 + # @choice.reload
39 render :xml => @choice.to_xml(:methods => [:item_data, :wins_plus_losses, :question_name])} 41 render :xml => @choice.to_xml(:methods => [:item_data, :wins_plus_losses, :question_name])}
40 format.json { render :json => @choice.to_json(:methods => [:data])} 42 format.json { render :json => @choice.to_json(:methods => [:data])}
41 end 43 end
app/models/choice.rb
@@ -26,6 +26,7 @@ class Choice < ActiveRecord::Base @@ -26,6 +26,7 @@ class Choice < ActiveRecord::Base
26 26
27 def lose! 27 def lose!
28 self.loss_count += 1 rescue (self.loss_count = 1) 28 self.loss_count += 1 rescue (self.loss_count = 1)
  29 + self.score = compute_score
29 save! 30 save!
30 end 31 end
31 32
@@ -51,7 +52,7 @@ class Choice < ActiveRecord::Base @@ -51,7 +52,7 @@ class Choice < ActiveRecord::Base
51 self.item = @item 52 self.item = @item
52 end 53 end
53 unless self.score 54 unless self.score
54 - self.score = 0.0 55 + self.score = 50.0
55 end 56 end
56 unless self.active? 57 unless self.active?
57 puts "this choice was not specifically set to active, so we are now asking if we should auto-activate" 58 puts "this choice was not specifically set to active, so we are now asking if we should auto-activate"
app/models/visitor.rb
@@ -16,31 +16,16 @@ class Visitor < ActiveRecord::Base @@ -16,31 +16,16 @@ class Visitor < ActiveRecord::Base
16 end 16 end
17 17
18 def vote_for!(prompt, ordinality) 18 def vote_for!(prompt, ordinality)
19 - # Why are there three vote objects created for every actual 'vote'? Why not have each vote have a questionid, promptid and choiceid?  
20 -# question_vote = votes.create!(:voteable_id => prompt.question_id, :voteable_type => "Question")  
21 -# logger.info "Visitor: #{self.inspect} voted for Question: #{prompt.question_id}"  
22 -  
23 -  
24 -# prompt_vote = votes.create!(:voteable => prompt)  
25 -# logger.info "Visitor: voted for Prompt: #{prompt.id.to_s}"  
26 - # @click = Click.new(:what_was_clicked => "on the API level, inside visitor#vote_for! with prompt id #{prompt.id}, ordinality #{ordinality.to_s}, choice: #{choice.item.data} (id: #{choice.id})")  
27 - # @click.save!  
28 -  
29 choices = prompt.choices 19 choices = prompt.choices
30 choice = choices[ordinality] #we need to guarantee that the choices are in the right order (by position) 20 choice = choices[ordinality] #we need to guarantee that the choices are in the right order (by position)
31 other_choices = choices - [choice] 21 other_choices = choices - [choice]
32 - other_choices.each {|c| c.lose! } 22 + other_choices.each do |c|
  23 + c.lose!
  24 + end
33 25
34 loser_choice = other_choices.first 26 loser_choice = other_choices.first
35 votes.create!(:question_id => prompt.question_id, :prompt_id => prompt.id, :voter_id=> self.id, :choice_id => choice.id, :loser_choice_id => loser_choice.id) 27 votes.create!(:question_id => prompt.question_id, :prompt_id => prompt.id, :voter_id=> self.id, :choice_id => choice.id, :loser_choice_id => loser_choice.id)
36 -  
37 -# choice_vote = votes.create!(:voteable => choice)  
38 - # logger.info "Visitor: voted for Prompt: #{prompt.id.to_s} for choice #{choice.item.data}"  
39 - # choice.save!  
40 - # choice.score = choice.compute_score  
41 - # logger.info "Just computed the score for that choice and it's apparently #{choice.score}"  
42 - # choice.save!  
43 - #logger.info "Saved. That choice's score is still #{choice.score}" 28 + choice.compute_score! #update score after win
44 end 29 end
45 30
46 def skip!(prompt) 31 def skip!(prompt)
db/migrate/20100323180141_set_blank_score_values_to_default_fifty.rb 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +class SetBlankScoreValuesToDefaultFifty < ActiveRecord::Migration
  2 + def self.up
  3 + ActiveRecord::Base.connection.execute('UPDATE choices SET score=50.0 where score =0')
  4 + end
  5 +
  6 + def self.down
  7 + end
  8 +end