A platform for community discussion. Free, open, simple.
FIX: Preload parent categories for sidebar (#25726)
Bianca Nenciua24d110258f3
diff --git a/app/models/site.rb b/app/models/site.rbindex c46c666e0b4..1390c1c25a3 100644--- a/app/models/site.rb+++ b/app/models/site.rb@@ -109,7 +109,16 @@ class Siteif @guardian.can_lazy_load_categories?preloaded_category_ids = []if @guardian.authenticated?- preloaded_category_ids.concat(@guardian.user.secured_sidebar_category_ids(@guardian))+ 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),+ )+ preloaded_category_ids.concat(sidebar_category_ids)endenddiff --git a/spec/models/site_spec.rb b/spec/models/site_spec.rbindex ff49ad5c5c9..cc23095c014 100644--- a/spec/models/site_spec.rb+++ b/spec/models/site_spec.rb@@ -183,6 +183,40 @@ RSpec.describe Site doDiscoursePluginRegistry.clear_modifiers!endend++ context "with lazy loaded categories enabled" do+ fab!(:user)++ before { SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}" }++ it "does not return any categories for anonymous users" do+ site = Site.new(Guardian.new)++ expect(site.categories).to eq([])+ end++ it "returns only sidebar categories and their parent categories" do+ parent_category = Fabricate(: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(+ parent_category.id,+ category.id,+ )+ end++ it "returns only visible sidebar categories" do+ Fabricate(:category_sidebar_section_link, linkable: category, user: user)+ category.update!(read_restricted: true)++ site = Site.new(Guardian.new(user))++ expect(site.categories).to eq([])+ end+ endendit "omits groups user can not see" dodiff --git a/spec/serializers/site_serializer_spec.rb b/spec/serializers/site_serializer_spec.rbindex 38019a3a6e8..668c30f3894 100644--- a/spec/serializers/site_serializer_spec.rb+++ b/spec/serializers/site_serializer_spec.rb@@ -133,32 +133,24 @@ RSpec.describe SiteSerializer docontext "with lazy loaded categories enabled" dofab!(:user)- let(:guardian) { Guardian.new(user) }+ fab!(:category)+ fab!(:sidebar) { Fabricate(:category_sidebar_section_link, linkable: category, user: user) }before { SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}" }- it "categories does not include any categories for anonymous users" do+ it "does not include any categories for anonymous users" doserialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_jsonexpect(serialized[:categories]).to eq(nil)end- it "categories include only sidebar categories" do- Fabricate(:category_sidebar_section_link, linkable: category, user: user)+ it "includes preloaded categories for logged in users" do+ guardian = Guardian.new(user)serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_jsonexpect(serialized[:categories].map { |c| c[:id] }).to contain_exactly(category.id)end-- it "categories include only visible sidebar categories" do- Fabricate(:category_sidebar_section_link, linkable: category, user: user)- category.update!(read_restricted: true)-- serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json-- expect(serialized[:categories]).to eq(nil)- endenddescribe "#anonymous_default_navigation_menu_tags" do