DEV: Added modifier hooks to allow plugins to tweak how categories and groups are fetched (#21837)

This commit adds modifiers that allow plugins to change how categories and groups are prefetched into the application and listed in the respective controllers.

Possible use cases:

- prevent some categories/groups from being prefetched when the application loads for performance reasons.
- prevent some categories/groups from being listed in their respective index pages.
This commit is contained in:
Sérgio Saquetim
2023-05-30 18:41:50 -03:00
committed by GitHub
parent 1e3a5124da
commit 908117e270
8 changed files with 196 additions and 26 deletions

View File

@@ -119,17 +119,19 @@ class CategoryList
end
def find_categories
@categories = Category.includes(CategoryList.included_associations).secured(@guardian)
query = Category.includes(CategoryList.included_associations).secured(@guardian)
@categories =
@categories.where(
query =
query.where(
"categories.parent_category_id = ?",
@options[:parent_category_id].to_i,
) if @options[:parent_category_id].present?
@categories = self.class.order_categories(@categories)
query = self.class.order_categories(query)
query =
DiscoursePluginRegistry.apply_modifier(:category_list_find_categories_query, query, self)
@categories = @categories.to_a
@categories = query.to_a
include_subcategories = @options[:include_subcategories] == true

View File

@@ -657,14 +657,17 @@ class Group < ActiveRecord::Base
groups ||= Group
relation =
groups.where("name ILIKE :term_like OR full_name ILIKE :term_like", term_like: "%#{name}%")
groups.where(
"groups.name ILIKE :term_like OR groups.full_name ILIKE :term_like",
term_like: "%#{name}%",
)
if sort == :auto
prefix = "#{name.gsub("_", "\\_")}%"
relation =
relation.reorder(
DB.sql_fragment(
"CASE WHEN name ILIKE :like OR full_name ILIKE :like THEN 0 ELSE 1 END ASC, name ASC",
"CASE WHEN groups.name ILIKE :like OR groups.full_name ILIKE :like THEN 0 ELSE 1 END ASC, groups.name ASC",
like: prefix,
),
)

View File

@@ -71,20 +71,27 @@ class Site
.cache
.fetch(categories_cache_key, expires_in: 30.minutes) do
categories =
Category
.includes(
:uploaded_logo,
:uploaded_logo_dark,
:uploaded_background,
:tags,
:tag_groups,
:form_templates,
category_required_tag_groups: :tag_group,
)
.joins("LEFT JOIN topics t on t.id = categories.topic_id")
.select("categories.*, t.slug topic_slug")
.order(:position)
.to_a
begin
query =
Category
.includes(
:uploaded_logo,
:uploaded_logo_dark,
:uploaded_background,
:tags,
:tag_groups,
:form_templates,
category_required_tag_groups: :tag_group,
)
.joins("LEFT JOIN topics t on t.id = categories.topic_id")
.select("categories.*, t.slug topic_slug")
.order(:position)
query =
DiscoursePluginRegistry.apply_modifier(:site_all_categories_cache_query, query, self)
query.to_a
end
if preloaded_category_custom_fields.present?
Category.preload_custom_fields(categories, preloaded_category_custom_fields)
@@ -151,7 +158,13 @@ class Site
end
def groups
Group.visible_groups(@guardian.user, "name ASC", include_everyone: true).includes(:flair_upload)
query =
Group.visible_groups(@guardian.user, "groups.name ASC", include_everyone: true).includes(
:flair_upload,
)
query = DiscoursePluginRegistry.apply_modifier(:site_groups_query, query, self)
query
end
def archetypes