Update scoreable to return a large positive number when score==0 (#290)

Avoid infinite activity score orders

Items with a zero activity score were getting an activity sorting score
of -infinity, because we base the value on log2(score). Adjusting the
calculation avoids this edge case, by always basing the sorting score on
a non-zero number.

---------

Co-authored-by: Kevin McConnell <kevin@37signals.com>
This commit is contained in:
Mike Dalessio
2025-03-11 07:39:17 -04:00
committed by GitHub
parent 7420adb2c4
commit e0fa376d4f
2 changed files with 10 additions and 1 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ module Bubble::Scorable
# We store this as log2 to tame the huge/tiny numbers we'd otherwise get
# when activity is far from the reference date.
days_diff = (activity_at - REFERENCE_DATE) / 1.day
Math.log2(score) + days_diff
Math.log2(1.0 + score) + days_diff
end
def last_scorable_activity_at
+9
View File
@@ -53,4 +53,13 @@ class Bubble::ScorableTest < ActiveSupport::TestCase
assert_equal [ bubble_new, bubble_mid, bubble_old ], Bubble.where(id: [ bubble_old, bubble_mid, bubble_new ]).ordered_by_activity
end
end
test "bubbles with no activity have a valid activity_score_order" do
bubble = Bubble.create! bucket: buckets(:writebook), creator: users(:kevin)
bubble.rescore
assert bubble.activity_score.zero?
assert_not bubble.activity_score_order.infinite?
end
end