From e2ceea88154844faebaea27768a9bfa3db4c3309 Mon Sep 17 00:00:00 2001 From: Daniel Waterworth Date: Mon, 6 May 2024 11:55:20 -0500 Subject: [PATCH] FIX: Preload all ancestors of sidebar categories (#26715) ... instead of just the immediate parents. --- app/models/site.rb | 7 +------ spec/models/site_spec.rb | 7 +++++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/app/models/site.rb b/app/models/site.rb index 03b435dede7..e5e08a3ffc2 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -111,12 +111,7 @@ class Site if @guardian.authenticated? sidebar_category_ids = @guardian.user.secured_sidebar_category_ids(@guardian) preloaded_category_ids.concat( - Category - .secured(@guardian) - .select(:parent_category_id) - .distinct - .where(id: sidebar_category_ids) - .pluck(:parent_category_id), + Category.secured(@guardian).ancestors_of(sidebar_category_ids).pluck(:id), ) preloaded_category_ids.concat(sidebar_category_ids) end diff --git a/spec/models/site_spec.rb b/spec/models/site_spec.rb index cc23095c014..f2a77c56845 100644 --- a/spec/models/site_spec.rb +++ b/spec/models/site_spec.rb @@ -195,14 +195,17 @@ RSpec.describe Site do expect(site.categories).to eq([]) end - it "returns only sidebar categories and their parent categories" do - parent_category = Fabricate(:category) + it "returns only sidebar categories and their ancestors" do + SiteSetting.max_category_nesting = 3 + grandfather_category = Fabricate(:category) + parent_category = Fabricate(:category, parent_category: grandfather_category) category.update!(parent_category: parent_category) Fabricate(:category_sidebar_section_link, linkable: category, user: user) site = Site.new(Guardian.new(user)) expect(site.categories.map { |c| c[:id] }).to contain_exactly( + grandfather_category.id, parent_category.id, category.id, )