DEV: More specific API to including extra associations in CategoryList (#20790)

This commit is contained in:
Mark VanLandingham 2023-03-23 12:39:38 -05:00 committed by GitHub
parent fe127b166d
commit ebada4a6b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 38 deletions

View File

@ -8,19 +8,19 @@ class CategoryList
attr_accessor :categories, :uncategorized attr_accessor :categories, :uncategorized
def self.on_preload(&blk) def self.register_included_association(association)
(@preload ||= Set.new) << blk @included_assocations ||= []
@included_assocations << association if !@included_assocations.include?(association)
end end
def self.cancel_preload(&blk) def self.included_associations
if @preload [
@preload.delete blk :uploaded_background,
@preload = nil if @preload.length == 0 :uploaded_logo,
end :uploaded_logo_dark,
end :topic_only_relative_url,
subcategories: [:topic_only_relative_url],
def self.preload(category_list) ].concat(@included_assocations || [])
@preload.each { |preload| preload.call(category_list) } if @preload
end end
def initialize(guardian = nil, options = {}) def initialize(guardian = nil, options = {})
@ -119,18 +119,7 @@ class CategoryList
end end
def find_categories def find_categories
@categories = @categories = Category.includes(CategoryList.included_associations).secured(@guardian)
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 = @categories =
@categories.where( @categories.where(

View File

@ -12,21 +12,6 @@ RSpec.describe CategoryList do
fab!(:admin) { Fabricate(:admin) } fab!(:admin) { Fabricate(:admin) }
let(:category_list) { CategoryList.new(Guardian.new(user), include_topics: true) } 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 describe "security" do
it "properly hide secure categories" do it "properly hide secure categories" do
cat = Fabricate(:category_with_definition) cat = Fabricate(:category_with_definition)