docbrown/app/services/search/article.rb
Jeremy Friesen c5bf4bf880
Extracting duplicate logic (#16035)
Prior to this commit, we had two places that need to know the nuances
ofquerying for tag flares and what we should include in our queries for
serialization.

With this commit, we're factoring towards a common source of knowledge
and providing a much needed test for the expected output of this
serialization.

Loosely related to #15916, #15983, #15994, and #16032.
2022-01-10 15:19:04 -05:00

32 lines
980 B
Ruby

module Search
class Article
DEFAULT_SORT_BY = "hotness_score DESC, comments_count DESC".freeze
def self.search_documents(
term: nil,
user_id: nil,
sort_by: nil,
sort_direction: nil,
page: nil,
per_page: nil
)
relation = Homepage::ArticlesQuery.call(user_id: user_id, page: page, per_page: per_page)
relation = relation.search_articles(term) if term.present?
relation = sort(relation, term, sort_by, sort_direction)
Homepage::ArticleSerializer.serialized_collection_from(relation: relation)
end
def self.sort(relation, term, sort_by, sort_direction)
# By skipping ordering, we rely on the custom ranking defined in the article's tsvector document
return relation if term.present? && sort_by.blank?
return relation.reorder(sort_by => sort_direction) if sort_by&.to_sym == :published_at
relation.reorder(DEFAULT_SORT_BY)
end
private_class_method :sort
end
end