Skip to content

Commit

Permalink
Extract methods to calculate hot score period
Browse files Browse the repository at this point in the history
  • Loading branch information
javierm committed Aug 20, 2019
1 parent 072d53f commit 18cde13
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/score_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ module ScoreCalculator
def self.hot_score(resource)
return 0 unless resource.created_at

period = [
Setting["hot_score_period_in_days"].to_i,
((Time.current - resource.created_at) / 1.day).ceil
].min
period = [max_period, resource_age(resource)].min

votes_total = resource.votes_for.where("created_at >= ?", period.days.ago).count
votes_up = resource.get_upvotes.where("created_at >= ?", period.days.ago).count
Expand All @@ -27,4 +24,12 @@ def self.confidence_score(votes_total, votes_up)
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

0 comments on commit 18cde13

Please sign in to comment.