From 966c7e7f07eefa066a31115193126509ee1bef5d Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 16 Nov 2017 14:42:38 -0500 Subject: [PATCH] FEATURE: Allow plugins to dynamically add seed fixture paths This is useful if your plugin wants different seed data for different locales for example. --- lib/discourse_plugin_registry.rb | 17 +++++++++++++++++ lib/plugin/instance.rb | 4 ++++ lib/tasks/db.rake | 4 ++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rb index 467e7fc42e8..e7274111490 100644 --- a/lib/discourse_plugin_registry.rb +++ b/lib/discourse_plugin_registry.rb @@ -64,6 +64,10 @@ class DiscoursePluginRegistry @html_builders ||= {} end + def seed_path_builders + @seed_path_builders ||= Set.new + end + def vendored_pretty_text @vendored_pretty_text ||= Set.new end @@ -136,6 +140,10 @@ class DiscoursePluginRegistry self.seed_data[key] = value end + def self.register_seed_path_builder(&block) + seed_path_builders << block + end + def self.register_html_builder(name, &block) html_builders[name] ||= [] html_builders[name] << block @@ -146,6 +154,14 @@ class DiscoursePluginRegistry builders.map { |b| b.call(ctx) }.join("\n").html_safe end + def self.seed_paths + result = SeedFu.fixture_paths.dup + unless Rails.env.test? && ENV['LOAD_PLUGINS'] != "1" + seed_path_builders.each { |b| result += b.call } + end + result + end + def javascripts self.class.javascripts end @@ -190,6 +206,7 @@ class DiscoursePluginRegistry asset_globs.clear html_builders.clear vendored_pretty_text.clear + seed_path_builders.clear end def self.setup(plugin_class) diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index 721441229f8..ee399c49217 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -340,6 +340,10 @@ class Plugin::Instance seed_data[key] = value end + def register_seed_path_builder(&block) + DiscoursePluginRegistry.register_seed_path_builder(&block) + end + def register_emoji(name, url) Plugin::CustomEmoji.register(name, url) end diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index f4ead49abbf..a1d74eff7f2 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -27,7 +27,7 @@ end # we need to run seed_fu every time we run rails db:migrate task 'db:migrate', [:multisite] => ['environment', 'set_locale'] do |_, args| - SeedFu.seed + SeedFu.seed(DiscoursePluginRegistry.seed_paths) Jobs::Onceoff.enqueue_all if Rails.env.test? && !args[:multisite] @@ -39,7 +39,7 @@ end task 'test:prepare' => 'environment' do I18n.locale = SiteSetting.default_locale rescue :en - SeedFu.seed + SeedFu.seed(DiscoursePluginRegistry.seed_paths) end task 'db:api_test_seed' => 'environment' do