diff --git a/app/controllers/choices_controller.rb b/app/controllers/choices_controller.rb index 9642b01..18b948f 100644 --- a/app/controllers/choices_controller.rb +++ b/app/controllers/choices_controller.rb @@ -9,7 +9,9 @@ class ChoicesController < InheritedResources::Base if params[:limit] @question = Question.find(params[:question_id])#, :include => :choices) @question.reload - @question.choices.each(&:compute_score!) + + #should compute score after each win/loss + #@question.choices.each(&:compute_score!) unless params[:include_inactive] @choices = Choice.find(:all, :conditions => {:question_id => @question.id, :active => true}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item) else @@ -17,7 +19,7 @@ class ChoicesController < InheritedResources::Base end else @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices - @question.choices.each(&:compute_score!) + #@question.choices.each(&:compute_score!) unless params[:include_inactive] @choices = @question.choices(true).active.find(:all, :include => :item) else @@ -34,8 +36,8 @@ class ChoicesController < InheritedResources::Base show! do |format| format.xml { @choice.reload - @choice.compute_score! - @choice.reload + # @choice.compute_score! + # @choice.reload render :xml => @choice.to_xml(:methods => [:item_data, :wins_plus_losses, :question_name])} format.json { render :json => @choice.to_json(:methods => [:data])} end diff --git a/app/models/choice.rb b/app/models/choice.rb index 7ef52cf..7b5edb6 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -26,6 +26,7 @@ class Choice < ActiveRecord::Base def lose! self.loss_count += 1 rescue (self.loss_count = 1) + self.score = compute_score save! end @@ -51,7 +52,7 @@ class Choice < ActiveRecord::Base self.item = @item end unless self.score - self.score = 0.0 + self.score = 50.0 end unless self.active? puts "this choice was not specifically set to active, so we are now asking if we should auto-activate" diff --git a/app/models/visitor.rb b/app/models/visitor.rb index 9407012..a9853e6 100644 --- a/app/models/visitor.rb +++ b/app/models/visitor.rb @@ -16,31 +16,16 @@ class Visitor < ActiveRecord::Base end 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}" - - -# 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! - 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! } + other_choices.each do |c| + c.lose! + end 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_choice_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}" + choice.compute_score! #update score after win end def skip!(prompt) diff --git a/db/migrate/20100323180141_set_blank_score_values_to_default_fifty.rb b/db/migrate/20100323180141_set_blank_score_values_to_default_fifty.rb new file mode 100644 index 0000000..1ba2d5b --- /dev/null +++ b/db/migrate/20100323180141_set_blank_score_values_to_default_fifty.rb @@ -0,0 +1,8 @@ +class SetBlankScoreValuesToDefaultFifty < ActiveRecord::Migration + def self.up + ActiveRecord::Base.connection.execute('UPDATE choices SET score=50.0 where score =0') + end + + def self.down + end +end -- libgit2 0.21.2