diff --git a/app/models/choice.rb b/app/models/choice.rb index 963a437..4b2305e 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -9,7 +9,7 @@ class Choice < ActiveRecord::Base validates_presence_of :question, :on => :create, :message => "can't be blank" #validates_length_of :item, :maximum => 140 - has_many :votes, :as => :voteable + has_many :votes has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id" has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id" named_scope :active, :conditions => { :active => true } diff --git a/app/models/prompt.rb b/app/models/prompt.rb index 706b5c4..122df5c 100644 --- a/app/models/prompt.rb +++ b/app/models/prompt.rb @@ -2,7 +2,7 @@ class Prompt < ActiveRecord::Base #has_many :choices, :order => 'position DESC' has_many :skips - has_many :votes, :as => :voteable + has_many :votes belongs_to :question, :counter_cache => true diff --git a/app/models/question.rb b/app/models/question.rb index 571f700..3af0005 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -16,7 +16,7 @@ class Question < ActiveRecord::Base end end end - has_many :votes, :as => :voteable + has_many :votes after_save :ensure_at_least_two_choices attr_accessor :ideas diff --git a/app/models/visitor.rb b/app/models/visitor.rb index 51502d1..e0c2438 100644 --- a/app/models/visitor.rb +++ b/app/models/visitor.rb @@ -15,26 +15,30 @@ class Visitor < ActiveRecord::Base def vote_for!(prompt, ordinality) # Why are there three vote objects created for every actual 'vote'? Why not have each vote have a questionid, promptid and choiceid? - question_vote = votes.create!(:voteable_id => prompt.question_id, :voteable_type => "Question") - logger.info "Visitor: #{self.inspect} voted for Question: #{prompt.question_id}" +# question_vote = votes.create!(:voteable_id => prompt.question_id, :voteable_type => "Question") +# logger.info "Visitor: #{self.inspect} voted for Question: #{prompt.question_id}" - choices = prompt.choices - choice = choices[ordinality] #we need to guarantee that the choices are in the right order (by position) - prompt_vote = votes.create!(:voteable => prompt) - logger.info "Visitor: voted for Prompt: #{prompt.id.to_s}" +# prompt_vote = votes.create!(:voteable => prompt) +# logger.info "Visitor: voted for Prompt: #{prompt.id.to_s}" # @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})") # @click.save! - choice_vote = votes.create!(:voteable => choice) + choices = prompt.choices + choice = choices[ordinality] #we need to guarantee that the choices are in the right order (by position) + other_choices = choices - [choice] + other_choices.each {|c| c.lose! } + + loser_choice = other_choices.first + votes.create!(:question_id => prompt.question_id, :prompt_id => prompt_id, :voter_id=> self.id, :choice_id => choice.id, :loser_id => loser_choice.id) + +# choice_vote = votes.create!(:voteable => choice) # logger.info "Visitor: voted for Prompt: #{prompt.id.to_s} for choice #{choice.item.data}" # choice.save! # choice.score = choice.compute_score # logger.info "Just computed the score for that choice and it's apparently #{choice.score}" # choice.save! #logger.info "Saved. That choice's score is still #{choice.score}" - other_choices = choices - [choice] - other_choices.each {|c| c.lose! } end def skip!(prompt) diff --git a/app/models/vote.rb b/app/models/vote.rb index b9ec8d5..c689240 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -1,4 +1,7 @@ class Vote < ActiveRecord::Base - belongs_to :voteable, :polymorphic => true, :counter_cache => true +# belongs_to :voteable, :polymorphic => true, :counter_cache => true belongs_to :voter, :class_name => "Visitor", :foreign_key => "voter_id" + belongs_to :question, :counter_cache => true + belongs_to :prompt, :counter_cache => true + belongs_to :choice, :counter_cache => true end diff --git a/db/migrate/20100215170553_rename_votes_table.rb b/db/migrate/20100215170553_rename_votes_table.rb new file mode 100644 index 0000000..046739f --- /dev/null +++ b/db/migrate/20100215170553_rename_votes_table.rb @@ -0,0 +1,9 @@ +class RenameVotesTable < ActiveRecord::Migration + def self.up + rename_table :votes, :oldvotes + end + + def self.down + rename_table :oldvotes, :votes + end +end diff --git a/db/migrate/20100215170926_create_refactored_vote_model.rb b/db/migrate/20100215170926_create_refactored_vote_model.rb new file mode 100644 index 0000000..9f726c8 --- /dev/null +++ b/db/migrate/20100215170926_create_refactored_vote_model.rb @@ -0,0 +1,18 @@ +class CreateRefactoredVoteModel < ActiveRecord::Migration + def self.up + create_table :votes do |table| + table.text :tracking + table.integer :site_id + table.integer :voter_id + table.integer :question_id + table.integer :prompt_id + table.integer :choice_id + table.integer :loser_choice_id + table.timestamps + end + end + + def self.down + drop_table :votes + end +end -- libgit2 0.21.2