DEV: Add preload API to CategoryList (#20778)
This commit is contained in:
parent
f12e77d500
commit
32aa821f12
|
@ -8,6 +8,21 @@ class CategoryList
|
|||
|
||||
attr_accessor :categories, :uncategorized
|
||||
|
||||
def self.on_preload(&blk)
|
||||
(@preload ||= Set.new) << blk
|
||||
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
|
||||
end
|
||||
|
||||
def initialize(guardian = nil, options = {})
|
||||
@guardian = guardian || Guardian.new
|
||||
@options = options
|
||||
|
@ -111,7 +126,11 @@ class CategoryList
|
|||
:uploaded_logo_dark,
|
||||
:topic_only_relative_url,
|
||||
subcategories: [:topic_only_relative_url],
|
||||
).secured(@guardian)
|
||||
)
|
||||
|
||||
CategoryList.preload(self)
|
||||
|
||||
@categories = @categories.secured(@guardian)
|
||||
|
||||
@categories =
|
||||
@categories.where(
|
||||
|
|
|
@ -12,6 +12,21 @@ 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)
|
||||
|
|
Loading…
Reference in New Issue