Commit d1736d29144dee413f9c64c92f1bd6ce562b8d31
1 parent
5e0f9114
Exists in
master
and in
1 other branch
replaced choice#votes_count/loss_count with wins/losses
Showing
8 changed files
with
46 additions
and
69 deletions
Show diff stats
app/controllers/choices_controller.rb
@@ -34,15 +34,6 @@ class ChoicesController < InheritedResources::Base | @@ -34,15 +34,6 @@ class ChoicesController < InheritedResources::Base | ||
34 | 34 | ||
35 | end | 35 | end |
36 | 36 | ||
37 | - def show | ||
38 | - show! do |format| | ||
39 | - format.xml { | ||
40 | - @choice.reload | ||
41 | - render :xml => @choice.to_xml(:methods => [:wins_plus_losses])} | ||
42 | - format.json { render :json => @choice.to_json(:methods => [:data])} | ||
43 | - end | ||
44 | - end | ||
45 | - | ||
46 | def votes | 37 | def votes |
47 | @choice = Choice.find(params[:id]) | 38 | @choice = Choice.find(params[:id]) |
48 | render :xml => @choice.votes.to_xml | 39 | render :xml => @choice.votes.to_xml |
app/models/choice.rb
@@ -16,39 +16,11 @@ class Choice < ActiveRecord::Base | @@ -16,39 +16,11 @@ class Choice < ActiveRecord::Base | ||
16 | 16 | ||
17 | after_save :update_questions_counter | 17 | after_save :update_questions_counter |
18 | 18 | ||
19 | - attr_protected :prompts_count, :votes_count, :loss_count, :wins, :losses, :score, | ||
20 | - :prompts_on_the_right_count, :prompts_on_the_left_count | 19 | + attr_protected :prompts_count, :wins, :losses, :score, :prompts_on_the_right_count, :prompts_on_the_left_count |
21 | 20 | ||
22 | def update_questions_counter | 21 | def update_questions_counter |
23 | self.question.update_attribute(:inactive_choices_count, self.question.choices.inactive.length) | 22 | self.question.update_attribute(:inactive_choices_count, self.question.choices.inactive.length) |
24 | end | 23 | end |
25 | - #attr_accessor :data | ||
26 | - | ||
27 | - def lose! | ||
28 | - Choice.increment_counter(:loss_count, self.id) | ||
29 | - self.loss_count +=1 # reflect the update just done above, so score is correct | ||
30 | - self.score = compute_score | ||
31 | - self.save | ||
32 | - end | ||
33 | - | ||
34 | - def win! | ||
35 | - self.votes_count += 1 rescue (self.votes_count = 1) | ||
36 | - self.score = compute_score | ||
37 | - self.save! | ||
38 | - end | ||
39 | - | ||
40 | - def wins_plus_losses | ||
41 | - wins + losses | ||
42 | - end | ||
43 | - | ||
44 | - # TODO delete these and refactor loss_count and votes_count into losses and wins | ||
45 | - def losses | ||
46 | - loss_count || 0 | ||
47 | - end | ||
48 | - | ||
49 | - def wins | ||
50 | - votes_count || 0 | ||
51 | - end | ||
52 | 24 | ||
53 | def before_create | 25 | def before_create |
54 | unless self.score | 26 | unless self.score |
app/models/vote.rb
@@ -9,8 +9,8 @@ class Vote < ActiveRecord::Base | @@ -9,8 +9,8 @@ class Vote < ActiveRecord::Base | ||
9 | belongs_to :voter, :class_name => "Visitor", :foreign_key => "voter_id" | 9 | belongs_to :voter, :class_name => "Visitor", :foreign_key => "voter_id" |
10 | belongs_to :question, :counter_cache => true | 10 | belongs_to :question, :counter_cache => true |
11 | belongs_to :prompt, :counter_cache => true | 11 | belongs_to :prompt, :counter_cache => true |
12 | - belongs_to :choice, :counter_cache => true | ||
13 | - belongs_to :loser_choice, :class_name => "Choice", :foreign_key => "loser_choice_id" | 12 | + belongs_to :choice, :counter_cache => true, :counter_cache => :wins |
13 | + belongs_to :loser_choice, :class_name => "Choice", :foreign_key => "loser_choice_id", :counter_cache => :losses | ||
14 | has_one :appearance, :as => :answerable | 14 | has_one :appearance, :as => :answerable |
15 | 15 | ||
16 | named_scope :recent, lambda { |*args| {:conditions => ["created_at > ?", (args.first || Date.today.beginning_of_day)]} } | 16 | named_scope :recent, lambda { |*args| {:conditions => ["created_at > ?", (args.first || Date.today.beginning_of_day)]} } |
@@ -25,10 +25,12 @@ class Vote < ActiveRecord::Base | @@ -25,10 +25,12 @@ class Vote < ActiveRecord::Base | ||
25 | after_create :update_winner_choice, :update_loser_choice | 25 | after_create :update_winner_choice, :update_loser_choice |
26 | 26 | ||
27 | def update_winner_choice | 27 | def update_winner_choice |
28 | - choice.win! | 28 | + choice.reload # make sure we're using updated counter values |
29 | + choice.compute_score! | ||
29 | end | 30 | end |
30 | 31 | ||
31 | def update_loser_choice | 32 | def update_loser_choice |
32 | - loser_choice.lose! | 33 | + loser_choice.reload |
34 | + loser_choice.compute_score! | ||
33 | end | 35 | end |
34 | end | 36 | end |
db/migrate/20100727203816_remove_wins_losses_and_rename_counts.rb
0 → 100644
@@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
1 | +class RemoveWinsLossesAndRenameCounts < ActiveRecord::Migration | ||
2 | + def self.up | ||
3 | + remove_column :choices, :wins | ||
4 | + remove_column :choices, :losses | ||
5 | + rename_column :choices, :votes_count, :wins | ||
6 | + rename_column :choices, :loss_count, :losses | ||
7 | + end | ||
8 | + | ||
9 | + def self.down | ||
10 | + rename_column :choices, :wins, :votes_count | ||
11 | + rename_column :choices, :losses, :loss_count | ||
12 | + add_column :choices, :wins, :integer | ||
13 | + add_column :choices, :losses, :integer | ||
14 | + end | ||
15 | +end |
db/schema.rb
@@ -9,7 +9,7 @@ | @@ -9,7 +9,7 @@ | ||
9 | # | 9 | # |
10 | # It's strongly recommended to check this file into your version control system. | 10 | # It's strongly recommended to check this file into your version control system. |
11 | 11 | ||
12 | -ActiveRecord::Schema.define(:version => 20100722052215) do | 12 | +ActiveRecord::Schema.define(:version => 20100727203816) do |
13 | 13 | ||
14 | create_table "appearances", :force => true do |t| | 14 | create_table "appearances", :force => true do |t| |
15 | t.integer "voter_id" | 15 | t.integer "voter_id" |
@@ -32,9 +32,7 @@ ActiveRecord::Schema.define(:version => 20100722052215) do | @@ -32,9 +32,7 @@ ActiveRecord::Schema.define(:version => 20100722052215) do | ||
32 | t.integer "item_id" | 32 | t.integer "item_id" |
33 | t.integer "question_id" | 33 | t.integer "question_id" |
34 | t.integer "position" | 34 | t.integer "position" |
35 | - t.integer "wins" | ||
36 | t.integer "ratings" | 35 | t.integer "ratings" |
37 | - t.integer "losses" | ||
38 | t.datetime "created_at" | 36 | t.datetime "created_at" |
39 | t.datetime "updated_at" | 37 | t.datetime "updated_at" |
40 | t.integer "request_id" | 38 | t.integer "request_id" |
@@ -45,8 +43,8 @@ ActiveRecord::Schema.define(:version => 20100722052215) do | @@ -45,8 +43,8 @@ ActiveRecord::Schema.define(:version => 20100722052215) do | ||
45 | t.string "local_identifier" | 43 | t.string "local_identifier" |
46 | t.integer "prompts_on_the_left_count", :default => 0 | 44 | t.integer "prompts_on_the_left_count", :default => 0 |
47 | t.integer "prompts_on_the_right_count", :default => 0 | 45 | t.integer "prompts_on_the_right_count", :default => 0 |
48 | - t.integer "votes_count", :default => 0 | ||
49 | - t.integer "loss_count", :default => 0 | 46 | + t.integer "wins", :default => 0 |
47 | + t.integer "losses", :default => 0 | ||
50 | t.integer "prompts_count", :default => 0 | 48 | t.integer "prompts_count", :default => 0 |
51 | t.string "data" | 49 | t.string "data" |
52 | t.integer "creator_id" | 50 | t.integer "creator_id" |
spec/models/choice_spec.rb
@@ -27,8 +27,6 @@ describe Choice do | @@ -27,8 +27,6 @@ describe Choice do | ||
27 | @unreasonable_value = 9999 | 27 | @unreasonable_value = 9999 |
28 | @protected_attributes = {} | 28 | @protected_attributes = {} |
29 | [ :prompts_count, | 29 | [ :prompts_count, |
30 | - :votes_count, | ||
31 | - :loss_count, | ||
32 | :wins, | 30 | :wins, |
33 | :losses, | 31 | :losses, |
34 | :score, | 32 | :score, |
@@ -100,15 +98,15 @@ describe Choice do | @@ -100,15 +98,15 @@ describe Choice do | ||
100 | end | 98 | end |
101 | it "correctly compute a score based on wins and losses" do | 99 | it "correctly compute a score based on wins and losses" do |
102 | choice1 = Choice.create!(@valid_attributes) | 100 | choice1 = Choice.create!(@valid_attributes) |
103 | - choice1.votes_count = 30 | ||
104 | - choice1.loss_count = 70 | 101 | + choice1.wins = 30 |
102 | + choice1.losses = 70 | ||
105 | choice1.compute_score.should be_close(30,1) | 103 | choice1.compute_score.should be_close(30,1) |
106 | end | 104 | end |
107 | it "compute score and save" do | 105 | it "compute score and save" do |
108 | choice1 = Choice.create!(@valid_attributes) | 106 | choice1 = Choice.create!(@valid_attributes) |
109 | choice1.score.should == 50 | 107 | choice1.score.should == 50 |
110 | - choice1.votes_count= 30 | ||
111 | - choice1.loss_count = 70 | 108 | + choice1.wins = 30 |
109 | + choice1.losses = 70 | ||
112 | choice1.compute_score! | 110 | choice1.compute_score! |
113 | choice1.score.should be_close(30, 1) | 111 | choice1.score.should be_close(30, 1) |
114 | end | 112 | end |
@@ -124,15 +122,19 @@ describe Choice do | @@ -124,15 +122,19 @@ describe Choice do | ||
124 | end | 122 | end |
125 | 123 | ||
126 | describe "voting updates things" do | 124 | describe "voting updates things" do |
127 | - before do | ||
128 | - @prompt = @question.choose_prompt | ||
129 | - @winning_choice = @prompt.left_choice | ||
130 | - @losing_choice = @prompt.right_choice | ||
131 | - @winning_choice.win! | ||
132 | - @losing_choice.lose! | 125 | + before do |
126 | + @prompt = @question.choose_prompt | ||
127 | + @winning_choice = @prompt.left_choice | ||
128 | + @losing_choice = @prompt.right_choice | ||
129 | + vote = Vote.create!(:choice_id => @winning_choice.id, | ||
130 | + :loser_choice_id => @losing_choice.id, | ||
131 | + :question_id => @question.id, | ||
132 | + :voter_id => @visitor.id, | ||
133 | + :prompt_id => @prompt.id ) | ||
133 | end | 134 | end |
134 | 135 | ||
135 | it "should update score on a win" do | 136 | it "should update score on a win" do |
137 | + @winning_choice.reload | ||
136 | @winning_choice.score.should be_close(67, 1) | 138 | @winning_choice.score.should be_close(67, 1) |
137 | end | 139 | end |
138 | it "should update score on a loss" do | 140 | it "should update score on a loss" do |
@@ -140,15 +142,13 @@ describe Choice do | @@ -140,15 +142,13 @@ describe Choice do | ||
140 | @losing_choice.score.should be_close(33,1) | 142 | @losing_choice.score.should be_close(33,1) |
141 | end | 143 | end |
142 | it "should update win count on a win" do | 144 | it "should update win count on a win" do |
143 | - @winning_choice.votes_count.should == 1 | 145 | + @winning_choice.reload |
144 | @winning_choice.wins.should == 1 | 146 | @winning_choice.wins.should == 1 |
145 | - @winning_choice.loss_count.should == 0 | ||
146 | @winning_choice.losses.should == 0 | 147 | @winning_choice.losses.should == 0 |
147 | end | 148 | end |
148 | it "should update loss count on a loss" do | 149 | it "should update loss count on a loss" do |
149 | - @losing_choice.votes_count.should == 0 | 150 | + @losing_choice.reload |
150 | @losing_choice.wins.should == 0 | 151 | @losing_choice.wins.should == 0 |
151 | - @losing_choice.loss_count.should == 1 | ||
152 | @losing_choice.losses.should == 1 | 152 | @losing_choice.losses.should == 1 |
153 | end | 153 | end |
154 | end | 154 | end |
spec/models/question_spec.rb
@@ -173,7 +173,7 @@ describe Question do | @@ -173,7 +173,7 @@ describe Question do | ||
173 | it "should provide average voter information" do | 173 | it "should provide average voter information" do |
174 | params = {:id => 124, :visitor_identifier => "jim", :with_prompt => true, :with_appearance => true, :with_average_votes => true } | 174 | params = {:id => 124, :visitor_identifier => "jim", :with_prompt => true, :with_appearance => true, :with_average_votes => true } |
175 | @question_optional_information = @question.get_optional_information(params) | 175 | @question_optional_information = @question.get_optional_information(params) |
176 | - @question_optional_information[:average_votes].should be_an_instance_of(Float) | 176 | + @question_optional_information[:average_votes].should be_an_instance_of(Fixnum) |
177 | @question_optional_information[:average_votes].should be_close(0.0, 0.1) | 177 | @question_optional_information[:average_votes].should be_close(0.0, 0.1) |
178 | 178 | ||
179 | vote_options = {:visitor_identifier => "jim", | 179 | vote_options = {:visitor_identifier => "jim", |
spec/models/vote_spec.rb
@@ -38,18 +38,18 @@ describe Vote do | @@ -38,18 +38,18 @@ describe Vote do | ||
38 | end | 38 | end |
39 | it "should update counter cache on choice" do | 39 | it "should update counter cache on choice" do |
40 | @prompt.left_choice.votes.size.should == 0 | 40 | @prompt.left_choice.votes.size.should == 0 |
41 | - @prompt.left_choice.votes_count.should == 0 | 41 | + @prompt.left_choice.wins.should == 0 |
42 | Factory.create(:vote, :question => @question, :prompt => @prompt, | 42 | Factory.create(:vote, :question => @question, :prompt => @prompt, |
43 | :choice => @prompt.left_choice) | 43 | :choice => @prompt.left_choice) |
44 | 44 | ||
45 | @prompt.left_choice.reload | 45 | @prompt.left_choice.reload |
46 | @prompt.left_choice.votes.size.should == 1 | 46 | @prompt.left_choice.votes.size.should == 1 |
47 | - @prompt.left_choice.votes_count.should == 1 | 47 | + @prompt.left_choice.wins.should == 1 |
48 | end | 48 | end |
49 | it "should update counter cache on loser_choice" do | 49 | it "should update counter cache on loser_choice" do |
50 | @prompt.left_choice.votes.size.should == 0 | 50 | @prompt.left_choice.votes.size.should == 0 |
51 | @prompt.right_choice.losses.should == 0 | 51 | @prompt.right_choice.losses.should == 0 |
52 | - @prompt.left_choice.votes_count.should == 0 | 52 | + @prompt.left_choice.wins.should == 0 |
53 | Factory.create(:vote, :question => @question, :prompt => @prompt, | 53 | Factory.create(:vote, :question => @question, :prompt => @prompt, |
54 | :choice => @prompt.left_choice, | 54 | :choice => @prompt.left_choice, |
55 | :loser_choice => @prompt.right_choice) | 55 | :loser_choice => @prompt.right_choice) |
@@ -57,8 +57,7 @@ describe Vote do | @@ -57,8 +57,7 @@ describe Vote do | ||
57 | 57 | ||
58 | @prompt.right_choice.reload | 58 | @prompt.right_choice.reload |
59 | @prompt.right_choice.votes.size.should == 0 | 59 | @prompt.right_choice.votes.size.should == 0 |
60 | - @prompt.right_choice.votes_count.should == 0 | ||
61 | - @prompt.right_choice.loss_count.should == 1 | 60 | + @prompt.right_choice.wins.should == 0 |
62 | @prompt.right_choice.losses.should == 1 | 61 | @prompt.right_choice.losses.should == 1 |
63 | end | 62 | end |
64 | 63 |