From 888d56774a6dc0cea7d26bc5cac1b6bb6d5b4f20 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 4 Dec 2019 12:26:23 -0500 Subject: [PATCH] DEV: HTML Builders should respect if a plugin is enabled or not (#8454) Previously they would return the HTML regardless of whether the plugin was enabled or not. --- lib/plugin/instance.rb | 5 ++++- spec/components/plugin/instance_spec.rb | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index 19024de7a12..9256916f9ee 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -418,7 +418,10 @@ class Plugin::Instance end def register_html_builder(name, &block) - DiscoursePluginRegistry.register_html_builder(name, &block) + plugin = self + DiscoursePluginRegistry.register_html_builder(name) do |*args| + block.call(*args) if plugin.enabled? + end end def register_asset(file, opts = nil) diff --git a/spec/components/plugin/instance_spec.rb b/spec/components/plugin/instance_spec.rb index 7286f5cc0a9..50064c3ad84 100644 --- a/spec/components/plugin/instance_spec.rb +++ b/spec/components/plugin/instance_spec.rb @@ -32,7 +32,10 @@ describe Plugin::Instance do context "with a plugin that extends things" do - class Trout; end + class Trout + attr_accessor :data + end + class TroutSerializer < ApplicationSerializer attribute :name @@ -90,7 +93,6 @@ describe Plugin::Instance do end it "checks enabled/disabled functionality for extensions" do - # with an enabled plugin @plugin.enabled = true expect(@trout.status?).to eq("evil") @@ -114,6 +116,17 @@ describe Plugin::Instance do expect(@child_serializer.include_scales?).to eq(false) expect(@child_serializer.name).to eq("a trout jr") end + + it "only returns HTML if enabled" do + ctx = Trout.new + ctx.data = "hello" + + @plugin.register_html_builder('test:html') { |c| "
#{c.data}
" } + @plugin.enabled = false + expect(DiscoursePluginRegistry.build_html('test:html', ctx)).to eq("") + @plugin.enabled = true + expect(DiscoursePluginRegistry.build_html('test:html', ctx)).to eq("
hello
") + end end end