1
0
Fork 0

Add enabled? wrapper for CustomField extensions

Dieser Commit ist enthalten in:
Angus McLeod 2020-12-01 18:20:02 +11:00
Ursprung f6f77547d7
Commit 39ce7248a8
6 geänderte Dateien mit 51 neuen und 14 gelöschten Zeilen

Datei anzeigen

@ -1,5 +1,5 @@
{
"result": {
"covered_percent": 89.17
"covered_percent": 89.25
}
}

Datei anzeigen

@ -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

Datei anzeigen

@ -1,6 +1,8 @@
module CustomWizardCustomFieldSerializer
def attributes(*args)
hash = super
if cw_fields_enabled?
@cw_klass = get_cw_class
if cw_fields.any?
@ -12,12 +14,17 @@ module CustomWizardCustomFieldSerializer
end
end
end
end
hash
end
private
def cw_fields_enabled?
SiteSetting.custom_wizard_enabled && CustomWizard::CustomField.enabled?
end
def cw_fields
CustomWizard::CustomField.list_by(:serializers, @cw_klass)
end

Datei anzeigen

@ -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

Datei anzeigen

@ -173,6 +173,7 @@ after_initialize do
CustomWizard::CustomField::CLASSES.keys.each do |klass|
add_model_callback(klass, :after_initialize) do
if CustomWizard::CustomField.enabled?
CustomWizard::CustomField.list_by(:klass, klass.to_s).each do |field|
klass.to_s
.classify
@ -180,6 +181,7 @@ after_initialize do
.register_custom_field_type(field.name, field.type.to_sym)
end
end
end
klass.to_s.classify.constantize.singleton_class.prepend CustomWizardCustomFieldPreloader
end

Datei anzeigen

@ -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