Created
December 22, 2022 03:03
-
-
Save tgxworld/c9ee8a6b566377a4900e00199ddf4067 to your computer and use it in GitHub Desktop.
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
diff --git a/app/models/category_list.rb b/app/models/category_list.rb | |
index ef8e78329e..d106fcead6 100644 | |
--- a/app/models/category_list.rb | |
+++ b/app/models/category_list.rb | |
@@ -41,19 +41,6 @@ class CategoryList | |
"categories_list" | |
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 | |
@@ -101,58 +88,71 @@ class CategoryList | |
:uploaded_logo, | |
:uploaded_logo_dark, | |
:topic_only_relative_url, | |
- subcategories: [:topic_only_relative_url] | |
).secured(@guardian) | |
+ include_subcategories = @options[:include_subcategories] == true | |
+ | |
+ if include_subcategories | |
+ @categories = @categories.includes(subcategories: [:topic_only_relative_url]) | |
+ else | |
+ @categories = @categories.where("categories.parent_category_id IS NULL") | |
+ end | |
+ | |
@categories = @categories.where("categories.parent_category_id = ?", @options[:parent_category_id].to_i) if @options[:parent_category_id].present? | |
- @categories = self.class.order_categories(@categories) | |
+ @categories = order_categories(@categories) | |
@categories = @categories.to_a | |
- include_subcategories = @options[:include_subcategories] == true | |
- | |
notification_levels = CategoryUser.notification_levels_for(@guardian.user) | |
default_notification_level = CategoryUser.default_notification_level | |
if @options[:parent_category_id].blank? | |
- subcategory_ids = {} | |
subcategory_list = {} | |
to_delete = Set.new | |
+ | |
@categories.each do |c| | |
- if c.parent_category_id.present? | |
- subcategory_ids[c.parent_category_id] ||= [] | |
- subcategory_ids[c.parent_category_id] << c.id | |
- if include_subcategories | |
- subcategory_list[c.parent_category_id] ||= [] | |
- subcategory_list[c.parent_category_id] << c | |
- end | |
- to_delete << c | |
+ if c.parent_category_id.present? && include_subcategories | |
+ subcategory_list[c.parent_category_id] ||= [] | |
+ subcategory_list[c.parent_category_id] << c | |
+ to_delete << c.id | |
end | |
end | |
+ | |
@categories.each do |c| | |
- c.subcategory_ids = subcategory_ids[c.id] || [] | |
+ c.subcategory_ids = | |
+ if c.parent_category_id | |
+ Category.subcategory_ids(c.id) - [c.id] # Category.subcategory_ids returns the category id as well | |
+ else | |
+ [] | |
+ end | |
+ | |
if include_subcategories | |
c.subcategory_list = subcategory_list[c.id] || [] | |
end | |
end | |
- @categories.delete_if { |c| to_delete.include?(c) } | |
+ | |
+ @categories.delete_if { |c| to_delete.include?(c.id) } | |
end | |
allowed_topic_create = Set.new(Category.topic_create_allowed(@guardian).pluck(:id)) | |
+ | |
categories_with_descendants.each do |category| | |
category.notification_level = notification_levels[category.id] || default_notification_level | |
category.permission = CategoryGroup.permission_types[:full] if allowed_topic_create.include?(category.id) | |
- category.has_children = category.subcategories.present? | |
+ category.has_children = category.subcategory_ids.present? | |
end | |
if @topics_by_category_id | |
categories_with_descendants.each do |c| | |
topics_in_cat = @topics_by_category_id[c.id] | |
+ | |
if topics_in_cat.present? | |
c.displayable_topics = [] | |
+ | |
topics_in_cat.each do |topic_id| | |
topic = @topics_by_id[topic_id] | |
+ | |
if topic.present? && @guardian.can_see?(topic) | |
# topic.category is very slow under rails 4.2 | |
topic.association(:category).target = c | |
@@ -164,6 +164,17 @@ class CategoryList | |
end | |
end | |
+ def order_categories(categories) | |
+ if SiteSetting.fixed_category_positions | |
+ categories.order(:position, :id) | |
+ else | |
+ categories.left_outer_joins(:featured_topics) | |
+ .group('categories.id') | |
+ .order("max(topics.bumped_at) DESC NULLS LAST") | |
+ .order('categories.id ASC') | |
+ end | |
+ end | |
+ | |
def prune_empty | |
return if SiteSetting.allow_uncategorized_topics | |
@categories.delete_if { |c| c.uncategorized? } | |
@@ -210,8 +221,14 @@ class CategoryList | |
return @categories_with_children if @categories_with_children && (categories == @categories) | |
return nil if categories.nil? | |
- result = categories.flat_map do |c| | |
- [c, *categories_with_descendants(c.subcategory_list)] | |
+ result = categories.flat_map do |category| | |
+ categories = [category] | |
+ | |
+ if category.subcategory_list | |
+ categories.push(*categories_with_descendants(category.subcategory_list)) | |
+ end | |
+ | |
+ categories | |
end | |
@categories_with_children = result if categories == @categories |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment