From f1637fc11e0acd4fb205e2fba5917010edb38c81 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Thu, 4 Jun 2015 15:56:17 -0400 Subject: [PATCH] FEATURE: plugins can register a custom admin quick start topic that will be seeded into new sites --- db/fixtures/999_topics.rb | 13 ++++++++-- lib/discourse_plugin_registry.rb | 9 +++++++ lib/plugin/instance.rb | 14 ++++++++++- .../discourse_plugin_registry_spec.rb | 24 +++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/db/fixtures/999_topics.rb b/db/fixtures/999_topics.rb index 1ab472c300b..0f813877b3c 100644 --- a/db/fixtures/999_topics.rb +++ b/db/fixtures/999_topics.rb @@ -54,6 +54,15 @@ if seed_welcome_topics post.topic.update_pinned(true) end - welcome = File.read(Rails.root + 'docs/ADMIN-QUICK-START-GUIDE.md') - PostCreator.create(Discourse.system_user, raw: welcome, title: "READ ME FIRST: Admin Quick Start Guide", skip_validations: true, category: staff ? staff.name : nil) + filename = DiscoursePluginRegistry.seed_data["admin_quick_start_filename"] + if filename.nil? || !File.exists?(filename) + filename = Rails.root + 'docs/ADMIN-QUICK-START-GUIDE.md' + end + + welcome = File.read(filename) + PostCreator.create( Discourse.system_user, + raw: welcome, + title: DiscoursePluginRegistry.seed_data["admin_quick_start_title"] || "READ ME FIRST: Admin Quick Start Guide", + skip_validations: true, + category: staff ? staff.name : nil) end diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rb index af605b27705..8875807f5ad 100644 --- a/lib/discourse_plugin_registry.rb +++ b/lib/discourse_plugin_registry.rb @@ -13,6 +13,7 @@ class DiscoursePluginRegistry attr_writer :sass_variables attr_writer :handlebars attr_writer :serialized_current_user_fields + attr_writer :seed_data attr_accessor :custom_html @@ -56,6 +57,10 @@ class DiscoursePluginRegistry def serialized_current_user_fields @serialized_current_user_fields ||= Set.new end + + def seed_data + @seed_data ||= HashWithIndifferentAccess.new({}) + end end def register_js(filename, options={}) @@ -104,6 +109,10 @@ class DiscoursePluginRegistry end end + def self.register_seed_data(key, value) + self.seed_data[key] = value + end + def javascripts self.class.javascripts end diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index 42b2e8bf960..99e978f9b98 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -17,6 +17,10 @@ class Plugin::Instance } end + def seed_data + @seed_data ||= {} + end + def self.find_all(parent_path) [].tap { |plugins| # also follows symlinks - http://stackoverflow.com/q/357754 @@ -166,7 +170,11 @@ class Plugin::Instance def register_color_scheme(name, colors) color_schemes << {name: name, colors: colors} - end + end + + def register_seed_data(key, value) + seed_data[key] = value + end def automatic_assets css = styles.join("\n") @@ -224,6 +232,10 @@ class Plugin::Instance register_assets! unless assets.blank? + seed_data.each do |key, value| + DiscoursePluginRegistry.register_seed_data(key, value) + end + # TODO: possibly amend this to a rails engine # Automatically include assets diff --git a/spec/components/discourse_plugin_registry_spec.rb b/spec/components/discourse_plugin_registry_spec.rb index 1171292676a..98c5709ac17 100644 --- a/spec/components/discourse_plugin_registry_spec.rb +++ b/spec/components/discourse_plugin_registry_spec.rb @@ -43,6 +43,14 @@ describe DiscoursePluginRegistry do end end + context '#seed_data' do + it 'defaults to an empty Set' do + registry.seed_data = nil + expect(registry.seed_data).to be_a(Hash) + expect(registry.seed_data.size).to eq(0) + end + end + context '.register_css' do before do registry_instance.register_css('hello.css') @@ -143,4 +151,20 @@ describe DiscoursePluginRegistry do end end + context '#register_seed_data' do + let(:registry) { DiscoursePluginRegistry } + + after do + registry.reset! + end + + it "registers seed data properly" do + registry.register_seed_data("admin_quick_start_title", "Banana Hosting: Quick Start Guide") + registry.register_seed_data("admin_quick_start_filename", File.expand_path("../docs/BANANA-QUICK-START.md", __FILE__)) + + expect(registry.seed_data["admin_quick_start_title"]).to eq("Banana Hosting: Quick Start Guide") + expect(registry.seed_data["admin_quick_start_filename"]).to eq(File.expand_path("../docs/BANANA-QUICK-START.md", __FILE__)) + end + end + end