Skip to content

A platform for community discussion. Free, open, simple.

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

Bianca Nenciubdb81b5346f2
diff --git a/app/models/category_list.rb b/app/models/category_list.rb
index ed7fa6673d0..5c3a673da7d 100644
--- a/app/models/category_list.rb
+++ b/app/models/category_list.rb
@@ -6,9 +6,6 @@ class CategoryList
cattr_accessor :preloaded_topic_custom_fields
self.preloaded_topic_custom_fields = Set.new
- cattr_accessor :preloaded_category_custom_fields
- self.preloaded_category_custom_fields = Set.new
-
attr_accessor :categories, :uncategorized
def self.register_included_association(association)
@@ -142,8 +139,8 @@ class CategoryList
@categories = query.to_a
- if preloaded_category_custom_fields.any?
- Category.preload_custom_fields(@categories, preloaded_category_custom_fields)
+ if Site.preloaded_category_custom_fields.any?
+ Category.preload_custom_fields(@categories, Site.preloaded_category_custom_fields)
end
include_subcategories = @options[:include_subcategories] == true
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb
index 324b555f990..53890edd99f 100644
--- a/lib/plugin/instance.rb
+++ b/lib/plugin/instance.rb
@@ -292,9 +292,9 @@ class Plugin::Instance
# Registers a category custom field to be loaded when rendering a category list
# Example usage:
- # register_category_list_preloaded_category_custom_fields("custom_field")
- def register_category_list_preloaded_category_custom_fields(field)
- CategoryList.preloaded_category_custom_fields << field
+ # register_preloaded_category_custom_fields("custom_field")
+ def register_preloaded_category_custom_fields(field)
+ Site.preloaded_category_custom_fields << field
end
def custom_avatar_column(column)
diff --git a/spec/models/category_list_spec.rb b/spec/models/category_list_spec.rb
index 255fb859b85..797073e3a41 100644
--- a/spec/models/category_list_spec.rb
+++ b/spec/models/category_list_spec.rb
@@ -362,10 +362,10 @@ RSpec.describe CategoryList do
fab!(:category) { Fabricate(:category, user: admin) }
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
- CategoryList.preloaded_category_custom_fields << "bob"
+ Site.preloaded_category_custom_fields << "bob"
expect(category_list.categories[-1].custom_field_preloaded?("bob")).to eq(true)
end