DEV: Use a single registry for preloaded category custom fields (#24272)

There was a registry for preloaded site categories and a new one has
been introduced recently for categories serialized through a
CategoryList.

Having two registries created a lot of friction for developers and this
commit merges them into a single one, providing a unified API.
This commit is contained in:
Bianca Nenciu 2023-11-09 18:23:24 +02:00 committed by GitHub
parent e772bb9fce
commit bdb81b5346
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 10 deletions

View File

@ -6,9 +6,6 @@ class CategoryList
cattr_accessor :preloaded_topic_custom_fields cattr_accessor :preloaded_topic_custom_fields
self.preloaded_topic_custom_fields = Set.new self.preloaded_topic_custom_fields = Set.new
cattr_accessor :preloaded_category_custom_fields
self.preloaded_category_custom_fields = Set.new
attr_accessor :categories, :uncategorized attr_accessor :categories, :uncategorized
def self.register_included_association(association) def self.register_included_association(association)
@ -142,8 +139,8 @@ class CategoryList
@categories = query.to_a @categories = query.to_a
if preloaded_category_custom_fields.any? if Site.preloaded_category_custom_fields.any?
Category.preload_custom_fields(@categories, preloaded_category_custom_fields) Category.preload_custom_fields(@categories, Site.preloaded_category_custom_fields)
end end
include_subcategories = @options[:include_subcategories] == true include_subcategories = @options[:include_subcategories] == true

View File

@ -292,9 +292,9 @@ class Plugin::Instance
# Registers a category custom field to be loaded when rendering a category list # Registers a category custom field to be loaded when rendering a category list
# Example usage: # Example usage:
# register_category_list_preloaded_category_custom_fields("custom_field") # register_preloaded_category_custom_fields("custom_field")
def register_category_list_preloaded_category_custom_fields(field) def register_preloaded_category_custom_fields(field)
CategoryList.preloaded_category_custom_fields << field Site.preloaded_category_custom_fields << field
end end
def custom_avatar_column(column) def custom_avatar_column(column)

View File

@ -362,10 +362,10 @@ RSpec.describe CategoryList do
fab!(:category) { Fabricate(:category, user: admin) } fab!(:category) { Fabricate(:category, user: admin) }
before { category.upsert_custom_fields("bob" => "marley") } before { category.upsert_custom_fields("bob" => "marley") }
after { CategoryList.preloaded_category_custom_fields = Set.new } after { Site.reset_preloaded_category_custom_fields }
it "can preloads custom fields" do it "can preloads custom fields" do
CategoryList.preloaded_category_custom_fields << "bob" Site.preloaded_category_custom_fields << "bob"
expect(category_list.categories[-1].custom_field_preloaded?("bob")).to eq(true) expect(category_list.categories[-1].custom_field_preloaded?("bob")).to eq(true)
end end