Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-09 11:52:54 +01:00
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": {
|
||||
"covered_percent": 89.17
|
||||
"covered_percent": 89.25
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
12
plugin.rb
12
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
|
||||
|
||||
|
|
|
@ -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
|
Laden …
In neuem Issue referenzieren