From bdb81b5346f2e9f83612f0bd6c05be25d1b6e56b Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Thu, 9 Nov 2023 18:23:24 +0200 Subject: [PATCH] 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. --- app/models/category_list.rb | 7 ++----- lib/plugin/instance.rb | 6 +++--- spec/models/category_list_spec.rb | 4 ++-- 3 files changed, 7 insertions(+), 10 deletions(-) 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