mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 18:51:08 +08:00
DEV: Prevent clearing plugin modifiers during plugin spec runs (#21359)
Clearing modifiers during a plugin spec run will affect all future specs. Instead, this commit introduces a more surgical `.unregister_modifier` API which plugins can use if they need to add/remove a modifier during a specific spec.
This commit is contained in:
@ -247,6 +247,9 @@ class DiscoursePluginRegistry
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.clear_modifiers!
|
def self.clear_modifiers!
|
||||||
|
if Rails.env.test? && GlobalSetting.load_plugins?
|
||||||
|
raise "Clearing modifiers during a plugin spec run will affect all future specs. Use unregister_modifier instead."
|
||||||
|
end
|
||||||
@modifiers = nil
|
@modifiers = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -256,6 +259,18 @@ class DiscoursePluginRegistry
|
|||||||
modifiers << [plugin_instance, blk]
|
modifiers << [plugin_instance, blk]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.unregister_modifier(plugin_instance, name, &blk)
|
||||||
|
raise "unregister_modifier can only be used in tests" if !Rails.env.test?
|
||||||
|
|
||||||
|
modifiers_for_name = @modifiers&.[](name)
|
||||||
|
raise "no #{name} modifiers found" if !modifiers_for_name
|
||||||
|
|
||||||
|
i = modifiers_for_name.find_index { |info| info == [plugin_instance, blk] }
|
||||||
|
raise "no modifier found for that plugin/block combination" if !i
|
||||||
|
|
||||||
|
modifiers_for_name.delete_at(i)
|
||||||
|
end
|
||||||
|
|
||||||
def self.apply_modifier(name, arg, *more_args)
|
def self.apply_modifier(name, arg, *more_args)
|
||||||
return arg if !@modifiers
|
return arg if !@modifiers
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user