From 39ce7248a8a70c1a2ef7592eb9413ca25615a30e Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Tue, 1 Dec 2020 18:20:02 +1100 Subject: [PATCH] Add enabled? wrapper for CustomField extensions --- coverage/.last_run.json | 2 +- extensions/custom_field/preloader.rb | 6 +++++- extensions/custom_field/serializer.rb | 21 ++++++++++++------- lib/custom_wizard/custom_field.rb | 13 ++++++++++++ plugin.rb | 12 ++++++----- .../custom_wizard/custom_field_spec.rb | 11 ++++++++++ 6 files changed, 51 insertions(+), 14 deletions(-) diff --git a/coverage/.last_run.json b/coverage/.last_run.json index 95ea134b..e9894915 100644 --- a/coverage/.last_run.json +++ b/coverage/.last_run.json @@ -1,5 +1,5 @@ { "result": { - "covered_percent": 89.17 + "covered_percent": 89.25 } } diff --git a/extensions/custom_field/preloader.rb b/extensions/custom_field/preloader.rb index eae22365..6478199f 100644 --- a/extensions/custom_field/preloader.rb +++ b/extensions/custom_field/preloader.rb @@ -1,6 +1,6 @@ module CustomWizardCustomFieldPreloader def preload_custom_fields(objects, fields) - if objects.present? + if objects.present? && cw_fields_enabled? @cw_klass = objects.first.class.name.underscore if cw_fields.any? cw_fields.each do |field| @@ -11,6 +11,10 @@ module CustomWizardCustomFieldPreloader super(objects, fields) end + def cw_fields_enabled? + SiteSetting.custom_wizard_enabled && CustomWizard::CustomField.enabled? + end + def cw_fields CustomWizard::CustomField.list_by(:klass, @cw_klass) end diff --git a/extensions/custom_field/serializer.rb b/extensions/custom_field/serializer.rb index 994ba7de..d7f3b10c 100644 --- a/extensions/custom_field/serializer.rb +++ b/extensions/custom_field/serializer.rb @@ -1,14 +1,17 @@ module CustomWizardCustomFieldSerializer def attributes(*args) hash = super - @cw_klass = get_cw_class - if cw_fields.any? - cw_fields.each do |field| - if @cw_klass == "topic_view" - hash[field.name.to_sym] = object.topic.custom_fields["#{field.name}"] - else - hash[field.name.to_sym] = object.custom_fields["#{field.name}"] + if cw_fields_enabled? + @cw_klass = get_cw_class + + if cw_fields.any? + cw_fields.each do |field| + if @cw_klass == "topic_view" + hash[field.name.to_sym] = object.topic.custom_fields["#{field.name}"] + else + hash[field.name.to_sym] = object.custom_fields["#{field.name}"] + end end end end @@ -17,6 +20,10 @@ module CustomWizardCustomFieldSerializer end private + + def cw_fields_enabled? + SiteSetting.custom_wizard_enabled && CustomWizard::CustomField.enabled? + end def cw_fields CustomWizard::CustomField.list_by(:serializers, @cw_klass) diff --git a/lib/custom_wizard/custom_field.rb b/lib/custom_wizard/custom_field.rb index dd8edc5d..0fd82f1e 100644 --- a/lib/custom_wizard/custom_field.rb +++ b/lib/custom_wizard/custom_field.rb @@ -122,6 +122,7 @@ class ::CustomWizard::CustomField def self.reset @list = nil + @any = nil end def self.list @@ -199,4 +200,16 @@ class ::CustomWizard::CustomField Discourse.clear_readonly! Discourse.request_refresh! end + + def self.any? + if @any.nil? + @any = PluginStoreRow.where(plugin_name: NAMESPACE).exists? + else + @any + end + end + + def self.enabled? + any? + end end \ No newline at end of file diff --git a/plugin.rb b/plugin.rb index 0432b6ba..f7220e64 100644 --- a/plugin.rb +++ b/plugin.rb @@ -173,11 +173,13 @@ after_initialize do CustomWizard::CustomField::CLASSES.keys.each do |klass| add_model_callback(klass, :after_initialize) do - CustomWizard::CustomField.list_by(:klass, klass.to_s).each do |field| - klass.to_s - .classify - .constantize - .register_custom_field_type(field.name, field.type.to_sym) + if CustomWizard::CustomField.enabled? + CustomWizard::CustomField.list_by(:klass, klass.to_s).each do |field| + klass.to_s + .classify + .constantize + .register_custom_field_type(field.name, field.type.to_sym) + end end end diff --git a/spec/components/custom_wizard/custom_field_spec.rb b/spec/components/custom_wizard/custom_field_spec.rb index 944e6c74..cb03bd2a 100644 --- a/spec/components/custom_wizard/custom_field_spec.rb +++ b/spec/components/custom_wizard/custom_field_spec.rb @@ -175,4 +175,15 @@ describe CustomWizard::CustomField do expect(CustomWizard::CustomField.list_by(:klass, 'topic').length).to eq(1) end end + + it "is enabled if there are custom fields" do + custom_field_json['custom_fields'].each do |field_json| + CustomWizard::CustomField.new(nil, field_json).save + end + expect(CustomWizard::CustomField.enabled?).to eq(true) + end + + it "is not enabled if there are no custom fields" do + expect(CustomWizard::CustomField.enabled?).to eq(false) + end end \ No newline at end of file