FIX: Preload all ancestors of sidebar categories (#26715)

... instead of just the immediate parents.
This commit is contained in:
Daniel Waterworth 2024-05-06 11:55:20 -05:00 committed by GitHub
parent a6b8051645
commit e2ceea8815
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 8 deletions

View File

@ -111,12 +111,7 @@ class Site
if @guardian.authenticated? if @guardian.authenticated?
sidebar_category_ids = @guardian.user.secured_sidebar_category_ids(@guardian) sidebar_category_ids = @guardian.user.secured_sidebar_category_ids(@guardian)
preloaded_category_ids.concat( preloaded_category_ids.concat(
Category Category.secured(@guardian).ancestors_of(sidebar_category_ids).pluck(:id),
.secured(@guardian)
.select(:parent_category_id)
.distinct
.where(id: sidebar_category_ids)
.pluck(:parent_category_id),
) )
preloaded_category_ids.concat(sidebar_category_ids) preloaded_category_ids.concat(sidebar_category_ids)
end end

View File

@ -195,14 +195,17 @@ RSpec.describe Site do
expect(site.categories).to eq([]) expect(site.categories).to eq([])
end end
it "returns only sidebar categories and their parent categories" do it "returns only sidebar categories and their ancestors" do
parent_category = Fabricate(:category) SiteSetting.max_category_nesting = 3
grandfather_category = Fabricate(:category)
parent_category = Fabricate(:category, parent_category: grandfather_category)
category.update!(parent_category: parent_category) category.update!(parent_category: parent_category)
Fabricate(:category_sidebar_section_link, linkable: category, user: user) Fabricate(:category_sidebar_section_link, linkable: category, user: user)
site = Site.new(Guardian.new(user)) site = Site.new(Guardian.new(user))
expect(site.categories.map { |c| c[:id] }).to contain_exactly( expect(site.categories.map { |c| c[:id] }).to contain_exactly(
grandfather_category.id,
parent_category.id, parent_category.id,
category.id, category.id,
) )