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

@@ -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