Add enabled? wrapper for CustomField extensions
Dieser Commit ist enthalten in:
Ursprung
f6f77547d7
Commit
39ce7248a8
6 geänderte Dateien mit 51 neuen und 14 gelöschten Zeilen
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"result": {
|
"result": {
|
||||||
"covered_percent": 89.17
|
"covered_percent": 89.25
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
module CustomWizardCustomFieldPreloader
|
module CustomWizardCustomFieldPreloader
|
||||||
def preload_custom_fields(objects, fields)
|
def preload_custom_fields(objects, fields)
|
||||||
if objects.present?
|
if objects.present? && cw_fields_enabled?
|
||||||
@cw_klass = objects.first.class.name.underscore
|
@cw_klass = objects.first.class.name.underscore
|
||||||
if cw_fields.any?
|
if cw_fields.any?
|
||||||
cw_fields.each do |field|
|
cw_fields.each do |field|
|
||||||
|
@ -11,6 +11,10 @@ module CustomWizardCustomFieldPreloader
|
||||||
super(objects, fields)
|
super(objects, fields)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def cw_fields_enabled?
|
||||||
|
SiteSetting.custom_wizard_enabled && CustomWizard::CustomField.enabled?
|
||||||
|
end
|
||||||
|
|
||||||
def cw_fields
|
def cw_fields
|
||||||
CustomWizard::CustomField.list_by(:klass, @cw_klass)
|
CustomWizard::CustomField.list_by(:klass, @cw_klass)
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
module CustomWizardCustomFieldSerializer
|
module CustomWizardCustomFieldSerializer
|
||||||
def attributes(*args)
|
def attributes(*args)
|
||||||
hash = super
|
hash = super
|
||||||
|
|
||||||
|
if cw_fields_enabled?
|
||||||
@cw_klass = get_cw_class
|
@cw_klass = get_cw_class
|
||||||
|
|
||||||
if cw_fields.any?
|
if cw_fields.any?
|
||||||
|
@ -12,12 +14,17 @@ module CustomWizardCustomFieldSerializer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
hash
|
hash
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def cw_fields_enabled?
|
||||||
|
SiteSetting.custom_wizard_enabled && CustomWizard::CustomField.enabled?
|
||||||
|
end
|
||||||
|
|
||||||
def cw_fields
|
def cw_fields
|
||||||
CustomWizard::CustomField.list_by(:serializers, @cw_klass)
|
CustomWizard::CustomField.list_by(:serializers, @cw_klass)
|
||||||
end
|
end
|
||||||
|
|
|
@ -122,6 +122,7 @@ class ::CustomWizard::CustomField
|
||||||
|
|
||||||
def self.reset
|
def self.reset
|
||||||
@list = nil
|
@list = nil
|
||||||
|
@any = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.list
|
def self.list
|
||||||
|
@ -199,4 +200,16 @@ class ::CustomWizard::CustomField
|
||||||
Discourse.clear_readonly!
|
Discourse.clear_readonly!
|
||||||
Discourse.request_refresh!
|
Discourse.request_refresh!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.any?
|
||||||
|
if @any.nil?
|
||||||
|
@any = PluginStoreRow.where(plugin_name: NAMESPACE).exists?
|
||||||
|
else
|
||||||
|
@any
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.enabled?
|
||||||
|
any?
|
||||||
|
end
|
||||||
end
|
end
|
|
@ -173,6 +173,7 @@ after_initialize do
|
||||||
|
|
||||||
CustomWizard::CustomField::CLASSES.keys.each do |klass|
|
CustomWizard::CustomField::CLASSES.keys.each do |klass|
|
||||||
add_model_callback(klass, :after_initialize) do
|
add_model_callback(klass, :after_initialize) do
|
||||||
|
if CustomWizard::CustomField.enabled?
|
||||||
CustomWizard::CustomField.list_by(:klass, klass.to_s).each do |field|
|
CustomWizard::CustomField.list_by(:klass, klass.to_s).each do |field|
|
||||||
klass.to_s
|
klass.to_s
|
||||||
.classify
|
.classify
|
||||||
|
@ -180,6 +181,7 @@ after_initialize do
|
||||||
.register_custom_field_type(field.name, field.type.to_sym)
|
.register_custom_field_type(field.name, field.type.to_sym)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
klass.to_s.classify.constantize.singleton_class.prepend CustomWizardCustomFieldPreloader
|
klass.to_s.classify.constantize.singleton_class.prepend CustomWizardCustomFieldPreloader
|
||||||
end
|
end
|
||||||
|
|
|
@ -175,4 +175,15 @@ describe CustomWizard::CustomField do
|
||||||
expect(CustomWizard::CustomField.list_by(:klass, 'topic').length).to eq(1)
|
expect(CustomWizard::CustomField.list_by(:klass, 'topic').length).to eq(1)
|
||||||
end
|
end
|
||||||
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
|
end
|
Laden …
In neuem Issue referenzieren