Created
November 24, 2011 18:31
-
-
Save xdite/1391980 to your computer and use it in GitHub Desktop.
stackoverflow & reddit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Order | |
module Ranking | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
def update_r_ranking | |
t = self | |
t.r_ranking = Topic.calculate_ranking(t.pushes_count, t.created_at) | |
t.save(:validate => false) | |
end | |
def update_s_ranking | |
t = self | |
factor = Math.log10(t.visits_count+1)*4 + (t.comments_count * t.pushes_count )/5 + t.comment_pushes_count.to_i | |
#http://meta.stackoverflow.com/questions/11602/what-formula-should-be-used-to-determine-hot-questions | |
t.s_ranking = Topic.calculate_ranking(factor, t.created_at) | |
t.save(:validate => false) | |
end | |
module ClassMethods | |
def calculate_ranking(score, created_at) | |
order = Math.log10(([score.abs,1].max)) | |
if score > 0 | |
sign = 1 | |
elsif score < 0 | |
sign = -1 | |
else | |
sign = 0 | |
end | |
seconds = created_at - DateTime.new(1970,1,1) | |
long_num = order + sign * seconds / 45000 | |
(long_num * 10**7).round.to_f / (10**7) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment