diff --git a/app/models/ability.rb b/app/models/ability.rb index b43648f59..b377084c8 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -12,7 +12,7 @@ def manual? # @param user [User, nil] user to get the percent for # @return [Integer] edit score percent def edit_score_percent_for(user) - return 0 if edit_score_threshold.nil? || user.nil? + return 0 if unreachable_threshold_for?(edit_score_threshold, user) return 100 if edit_score_threshold.zero? linear_score = linearize_progress(user.community_user.edit_score) @@ -25,7 +25,7 @@ def edit_score_percent_for(user) # @param user [User, nil] user to get the percent for # @return [Integer] flag score percent def flag_score_percent_for(user) - return 0 if flag_score_threshold.nil? || user.nil? + return 0 if unreachable_threshold_for?(flag_score_threshold, user) return 100 if flag_score_threshold.zero? linear_score = linearize_progress(user.community_user.flag_score) @@ -38,7 +38,7 @@ def flag_score_percent_for(user) # @param user [User, nil] user to get the percent for # @return [Integer] post score percent def post_score_percent_for(user) - return 0 if post_score_threshold.nil? || user.nil? + return 0 if unreachable_threshold_for?(post_score_threshold, user) return 100 if post_score_threshold.zero? linear_score = linearize_progress(user.community_user.post_score) @@ -65,4 +65,13 @@ def self.trust_levels def self.[](key) find_by internal_id: key end + + private + + # Is a given threshold never reachable for a given user? + # @param threshold [Numeric] threshold to check + # @param user [User, nil] user to check + def unreachable_threshold_for?(threshold, user) + threshold.nil? || threshold.negative? || user.nil? + end end