mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:11:04 +08:00
FEATURE: modifier API for plugins (#20887)
Introduces a new API for plugin data modification without class-based extension overhead. This commit introduces a new API that allows plugins to modify data in cases where they return different data rather than additional data, as is common with filtered_registers in DiscoursePluginRegistry. This API removes the need for defining class-based extension points. When a plugin registers a modifier, it will automatically be called if the plugin is enabled. The core will then modify the parameter sent to it using the block registered by the plugin: ```ruby DiscoursePluginRegistry.register_modifier(plugin_instance, :magic_sum_modifier) { |a, b| a + b } sum = DiscoursePluginRegistry.apply_modifier(:magic_sum_filter, 1, 2) expect(sum).to eq(3) ``` Key features of these modifiers: - Operate in a stack (first registered, first called) - Automatically disabled when the plugin is disabled - Pass the cumulative result of all block invocations to the caller
This commit is contained in:
@ -827,4 +827,17 @@ RSpec.describe Plugin::Instance do
|
||||
expect(callback_called).to eq(false)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#register_modifier" do
|
||||
let(:plugin) { Plugin::Instance.new }
|
||||
|
||||
after { DiscoursePluginRegistry.clear_modifiers! }
|
||||
|
||||
it "allows modifier registration" do
|
||||
plugin.register_modifier(:magic_sum_modifier) { |a, b| a + b }
|
||||
|
||||
sum = DiscoursePluginRegistry.apply_modifier(:magic_sum_modifier, 1, 2)
|
||||
expect(sum).to eq(3)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user