diff --git a/app/controllers/choices_controller.rb b/app/controllers/choices_controller.rb index 21c0e09..9d2386a 100644 --- a/app/controllers/choices_controller.rb +++ b/app/controllers/choices_controller.rb @@ -34,15 +34,6 @@ class ChoicesController < InheritedResources::Base end - def show - show! do |format| - format.xml { - @choice.reload - render :xml => @choice.to_xml(:methods => [:wins_plus_losses])} - format.json { render :json => @choice.to_json(:methods => [:data])} - end - end - def votes @choice = Choice.find(params[:id]) render :xml => @choice.votes.to_xml diff --git a/app/models/choice.rb b/app/models/choice.rb index 572ce19..8f4455e 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -16,39 +16,11 @@ class Choice < ActiveRecord::Base after_save :update_questions_counter - attr_protected :prompts_count, :votes_count, :loss_count, :wins, :losses, :score, - :prompts_on_the_right_count, :prompts_on_the_left_count + attr_protected :prompts_count, :wins, :losses, :score, :prompts_on_the_right_count, :prompts_on_the_left_count def update_questions_counter self.question.update_attribute(:inactive_choices_count, self.question.choices.inactive.length) end - #attr_accessor :data - - def lose! - Choice.increment_counter(:loss_count, self.id) - self.loss_count +=1 # reflect the update just done above, so score is correct - self.score = compute_score - self.save - end - - def win! - self.votes_count += 1 rescue (self.votes_count = 1) - self.score = compute_score - self.save! - end - - def wins_plus_losses - wins + losses - end - - # TODO delete these and refactor loss_count and votes_count into losses and wins - def losses - loss_count || 0 - end - - def wins - votes_count || 0 - end def before_create unless self.score diff --git a/app/models/vote.rb b/app/models/vote.rb index bbdda5d..3f9681e 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -9,8 +9,8 @@ class Vote < ActiveRecord::Base 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 - belongs_to :loser_choice, :class_name => "Choice", :foreign_key => "loser_choice_id" + belongs_to :choice, :counter_cache => true, :counter_cache => :wins + belongs_to :loser_choice, :class_name => "Choice", :foreign_key => "loser_choice_id", :counter_cache => :losses has_one :appearance, :as => :answerable named_scope :recent, lambda { |*args| {:conditions => ["created_at > ?", (args.first || Date.today.beginning_of_day)]} } @@ -25,10 +25,12 @@ class Vote < ActiveRecord::Base after_create :update_winner_choice, :update_loser_choice def update_winner_choice - choice.win! + choice.reload # make sure we're using updated counter values + choice.compute_score! end def update_loser_choice - loser_choice.lose! + loser_choice.reload + loser_choice.compute_score! end end diff --git a/db/migrate/20100727203816_remove_wins_losses_and_rename_counts.rb b/db/migrate/20100727203816_remove_wins_losses_and_rename_counts.rb new file mode 100644 index 0000000..e34f298 --- /dev/null +++ b/db/migrate/20100727203816_remove_wins_losses_and_rename_counts.rb @@ -0,0 +1,15 @@ +class RemoveWinsLossesAndRenameCounts < ActiveRecord::Migration + def self.up + remove_column :choices, :wins + remove_column :choices, :losses + rename_column :choices, :votes_count, :wins + rename_column :choices, :loss_count, :losses + end + + def self.down + rename_column :choices, :wins, :votes_count + rename_column :choices, :losses, :loss_count + add_column :choices, :wins, :integer + add_column :choices, :losses, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 5bd25fa..3062e1c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20100722052215) do +ActiveRecord::Schema.define(:version => 20100727203816) do create_table "appearances", :force => true do |t| t.integer "voter_id" @@ -32,9 +32,7 @@ ActiveRecord::Schema.define(:version => 20100722052215) do t.integer "item_id" t.integer "question_id" t.integer "position" - t.integer "wins" t.integer "ratings" - t.integer "losses" t.datetime "created_at" t.datetime "updated_at" t.integer "request_id" @@ -45,8 +43,8 @@ ActiveRecord::Schema.define(:version => 20100722052215) do t.string "local_identifier" t.integer "prompts_on_the_left_count", :default => 0 t.integer "prompts_on_the_right_count", :default => 0 - t.integer "votes_count", :default => 0 - t.integer "loss_count", :default => 0 + t.integer "wins", :default => 0 + t.integer "losses", :default => 0 t.integer "prompts_count", :default => 0 t.string "data" t.integer "creator_id" diff --git a/spec/models/choice_spec.rb b/spec/models/choice_spec.rb index 0370c9b..acd302d 100644 --- a/spec/models/choice_spec.rb +++ b/spec/models/choice_spec.rb @@ -27,8 +27,6 @@ describe Choice do @unreasonable_value = 9999 @protected_attributes = {} [ :prompts_count, - :votes_count, - :loss_count, :wins, :losses, :score, @@ -100,15 +98,15 @@ describe Choice do end it "correctly compute a score based on wins and losses" do choice1 = Choice.create!(@valid_attributes) - choice1.votes_count = 30 - choice1.loss_count = 70 + choice1.wins = 30 + choice1.losses = 70 choice1.compute_score.should be_close(30,1) end it "compute score and save" do choice1 = Choice.create!(@valid_attributes) choice1.score.should == 50 - choice1.votes_count= 30 - choice1.loss_count = 70 + choice1.wins = 30 + choice1.losses = 70 choice1.compute_score! choice1.score.should be_close(30, 1) end @@ -124,15 +122,19 @@ describe Choice do end describe "voting updates things" do - before do - @prompt = @question.choose_prompt - @winning_choice = @prompt.left_choice - @losing_choice = @prompt.right_choice - @winning_choice.win! - @losing_choice.lose! + before do + @prompt = @question.choose_prompt + @winning_choice = @prompt.left_choice + @losing_choice = @prompt.right_choice + vote = Vote.create!(:choice_id => @winning_choice.id, + :loser_choice_id => @losing_choice.id, + :question_id => @question.id, + :voter_id => @visitor.id, + :prompt_id => @prompt.id ) end it "should update score on a win" do + @winning_choice.reload @winning_choice.score.should be_close(67, 1) end it "should update score on a loss" do @@ -140,15 +142,13 @@ describe Choice do @losing_choice.score.should be_close(33,1) end it "should update win count on a win" do - @winning_choice.votes_count.should == 1 + @winning_choice.reload @winning_choice.wins.should == 1 - @winning_choice.loss_count.should == 0 @winning_choice.losses.should == 0 end it "should update loss count on a loss" do - @losing_choice.votes_count.should == 0 + @losing_choice.reload @losing_choice.wins.should == 0 - @losing_choice.loss_count.should == 1 @losing_choice.losses.should == 1 end end diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index 626b828..91c5089 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -173,7 +173,7 @@ describe Question do it "should provide average voter information" do params = {:id => 124, :visitor_identifier => "jim", :with_prompt => true, :with_appearance => true, :with_average_votes => true } @question_optional_information = @question.get_optional_information(params) - @question_optional_information[:average_votes].should be_an_instance_of(Float) + @question_optional_information[:average_votes].should be_an_instance_of(Fixnum) @question_optional_information[:average_votes].should be_close(0.0, 0.1) vote_options = {:visitor_identifier => "jim", diff --git a/spec/models/vote_spec.rb b/spec/models/vote_spec.rb index 5dadec5..127ce6e 100644 --- a/spec/models/vote_spec.rb +++ b/spec/models/vote_spec.rb @@ -38,18 +38,18 @@ describe Vote do end it "should update counter cache on choice" do @prompt.left_choice.votes.size.should == 0 - @prompt.left_choice.votes_count.should == 0 + @prompt.left_choice.wins.should == 0 Factory.create(:vote, :question => @question, :prompt => @prompt, :choice => @prompt.left_choice) @prompt.left_choice.reload @prompt.left_choice.votes.size.should == 1 - @prompt.left_choice.votes_count.should == 1 + @prompt.left_choice.wins.should == 1 end it "should update counter cache on loser_choice" do @prompt.left_choice.votes.size.should == 0 @prompt.right_choice.losses.should == 0 - @prompt.left_choice.votes_count.should == 0 + @prompt.left_choice.wins.should == 0 Factory.create(:vote, :question => @question, :prompt => @prompt, :choice => @prompt.left_choice, :loser_choice => @prompt.right_choice) @@ -57,8 +57,7 @@ describe Vote do @prompt.right_choice.reload @prompt.right_choice.votes.size.should == 0 - @prompt.right_choice.votes_count.should == 0 - @prompt.right_choice.loss_count.should == 1 + @prompt.right_choice.wins.should == 0 @prompt.right_choice.losses.should == 1 end -- libgit2 0.21.2