mirror of
https://github.com/discourse/discourse.git
synced 2025-02-16 23:12:45 +08:00
DEV: Add category custom field preloading to CategoryList (#23969)
This commit also introduced a plugin API for preloading category custom fields.
This commit is contained in:
parent
b8ee52c4cb
commit
2a75656ff2
|
@ -6,6 +6,9 @@ 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)
|
||||
|
@ -139,6 +142,10 @@ class CategoryList
|
|||
|
||||
@categories = query.to_a
|
||||
|
||||
if preloaded_category_custom_fields.any?
|
||||
Category.preload_custom_fields(@categories, preloaded_category_custom_fields)
|
||||
end
|
||||
|
||||
include_subcategories = @options[:include_subcategories] == true
|
||||
|
||||
notification_levels = CategoryUser.notification_levels_for(@guardian.user)
|
||||
|
|
|
@ -290,6 +290,13 @@ class Plugin::Instance
|
|||
Upload.add_in_use_callback(&block)
|
||||
end
|
||||
|
||||
# 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
|
||||
end
|
||||
|
||||
def custom_avatar_column(column)
|
||||
reloadable_patch do |plugin|
|
||||
UserLookup.lookup_columns << column
|
||||
|
|
|
@ -357,4 +357,21 @@ RSpec.describe CategoryList do
|
|||
DiscoursePluginRegistry.clear_modifiers!
|
||||
end
|
||||
end
|
||||
|
||||
describe "with custom fields" do
|
||||
fab!(:category) { Fabricate(:category, user: admin) }
|
||||
|
||||
before { category.upsert_custom_fields("bob" => "marley") }
|
||||
after { CategoryList.preloaded_category_custom_fields = Set.new }
|
||||
|
||||
it "can preloads custom fields" do
|
||||
CategoryList.preloaded_category_custom_fields << "bob"
|
||||
|
||||
expect(category_list.categories[-1].custom_field_preloaded?("bob")).to eq(true)
|
||||
end
|
||||
|
||||
it "does not preload fields that were not set for preloading" do
|
||||
expect(category_list.categories[-1].custom_field_preloaded?("bob")).to be_falsey
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -372,6 +372,44 @@ RSpec.describe CategoriesController do
|
|||
response.parsed_body["category_list"]["categories"].map { |x| x["id"] },
|
||||
).not_to include(uncategorized.id)
|
||||
end
|
||||
|
||||
describe "with serialized category custom fields" do
|
||||
class CategoryPlugin < Plugin::Instance
|
||||
end
|
||||
|
||||
let!(:plugin) do
|
||||
plugin = CategoryPlugin.new
|
||||
plugin.add_to_serializer(:basic_category, :bob) { object.custom_fields["bob"] }
|
||||
plugin
|
||||
end
|
||||
|
||||
before { category.upsert_custom_fields("bob" => "marley") }
|
||||
after { CategoryList.preloaded_category_custom_fields = Set.new }
|
||||
|
||||
context "when custom fields are not preloaded" do
|
||||
it "increases the query count" do
|
||||
queries = track_sql_queries { get "/categories.json" }
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
category = response.parsed_body["category_list"]["categories"][-1]
|
||||
expect(category["bob"]).to eq("marley")
|
||||
expect(queries.count).to eq(7)
|
||||
end
|
||||
end
|
||||
|
||||
context "when custom fields are preloaded" do
|
||||
before { CategoryList.preloaded_category_custom_fields << "bob" }
|
||||
|
||||
it "does not increase the query count" do
|
||||
queries = track_sql_queries { get "/categories.json" }
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
category = response.parsed_body["category_list"]["categories"][-1]
|
||||
expect(category["bob"]).to eq("marley")
|
||||
expect(queries.count).to eq(6)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "extensibility event" do
|
||||
|
|
Loading…
Reference in New Issue
Block a user