FIX: category list order using category featured topics (#7283)

This commit is contained in:
Maja Komel
2019-04-25 11:07:19 +02:00
committed by Régis Hanol
parent e386fa7674
commit 996e5e5dfa
2 changed files with 66 additions and 20 deletions

View File

@@ -43,6 +43,19 @@ class CategoryList
"categories_list".freeze
end
def self.order_categories(categories)
if SiteSetting.fixed_category_positions
categories.order(:position, :id)
else
allowed_category_ids = categories.pluck(:id) << nil # `nil` is necessary to include categories without any associated topics
categories.left_outer_joins(:featured_topics)
.where(topics: { category_id: allowed_category_ids })
.group('categories.id')
.order("max(topics.bumped_at) DESC NULLS LAST")
.order('categories.id ASC')
end
end
private
def find_relevant_topics
@@ -75,11 +88,7 @@ class CategoryList
@categories = @categories.where("categories.parent_category_id = ?", @options[:parent_category_id].to_i) if @options[:parent_category_id].present?
if SiteSetting.fixed_category_positions
@categories = @categories.order(:position, :id)
else
@categories = @categories.includes(:latest_post).order("posts.created_at DESC NULLS LAST").order('categories.id ASC')
end
@categories = self.class.order_categories(@categories)
@categories = @categories.to_a