From ebada4a6b0a1dade733de261441f437ebc06a25a Mon Sep 17 00:00:00 2001 From: Mark VanLandingham Date: Thu, 23 Mar 2023 12:39:38 -0500 Subject: [PATCH] DEV: More specific API to including extra associations in CategoryList (#20790) --- app/models/category_list.rb | 35 +++++++++++-------------------- spec/models/category_list_spec.rb | 15 ------------- 2 files changed, 12 insertions(+), 38 deletions(-) diff --git a/app/models/category_list.rb b/app/models/category_list.rb index 5f6a125a73d..f23abdf0058 100644 --- a/app/models/category_list.rb +++ b/app/models/category_list.rb @@ -8,19 +8,19 @@ class CategoryList attr_accessor :categories, :uncategorized - def self.on_preload(&blk) - (@preload ||= Set.new) << blk + def self.register_included_association(association) + @included_assocations ||= [] + @included_assocations << association if !@included_assocations.include?(association) end - def self.cancel_preload(&blk) - if @preload - @preload.delete blk - @preload = nil if @preload.length == 0 - end - end - - def self.preload(category_list) - @preload.each { |preload| preload.call(category_list) } if @preload + def self.included_associations + [ + :uploaded_background, + :uploaded_logo, + :uploaded_logo_dark, + :topic_only_relative_url, + subcategories: [:topic_only_relative_url], + ].concat(@included_assocations || []) end def initialize(guardian = nil, options = {}) @@ -119,18 +119,7 @@ class CategoryList end def find_categories - @categories = - Category.includes( - :uploaded_background, - :uploaded_logo, - :uploaded_logo_dark, - :topic_only_relative_url, - subcategories: [:topic_only_relative_url], - ) - - CategoryList.preload(self) - - @categories = @categories.secured(@guardian) + @categories = Category.includes(CategoryList.included_associations).secured(@guardian) @categories = @categories.where( diff --git a/spec/models/category_list_spec.rb b/spec/models/category_list_spec.rb index b331c78f3d9..e712d1ea378 100644 --- a/spec/models/category_list_spec.rb +++ b/spec/models/category_list_spec.rb @@ -12,21 +12,6 @@ RSpec.describe CategoryList do fab!(:admin) { Fabricate(:admin) } let(:category_list) { CategoryList.new(Guardian.new(user), include_topics: true) } - describe "preload" do - it "allows preloading of data" do - preloaded_list = nil - preloader = lambda { |view| preloaded_list = view } - - CategoryList.on_preload(&preloader) - - expect(preloaded_list).to eq(nil) - category_list - expect(preloaded_list).to eq(preloaded_list) - - CategoryList.cancel_preload(&preloader) - end - end - describe "security" do it "properly hide secure categories" do cat = Fabricate(:category_with_definition)