FEATURE: Add new plugin API to allow plugins to extend `Site#categories` (#13773)
This commit is contained in:
parent
8de8989576
commit
a1047f5ef4
|
@ -7,6 +7,14 @@ class Site
|
|||
cattr_accessor :preloaded_category_custom_fields
|
||||
self.preloaded_category_custom_fields = Set.new
|
||||
|
||||
def self.add_categories_callbacks(&block)
|
||||
categories_callbacks << block
|
||||
end
|
||||
|
||||
def self.categories_callbacks
|
||||
@categories_callbacks ||= []
|
||||
end
|
||||
|
||||
def initialize(guardian)
|
||||
@guardian = guardian
|
||||
end
|
||||
|
@ -104,6 +112,11 @@ class Site
|
|||
end
|
||||
|
||||
categories.reject! { |c| c[:parent_category_id] && !by_id[c[:parent_category_id]] }
|
||||
|
||||
self.class.categories_callbacks.each do |callback|
|
||||
callback.call(categories)
|
||||
end
|
||||
|
||||
categories
|
||||
end
|
||||
end
|
||||
|
|
|
@ -231,6 +231,17 @@ class Plugin::Instance
|
|||
DiscoursePluginRegistry.register_topic_thumbnail_size(size, self)
|
||||
end
|
||||
|
||||
# Register a callback to add custom payload to Site#categories
|
||||
# Example usage:
|
||||
# register_site_categories_callback do |categories|
|
||||
# categories.each do |category|
|
||||
# category[:some_field] = 'test'
|
||||
# end
|
||||
# end
|
||||
def register_site_categories_callback(&block)
|
||||
Site.add_categories_callbacks(&block)
|
||||
end
|
||||
|
||||
def custom_avatar_column(column)
|
||||
reloadable_patch do |plugin|
|
||||
UserLookup.lookup_columns << column
|
||||
|
|
|
@ -647,4 +647,25 @@ describe Plugin::Instance do
|
|||
}.to raise_error(RuntimeError)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#register_site_categories_callback' do
|
||||
fab!(:category) { Fabricate(:category) }
|
||||
|
||||
it 'adds a callback to the Site#categories' do
|
||||
instance = Plugin::Instance.new
|
||||
|
||||
instance.register_site_categories_callback do |categories|
|
||||
categories.each do |category|
|
||||
category[:test_field] = "test"
|
||||
end
|
||||
end
|
||||
|
||||
site = Site.new(Guardian.new)
|
||||
|
||||
expect(site.categories.first[:test_field]).to eq("test")
|
||||
ensure
|
||||
Site.clear_cache
|
||||
Site.categories_callbacks.clear
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue