Commit 3efd083fa1723726adb2aa5304fec60ec70f9126

Authored by Pius Uzamere
1 parent be2c59fc

cast question votes so that we can cache the total vote count

app/controllers/questions_controller.rb
@@ -7,8 +7,7 @@ class QuestionsController < InheritedResources::Base @@ -7,8 +7,7 @@ class QuestionsController < InheritedResources::Base
7 show! do |format| 7 show! do |format|
8 session['prompts_ids'] ||= [] 8 session['prompts_ids'] ||= []
9 format.xml { 9 format.xml {
10 - #render :xml => @question.to_xml(:methods => [:item_count, :left_choice_text, :right_choice_text, :picked_prompt_id, :votes_count, :creator_id])  
11 - render :xml => @question.to_xml(:methods => [:item_count, :left_choice_text, :right_choice_text, :picked_prompt_id, :votes_count]) 10 + render :xml => @question.to_xml(:methods => [:item_count, :left_choice_text, :right_choice_text, :picked_prompt_id])
12 } 11 }
13 end 12 end
14 end 13 end
app/models/question.rb
@@ -48,11 +48,6 @@ class Question < ActiveRecord::Base @@ -48,11 +48,6 @@ class Question < ActiveRecord::Base
48 u.questions_voted_on.include? self 48 u.questions_voted_on.include? self
49 end 49 end
50 50
51 - def votes_count  
52 - Vote.count(:all, :conditions => {:voteable_id => id, :voteable_type => 'Question'})  
53 - end  
54 -  
55 -  
56 51
57 validates_presence_of :site, :on => :create, :message => "can't be blank" 52 validates_presence_of :site, :on => :create, :message => "can't be blank"
58 validates_presence_of :creator, :on => :create, :message => "can't be blank" 53 validates_presence_of :creator, :on => :create, :message => "can't be blank"
app/models/visitor.rb
@@ -5,6 +5,7 @@ class Visitor < ActiveRecord::Base @@ -5,6 +5,7 @@ class Visitor < ActiveRecord::Base
5 has_many :skips, :class_name => "Skip", :foreign_key => "skipper_id" 5 has_many :skips, :class_name => "Skip", :foreign_key => "skipper_id"
6 has_many :items, :class_name => "Item", :foreign_key => "creator_id" 6 has_many :items, :class_name => "Item", :foreign_key => "creator_id"
7 has_many :clicks 7 has_many :clicks
  8 +
8 validates_presence_of :site, :on => :create, :message => "can't be blank" 9 validates_presence_of :site, :on => :create, :message => "can't be blank"
9 validates_uniqueness_of :identifier, :on => :create, :message => "must be unique", :scope => :site_id 10 validates_uniqueness_of :identifier, :on => :create, :message => "must be unique", :scope => :site_id
10 11
@@ -13,10 +14,15 @@ class Visitor < ActiveRecord::Base @@ -13,10 +14,15 @@ class Visitor < ActiveRecord::Base
13 end 14 end
14 15
15 def vote_for!(prompt, ordinality) 16 def vote_for!(prompt, ordinality)
  17 + question_vote = votes.create!(:voteable => prompt.question)
  18 + logger.info "Visitor: #{self.inspect} voted for Question: #{prompt.question.inspect}"
  19 +
  20 +
16 choices = prompt.choices 21 choices = prompt.choices
17 choice = choices[ordinality] #we need to guarantee that the choices are in the right order (by position) 22 choice = choices[ordinality] #we need to guarantee that the choices are in the right order (by position)
18 prompt_vote = votes.create!(:voteable => prompt) 23 prompt_vote = votes.create!(:voteable => prompt)
19 logger.info "Visitor: #{self.inspect} voted for Prompt: #{prompt.inspect}" 24 logger.info "Visitor: #{self.inspect} voted for Prompt: #{prompt.inspect}"
  25 +
20 choice_vote = votes.create!(:voteable => choice) 26 choice_vote = votes.create!(:voteable => choice)
21 logger.info "Visitor: #{self.inspect} voted for Choice: #{choice.inspect}" 27 logger.info "Visitor: #{self.inspect} voted for Choice: #{choice.inspect}"
22 choice.save! 28 choice.save!
db/migrate/20091204144003_update_vote_count_for_questions.rb 0 → 100644
@@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
  1 +class UpdateVoteCountForQuestions < ActiveRecord::Migration
  2 + def self.up
  3 + Question.reset_column_information
  4 + Question.find(:all).each do |q|
  5 + Question.update_counters q.id, :votes_count => q.choices.collect(&:votes_count).sum
  6 + end
  7 + end
  8 +
  9 + def self.down
  10 + end
  11 +end