Files
nairobi/lib/score_calculator.rb
Javi Martín 28aafbd4bc Add and apply Style/InvertibleUnlessCondition rule
This rule was added in rubocop 1.44.0. It's useful to avoid accidental
`unless !condition` clauses.

Note we aren't replacing `unless zero?` with `if nonzero?` because we
never use `nonzero?`; using it sounds like `if !zero?`.

Replacing `unless any?` with `if none?` is only consistent if we also replace
`unless present?` with `if blank?`, so we're also adding this case. For
consistency, we're also replacing `unless blank?` with `if present?`.

We're also simplifying code dealing with `> 0` conditions in order to
make the code (hopefully) easier to understand.

Also for consistency, we're enabling the `Style/InverseMethods` rule,
which follows a similar idea.
2023-09-07 19:14:03 +02:00

34 lines
917 B
Ruby

module ScoreCalculator
def self.hot_score(resource)
return 0 unless resource.created_at
period = [1, [max_period, resource_age(resource)].min].max
votes_total = resource.votes_for.where("created_at >= ?", period.days.ago).count
votes_up = resource.get_upvotes.where("created_at >= ?", period.days.ago).count
votes_down = votes_total - votes_up
votes_score = votes_up - votes_down
(votes_score.to_f / period).round
end
def self.confidence_score(votes_total, votes_up)
return 1 if votes_total.zero?
votes_total = votes_total.to_f
votes_up = votes_up.to_f
votes_down = votes_total - votes_up
score = votes_up - votes_down
score * (votes_up / votes_total) * 100
end
def self.max_period
Setting["hot_score_period_in_days"].to_i
end
def self.resource_age(resource)
((Time.current - resource.created_at) / 1.day).ceil
end
end