Commit fe8ccbdf58f19af1bbf9c53f341387fd67229b94
1 parent
b18e4649
Exists in
master
and in
1 other branch
When we invalidate votes, we also need to update the score
Showing
2 changed files
with
26 additions
and
2 deletions
Show diff stats
app/models/vote.rb
... | ... | @@ -23,7 +23,7 @@ class Vote < ActiveRecord::Base |
23 | 23 | serialize :tracking |
24 | 24 | |
25 | 25 | after_create :update_winner_choice, :update_loser_choice |
26 | - after_save :update_counter_caches_based_on_flags | |
26 | + after_save :update_cached_values_based_on_flags | |
27 | 27 | |
28 | 28 | def update_winner_choice |
29 | 29 | choice.reload # make sure we're using updated counter values |
... | ... | @@ -37,7 +37,7 @@ class Vote < ActiveRecord::Base |
37 | 37 | |
38 | 38 | # this is necessary to handle counter cache, at least until the following patch is accepted: |
39 | 39 | # https://rails.lighthouseapp.com/projects/8994/tickets/3521-patch-add-conditional-counter-cache |
40 | - def update_counter_caches_based_on_flags | |
40 | + def update_cached_values_based_on_flags | |
41 | 41 | if valid_record_changed? |
42 | 42 | if valid_record |
43 | 43 | Question.increment_counter(:votes_count, self.question_id) |
... | ... | @@ -50,6 +50,11 @@ class Vote < ActiveRecord::Base |
50 | 50 | Choice.decrement_counter(:wins, self.choice_id) |
51 | 51 | Choice.decrement_counter(:losses, self.loser_choice_id) |
52 | 52 | end |
53 | + | |
54 | + choice.reload | |
55 | + choice.compute_score! | |
56 | + loser_choice.reload | |
57 | + loser_choice.compute_score! | |
53 | 58 | end |
54 | 59 | end |
55 | 60 | end | ... | ... |
spec/models/vote_spec.rb
... | ... | @@ -183,5 +183,24 @@ describe Vote do |
183 | 183 | @prompt.right_choice.losses.should == 0 |
184 | 184 | |
185 | 185 | end |
186 | + | |
187 | + it "should update score choice and loser_choice after invalid is changed" do | |
188 | + vote = Factory.create(:vote, :question => @question, :prompt => @prompt, | |
189 | + :choice => @prompt.left_choice, | |
190 | + :loser_choice => @prompt.right_choice) | |
191 | + | |
192 | + @prompt.left_choice.reload | |
193 | + @prompt.left_choice.score.should be_close(67, 1) | |
194 | + @prompt.right_choice.reload | |
195 | + @prompt.right_choice.score.should be_close(33, 1) | |
196 | + | |
197 | + vote.valid_record = false | |
198 | + vote.save | |
199 | + | |
200 | + @prompt.left_choice.reload | |
201 | + @prompt.left_choice.score.should == 50 | |
202 | + @prompt.right_choice.reload | |
203 | + @prompt.right_choice.score.should == 50 | |
204 | + end | |
186 | 205 | |
187 | 206 | end | ... | ... |