Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-25 10:40:28 +01:00
Move to more reliable custom field metaprogramming
Dieser Commit ist enthalten in:
Ursprung
54d244c1d8
Commit
9d260635ca
3 geänderte Dateien mit 31 neuen und 27 gelöschten Zeilen
|
@ -26,7 +26,6 @@ class CustomWizard::AdminCustomFieldsController < CustomWizard::AdminController
|
||||||
end
|
end
|
||||||
|
|
||||||
if all_fields_saved
|
if all_fields_saved
|
||||||
CustomWizard::CustomField.register_fields
|
|
||||||
render json: success_json
|
render json: success_json
|
||||||
else
|
else
|
||||||
render json: error_json
|
render json: error_json
|
||||||
|
|
|
@ -80,30 +80,12 @@ class ::CustomWizard::CustomField
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.register_fields
|
def self.list_by(attr, value)
|
||||||
self.list.each do |field|
|
self.list.select do |cf|
|
||||||
klass = field.klass.classify.constantize
|
if attr == 'serializers'
|
||||||
|
cf.send(attr).include?(value)
|
||||||
klass.register_custom_field_type(field.name, field.type.to_sym)
|
|
||||||
|
|
||||||
klass.define_method(field.name) do
|
|
||||||
custom_fields[field.name]
|
|
||||||
end
|
|
||||||
|
|
||||||
if field.serializers.any?
|
|
||||||
field.serializers.each do |serializer|
|
|
||||||
serializer_klass = "#{serializer}_serializer".classify.constantize
|
|
||||||
|
|
||||||
serializer_klass.class_eval { attributes(field.name.to_sym) }
|
|
||||||
|
|
||||||
serializer_klass.define_method(field.name) do
|
|
||||||
if serializer == 'topic_view'
|
|
||||||
object.topic.send(field.name)
|
|
||||||
else
|
else
|
||||||
object.send(field.name)
|
cf.send(attr) == value
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
25
plugin.rb
25
plugin.rb
|
@ -163,7 +163,30 @@ after_initialize do
|
||||||
|
|
||||||
CustomWizard::Wizard.register_styles
|
CustomWizard::Wizard.register_styles
|
||||||
|
|
||||||
CustomWizard::CustomField.register_fields
|
CustomWizard::CustomField::CLASSES.each do |klass|
|
||||||
|
add_model_callback(klass.to_sym, :after_initialize) do
|
||||||
|
CustomWizard::CustomField.list_by('klass', klass).each do |field|
|
||||||
|
klass.classify
|
||||||
|
.constantize
|
||||||
|
.register_custom_field_type(field.name, field.type.to_sym)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
CustomWizard::CustomField::SERIALIZERS.each do |serializer_klass|
|
||||||
|
"#{serializer_klass}_serializer".classify.constantize.class_eval do
|
||||||
|
CustomWizard::CustomField.list_by('serializers', serializer_klass).each do |field|
|
||||||
|
attributes(field.name.to_sym)
|
||||||
|
class_eval %{def #{field.name}
|
||||||
|
if "#{serializer_klass}" == "topic_view"
|
||||||
|
object.topic.custom_fields["#{field.name}"]
|
||||||
|
else
|
||||||
|
object.custom_fields["#{field.name}"]
|
||||||
|
end
|
||||||
|
end}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
DiscourseEvent.trigger(:custom_wizard_ready)
|
DiscourseEvent.trigger(:custom_wizard_ready)
|
||||||
end
|
end
|
||||||
|
|
Laden …
In neuem Issue referenzieren