From 8e32f656fa27c8ba5ad36d0c3a00298636acfeb6 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 25 Nov 2021 23:35:09 -0600 Subject: [PATCH] votes: fix division by zero in upvote ratio. Fix a division by zero when calculating the upvote ratio in the votes tooltip. This could happen if the post had votes according to the post_votes table, but didn't have any votes according to the up_score and down_score. This should never happen, yet it did happen for post 2248039 and post 2959269. --- app/components/post_votes_tooltip_component.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/components/post_votes_tooltip_component.rb b/app/components/post_votes_tooltip_component.rb index 8408bcdb2..32abdad8a 100644 --- a/app/components/post_votes_tooltip_component.rb +++ b/app/components/post_votes_tooltip_component.rb @@ -19,9 +19,13 @@ class PostVotesTooltipComponent < ApplicationComponent vote.is_positive? ? upvote_icon : downvote_icon end + def vote_count + post.up_score + post.down_score.abs + end + def upvote_ratio - return nil if votes.length == 0 - sprintf("(%.1f%%)", 100.0 * post.up_score / (post.up_score + post.down_score.abs)) + return nil if vote_count == 0 + sprintf("(%.1f%%)", 100.0 * post.up_score / vote_count) end def voter_name(vote)