Commit 986291aeb2357b01a9e79d6ff02bfde27f898607
1 parent
6aa016fd
Exists in
master
and in
1 other branch
Refactored vote model to get better statistics
Showing
7 changed files
with
47 additions
and
13 deletions
Show diff stats
app/models/choice.rb
@@ -9,7 +9,7 @@ class Choice < ActiveRecord::Base | @@ -9,7 +9,7 @@ class Choice < ActiveRecord::Base | ||
9 | validates_presence_of :question, :on => :create, :message => "can't be blank" | 9 | validates_presence_of :question, :on => :create, :message => "can't be blank" |
10 | #validates_length_of :item, :maximum => 140 | 10 | #validates_length_of :item, :maximum => 140 |
11 | 11 | ||
12 | - has_many :votes, :as => :voteable | 12 | + has_many :votes |
13 | has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id" | 13 | has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id" |
14 | has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id" | 14 | has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id" |
15 | named_scope :active, :conditions => { :active => true } | 15 | named_scope :active, :conditions => { :active => true } |
app/models/prompt.rb
@@ -2,7 +2,7 @@ class Prompt < ActiveRecord::Base | @@ -2,7 +2,7 @@ class Prompt < ActiveRecord::Base | ||
2 | #has_many :choices, :order => 'position DESC' | 2 | #has_many :choices, :order => 'position DESC' |
3 | 3 | ||
4 | has_many :skips | 4 | has_many :skips |
5 | - has_many :votes, :as => :voteable | 5 | + has_many :votes |
6 | 6 | ||
7 | 7 | ||
8 | belongs_to :question, :counter_cache => true | 8 | belongs_to :question, :counter_cache => true |
app/models/question.rb
@@ -16,7 +16,7 @@ class Question < ActiveRecord::Base | @@ -16,7 +16,7 @@ class Question < ActiveRecord::Base | ||
16 | end | 16 | end |
17 | end | 17 | end |
18 | end | 18 | end |
19 | - has_many :votes, :as => :voteable | 19 | + has_many :votes |
20 | 20 | ||
21 | after_save :ensure_at_least_two_choices | 21 | after_save :ensure_at_least_two_choices |
22 | attr_accessor :ideas | 22 | attr_accessor :ideas |
app/models/visitor.rb
@@ -15,26 +15,30 @@ class Visitor < ActiveRecord::Base | @@ -15,26 +15,30 @@ class Visitor < ActiveRecord::Base | ||
15 | 15 | ||
16 | def vote_for!(prompt, ordinality) | 16 | def vote_for!(prompt, ordinality) |
17 | # Why are there three vote objects created for every actual 'vote'? Why not have each vote have a questionid, promptid and choiceid? | 17 | # Why are there three vote objects created for every actual 'vote'? Why not have each vote have a questionid, promptid and choiceid? |
18 | - question_vote = votes.create!(:voteable_id => prompt.question_id, :voteable_type => "Question") | ||
19 | - logger.info "Visitor: #{self.inspect} voted for Question: #{prompt.question_id}" | 18 | +# question_vote = votes.create!(:voteable_id => prompt.question_id, :voteable_type => "Question") |
19 | +# logger.info "Visitor: #{self.inspect} voted for Question: #{prompt.question_id}" | ||
20 | 20 | ||
21 | 21 | ||
22 | - choices = prompt.choices | ||
23 | - choice = choices[ordinality] #we need to guarantee that the choices are in the right order (by position) | ||
24 | - prompt_vote = votes.create!(:voteable => prompt) | ||
25 | - logger.info "Visitor: voted for Prompt: #{prompt.id.to_s}" | 22 | +# prompt_vote = votes.create!(:voteable => prompt) |
23 | +# 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})") | 24 | # @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! | 25 | # @click.save! |
28 | 26 | ||
29 | - choice_vote = votes.create!(:voteable => choice) | 27 | + choices = prompt.choices |
28 | + choice = choices[ordinality] #we need to guarantee that the choices are in the right order (by position) | ||
29 | + other_choices = choices - [choice] | ||
30 | + other_choices.each {|c| c.lose! } | ||
31 | + | ||
32 | + loser_choice = other_choices.first | ||
33 | + votes.create!(:question_id => prompt.question_id, :prompt_id => prompt_id, :voter_id=> self.id, :choice_id => choice.id, :loser_id => loser_choice.id) | ||
34 | + | ||
35 | +# choice_vote = votes.create!(:voteable => choice) | ||
30 | # logger.info "Visitor: voted for Prompt: #{prompt.id.to_s} for choice #{choice.item.data}" | 36 | # logger.info "Visitor: voted for Prompt: #{prompt.id.to_s} for choice #{choice.item.data}" |
31 | # choice.save! | 37 | # choice.save! |
32 | # choice.score = choice.compute_score | 38 | # choice.score = choice.compute_score |
33 | # logger.info "Just computed the score for that choice and it's apparently #{choice.score}" | 39 | # logger.info "Just computed the score for that choice and it's apparently #{choice.score}" |
34 | # choice.save! | 40 | # choice.save! |
35 | #logger.info "Saved. That choice's score is still #{choice.score}" | 41 | #logger.info "Saved. That choice's score is still #{choice.score}" |
36 | - other_choices = choices - [choice] | ||
37 | - other_choices.each {|c| c.lose! } | ||
38 | end | 42 | end |
39 | 43 | ||
40 | def skip!(prompt) | 44 | def skip!(prompt) |
app/models/vote.rb
1 | class Vote < ActiveRecord::Base | 1 | class Vote < ActiveRecord::Base |
2 | - belongs_to :voteable, :polymorphic => true, :counter_cache => true | 2 | +# belongs_to :voteable, :polymorphic => true, :counter_cache => true |
3 | belongs_to :voter, :class_name => "Visitor", :foreign_key => "voter_id" | 3 | belongs_to :voter, :class_name => "Visitor", :foreign_key => "voter_id" |
4 | + belongs_to :question, :counter_cache => true | ||
5 | + belongs_to :prompt, :counter_cache => true | ||
6 | + belongs_to :choice, :counter_cache => true | ||
4 | end | 7 | end |
db/migrate/20100215170926_create_refactored_vote_model.rb
0 → 100644
@@ -0,0 +1,18 @@ | @@ -0,0 +1,18 @@ | ||
1 | +class CreateRefactoredVoteModel < ActiveRecord::Migration | ||
2 | + def self.up | ||
3 | + create_table :votes do |table| | ||
4 | + table.text :tracking | ||
5 | + table.integer :site_id | ||
6 | + table.integer :voter_id | ||
7 | + table.integer :question_id | ||
8 | + table.integer :prompt_id | ||
9 | + table.integer :choice_id | ||
10 | + table.integer :loser_choice_id | ||
11 | + table.timestamps | ||
12 | + end | ||
13 | + end | ||
14 | + | ||
15 | + def self.down | ||
16 | + drop_table :votes | ||
17 | + end | ||
18 | +end |