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,14 +1,17 @@
|
||||||
module CustomWizardCustomFieldSerializer
|
module CustomWizardCustomFieldSerializer
|
||||||
def attributes(*args)
|
def attributes(*args)
|
||||||
hash = super
|
hash = super
|
||||||
@cw_klass = get_cw_class
|
|
||||||
|
|
||||||
if cw_fields.any?
|
if cw_fields_enabled?
|
||||||
cw_fields.each do |field|
|
@cw_klass = get_cw_class
|
||||||
if @cw_klass == "topic_view"
|
|
||||||
hash[field.name.to_sym] = object.topic.custom_fields["#{field.name}"]
|
if cw_fields.any?
|
||||||
else
|
cw_fields.each do |field|
|
||||||
hash[field.name.to_sym] = object.custom_fields["#{field.name}"]
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -18,6 +21,10 @@ module CustomWizardCustomFieldSerializer
|
||||||
|
|
||||||
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
|
12
plugin.rb
12
plugin.rb
|
@ -173,11 +173,13 @@ 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
|
||||||
CustomWizard::CustomField.list_by(:klass, klass.to_s).each do |field|
|
if CustomWizard::CustomField.enabled?
|
||||||
klass.to_s
|
CustomWizard::CustomField.list_by(:klass, klass.to_s).each do |field|
|
||||||
.classify
|
klass.to_s
|
||||||
.constantize
|
.classify
|
||||||
.register_custom_field_type(field.name, field.type.to_sym)
|
.constantize
|
||||||
|
.register_custom_field_type(field.name, field.type.to_sym)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
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