Commit c12546915f639f23fb848e745ff6749df6aeb2f9

Authored by Pius Uzamere
1 parent 3972a618

DRAMATIC improvements in performance through counter caches and more intelligent picking

app/controllers/clicks_controller.rb
... ... @@ -40,9 +40,13 @@ class ClicksController < ApplicationController
40 40 # POST /clicks
41 41 # POST /clicks.xml
42 42 def create
43   - authenticate
44   - p = params[:click].except(:sid).merge(:visitor_id => current_user.visitors.find_or_create_by_identifier(params[:click][:sid]).id)
45   - @click = Click.new(p)
  43 + #authenticate
  44 + if signed_in?
  45 + p = params[:click].except(:sid).merge(:visitor_id => current_user.visitors.find_or_create_by_identifier(params[:click][:sid]).id)
  46 + @click = Click.new(p)
  47 + else
  48 + render :nothing => true and return
  49 + end
46 50  
47 51 respond_to do |format|
48 52 if @click.save
... ...
app/controllers/questions_controller.rb
... ... @@ -7,7 +7,8 @@ class QuestionsController < InheritedResources::Base
7 7 show! do |format|
8 8 session['prompts_ids'] ||= []
9 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])
  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])
11 12 }
12 13 end
13 14 end
... ...
app/models/choice.rb
... ... @@ -17,18 +17,14 @@ class Choice < ActiveRecord::Base
17 17 end
18 18  
19 19 def wins_plus_losses
20   - (prompts_on_the_left.collect(&:votes_count).sum + prompts_on_the_right.collect(&:votes_count).sum)
  20 + #(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}")
21 22 end
22 23  
23 24 def wins
24 25 votes_count
25 26 end
26 27  
27   - def votes_count
28   - votes(true).size
29   - end
30   -
31   -
32 28 after_create :generate_prompts
33 29 def before_create
34 30 unless item
... ...
app/models/prompt.rb
... ... @@ -2,12 +2,12 @@ class Prompt < ActiveRecord::Base
2 2 #has_many :choices, :order => 'position DESC'
3 3  
4 4 has_many :skips
5   - has_many :votes, :as => :voteable
  5 + has_many :votes, :as => :voteable
6 6  
7 7  
8   - belongs_to :question
9   - belongs_to :left_choice, :class_name => "Choice", :foreign_key => "left_choice_id"
10   - belongs_to :right_choice, :class_name => "Choice", :foreign_key => "right_choice_id"
  8 + belongs_to :question, :counter_cache => true
  9 + belongs_to :left_choice, :class_name => "Choice", :foreign_key => "left_choice_id", :counter_cache => true
  10 + belongs_to :right_choice, :class_name => "Choice", :foreign_key => "right_choice_id", :counter_cache => true
11 11  
12 12 named_scope :with_left_choice, lambda { |*args| {:conditions => ["left_choice_id = ?", (args.first.id)]} }
13 13 named_scope :with_right_choice, lambda { |*args| {:conditions => ["right_choice_id = ?", (args.first.id)]} }
... ... @@ -26,10 +26,6 @@ class Prompt < ActiveRecord::Base
26 26 validates_presence_of :left_choice, :on => :create, :message => "can't be blank"
27 27 validates_presence_of :right_choice, :on => :create, :message => "can't be blank"
28 28  
29   - def votes_count
30   - votes(true).size
31   - end
32   -
33 29 def choices
34 30 [left_choice, right_choice]
35 31 end
... ...
app/models/question.rb
... ... @@ -18,31 +18,18 @@ class Question < ActiveRecord::Base
18 18 attr_accessor :ideas
19 19  
20 20 def item_count
21   - choice_count
  21 + choices_count
22 22 end
23 23  
24   - def prompt_count
25   - Prompt.count(:all, :conditions => {:question_id => id})
26   - end
27   -
28   - def choice_count
29   - Choice.count(:all, :conditions => {:question_id => id})
30   - end
31   -
32   - def votes_count
33   - Vote.count(:all, :conditions => {:voteable_id => id, :voteable_type => 'Question'})
34   - end
35   -
36 24 def picked_prompt
37   - prompts[rand(prompts.count-1)]#Prompt.find(picked_prompt_id)
  25 + #prompts[rand(prompts.count-1)]#Prompt.find(picked_prompt_id)
  26 + Prompt.find(rand(prompts_count-1))
38 27 end
39   -
  28 +
40 29 def picked_prompt_id
41   - lambda {@picked ||= prompts(true)[rand(prompts.count-1)].id}.call
42   -
43   - lambda { prompts(true)[rand(prompts.count-1)].id }.call
  30 + rand(prompts_count-1)
44 31 end
45   -
  32 +
46 33 def left_choice_text(prompt = nil)
47 34 prompt ||= prompts.first#prompts.pick
48 35 picked_prompt.left_choice.item.data
... ...
app/models/vote.rb
1 1 class Vote < ActiveRecord::Base
2   - belongs_to :voteable, :polymorphic => true
  2 + belongs_to :voteable, :polymorphic => true, :counter_cache => true
3 3 belongs_to :voter, :class_name => "Visitor", :foreign_key => "voter_id"
4 4 end
... ...
db/migrate/20091204012430_add_counter_caches_to_choices.rb 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +class AddCounterCachesToChoices < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :choices, :prompts_on_the_left_count, :integer, :default => 0
  4 + add_column :choices, :prompts_on_the_right_count, :integer, :default => 0
  5 + add_column :choices, :votes_count, :integer, :default => 0
  6 +
  7 + Choice.reset_column_information
  8 + Choice.find(:all).each do |c|
  9 + Choice.update_counters c.id, :prompts_on_the_left_count => c.prompts_on_the_left.length
  10 + Choice.update_counters c.id, :prompts_on_the_right_count => c.prompts_on_the_right.length
  11 + Choice.update_counters c.id, :votes_count => c.votes.length
  12 + end
  13 + end
  14 +
  15 + def self.down
  16 + remove_column :choices, :votes_count
  17 + remove_column :choices, :prompts_on_the_right_count
  18 + remove_column :choices, :prompts_on_the_left_count
  19 + end
  20 +end
... ...
db/migrate/20091204013843_add_votes_count_to_prompts.rb 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +class AddVotesCountToPrompts < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :prompts, :votes_count, :integer, :default => 0
  4 +
  5 + Prompt.reset_column_information
  6 + Prompt.find(:all).each do |p|
  7 + Prompt.update_counters p.id, :votes_count => p.votes.length
  8 + end
  9 +
  10 + end
  11 +
  12 + def self.down
  13 + remove_column :prompts, :votes_count
  14 + end
  15 +end
... ...
db/migrate/20091204015325_add_counter_caches_to_questions.rb 0 → 100644
... ... @@ -0,0 +1,16 @@
  1 +class AddCounterCachesToQuestions < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :questions, :votes_count, :integer, :default => 0
  4 +
  5 + Question.reset_column_information
  6 + Question.find(:all).each do |q|
  7 + Question.update_counters q.id, :choices_count => q.choices.length
  8 + Question.update_counters q.id, :prompts_count => q.prompts.length
  9 + Question.update_counters q.id, :votes_count => q.votes.length
  10 + end
  11 + end
  12 +
  13 + def self.down
  14 + remove_column :questions, :votes_count
  15 + end
  16 +end
... ...