DEV: Pass in site guardian for `Plugin::Instance.register_site_categories_callback` (#19430)

The guardian is useful for plugins to determine if the callback should
do anything. A common use case is to not do anything in the callback if
the user is anonymous.
This commit is contained in:
Alan Guo Xiang Tan 2022-12-13 07:49:13 +08:00 committed by GitHub
parent 3ee4b59c64
commit 0abd772098
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 3 deletions

View File

@ -134,7 +134,7 @@ class Site
categories.reject! { |c| c[:parent_category_id] && !by_id[c[:parent_category_id]] }
self.class.categories_callbacks.each do |callback|
callback.call(categories)
callback.call(categories, @guardian)
end
categories

View File

@ -649,13 +649,17 @@ RSpec.describe Plugin::Instance do
it 'adds a callback to the Site#categories' do
instance = Plugin::Instance.new
instance.register_site_categories_callback do |categories|
site_guardian = Guardian.new
instance.register_site_categories_callback do |categories, guardian|
categories.each do |category|
category[:test_field] = "test"
end
expect(guardian).to eq(site_guardian)
end
site = Site.new(Guardian.new)
site = Site.new(site_guardian)
expect(site.categories.first[:test_field]).to eq("test")
ensure