docbrown/app/labor/article_suggester.rb
Ben Halpern 2d97a5d837
Improve performance for additional content boxes and tags index (#1750)
* Improve performance for additional content boxes and tags index

* Modify followed_articles endpoint with new classic article api
2019-02-06 16:50:35 -04:00

37 lines
854 B
Ruby

class ArticleSuggester
attr_accessor :article
def initialize(article)
@article = article
end
def articles(num = 4)
if article.tag_list.any?
(suggestions_by_tag + other_suggestions(num)).flatten.first(num).to_a
else
other_suggestions(num).to_a
end
end
def other_suggestions(num = 4)
Article.where(featured: true, published: true).
where.not(id: article.id).
order("hotness_score DESC").
includes(:user).
offset(rand(0..offsets[1])).
first(num)
end
def suggestions_by_tag
Article.tagged_with(article.tag_list, any: true).
where(published: true).
where.not(id: article.id).
order("hotness_score DESC").
includes(:user).
offset(rand(0..offsets[0])).
first(4)
end
def offsets
Rails.env.production? ? [10, 120] : [0, 0]
end
end