From 51ef36abb4f05dff5f44aa3f07259a82f002385a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Sat, 12 Aug 2017 04:21:02 +0200 Subject: [PATCH] Add a bunch of reload-friendly class variables accessors plugin APIs --- lib/plugin/instance.rb | 55 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index 1f3cb575450..84abef45838 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -91,7 +91,9 @@ class Plugin::Instance end def whitelist_staff_user_custom_field(field) - User.register_plugin_staff_custom_field(field, self) + reloadable_patch do |plugin| + User.register_plugin_staff_custom_field(field, plugin) if plugin.enabled? + end end # Extend a class but check that the plugin is enabled @@ -140,6 +142,30 @@ class Plugin::Instance end end + def topic_view_post_custom_fields_whitelister(&block) + reloadable_patch do |plugin| + TopicView.add_post_custom_fields_whitelister(&block) if plugin.enabled? + end + end + + def add_preloaded_group_custom_field(field) + reloadable_patch do |plugin| + Group.preloaded_custom_field_names << field if plugin.enabled? + end + end + + def add_preloaded_topic_list_custom_field(field) + reloadable_patch do |plugin| + TopicList.preloaded_custom_fields << field if plugin.enabled? + end + end + + def add_permitted_post_create_param(name) + reloadable_patch do |plugin| + Post.permitted_create_params << name if plugin.enabled? + end + end + # Add validation method but check that the plugin is enabled def validate(klass, name, &block) klass = klass.to_s.classify.constantize @@ -205,20 +231,41 @@ class Plugin::Instance def notify_after_initialize color_schemes.each do |c| - ColorScheme.create_from_base(name: c[:name], colors: c[:colors]) unless ColorScheme.where(name: c[:name]).exists? + unless ColorScheme.where(name: c[:name]).exists? + ColorScheme.create_from_base(name: c[:name], colors: c[:colors]) + end end initializers.each do |callback| begin callback.call(self) rescue ActiveRecord::StatementInvalid => e - # When running db:migrate for the first time on a new database, plugin initializers might - # try to use models. Tolerate it. + # When running `db:migrate` for the first time on a new database, + # plugin initializers might try to use models. + # Tolerate it. raise e unless e.message.try(:include?, "PG::UndefinedTable") end end end + def register_topic_custom_field_type(name, type) + reloadable_patch do |plugin| + Topic.register_custom_field_type(name, type) if plugin.enabled? + end + end + + def register_post_custom_field_type(name, type) + reloadable_patch do |plugin| + Post.register_custom_field_type(name, type) if plugin.enabled? + end + end + + def register_group_custom_field_type(name, type) + reloadable_patch do |plugin| + Group.register_custom_field_type(name, type) if plugin.enabled? + end + end + def register_seedfu_fixtures(paths) paths = [paths] if !paths.kind_of?(Array) SeedFu.fixture_paths.concat(paths)