Skip to content

Commit 28d2763

Browse files
authored
Merge pull request #1909 from codidact/0valt/resilience
Make ability score percent calculation resilient against negative values
2 parents 1ff2d8b + f839259 commit 28d2763

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

app/models/ability.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def manual?
1212
# @param user [User, nil] user to get the percent for
1313
# @return [Integer] edit score percent
1414
def edit_score_percent_for(user)
15-
return 0 if edit_score_threshold.nil? || user.nil?
15+
return 0 if unreachable_threshold_for?(edit_score_threshold, user)
1616
return 100 if edit_score_threshold.zero?
1717

1818
linear_score = linearize_progress(user.community_user.edit_score)
@@ -25,7 +25,7 @@ def edit_score_percent_for(user)
2525
# @param user [User, nil] user to get the percent for
2626
# @return [Integer] flag score percent
2727
def flag_score_percent_for(user)
28-
return 0 if flag_score_threshold.nil? || user.nil?
28+
return 0 if unreachable_threshold_for?(flag_score_threshold, user)
2929
return 100 if flag_score_threshold.zero?
3030

3131
linear_score = linearize_progress(user.community_user.flag_score)
@@ -38,7 +38,7 @@ def flag_score_percent_for(user)
3838
# @param user [User, nil] user to get the percent for
3939
# @return [Integer] post score percent
4040
def post_score_percent_for(user)
41-
return 0 if post_score_threshold.nil? || user.nil?
41+
return 0 if unreachable_threshold_for?(post_score_threshold, user)
4242
return 100 if post_score_threshold.zero?
4343

4444
linear_score = linearize_progress(user.community_user.post_score)
@@ -65,4 +65,13 @@ def self.trust_levels
6565
def self.[](key)
6666
find_by internal_id: key
6767
end
68+
69+
private
70+
71+
# Is a given threshold never reachable for a given user?
72+
# @param threshold [Numeric] threshold to check
73+
# @param user [User, nil] user to check
74+
def unreachable_threshold_for?(threshold, user)
75+
threshold.nil? || threshold.negative? || user.nil?
76+
end
6877
end

0 commit comments

Comments
 (0)