mirror of
https://github.com/discourse/discourse.git
synced 2025-03-21 03:16:15 +08:00
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
|
cattr_accessor :preloaded_category_custom_fields
|
||||||
self.preloaded_category_custom_fields = Set.new
|
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)
|
def initialize(guardian)
|
||||||
@guardian = guardian
|
@guardian = guardian
|
||||||
end
|
end
|
||||||
@ -104,6 +112,11 @@ class Site
|
|||||||
end
|
end
|
||||||
|
|
||||||
categories.reject! { |c| c[:parent_category_id] && !by_id[c[:parent_category_id]] }
|
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
|
categories
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -231,6 +231,17 @@ class Plugin::Instance
|
|||||||
DiscoursePluginRegistry.register_topic_thumbnail_size(size, self)
|
DiscoursePluginRegistry.register_topic_thumbnail_size(size, self)
|
||||||
end
|
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)
|
def custom_avatar_column(column)
|
||||||
reloadable_patch do |plugin|
|
reloadable_patch do |plugin|
|
||||||
UserLookup.lookup_columns << column
|
UserLookup.lookup_columns << column
|
||||||
|
@ -647,4 +647,25 @@ describe Plugin::Instance do
|
|||||||
}.to raise_error(RuntimeError)
|
}.to raise_error(RuntimeError)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user