1
0
Fork 0

Make custom field metaprogramming dynamic

Dieser Commit ist enthalten in:
Angus McLeod 2020-10-20 20:02:13 +11:00
Ursprung 4e094edc7b
Commit 54d244c1d8
3 geänderte Dateien mit 30 neuen und 20 gelöschten Zeilen

Datei anzeigen

@ -26,6 +26,7 @@ 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

Datei anzeigen

@ -79,4 +79,32 @@ class ::CustomWizard::CustomField
self.new(data) self.new(data)
end end
end end
def self.register_fields
self.list.each do |field|
klass = field.klass.classify.constantize
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
object.send(field.name)
end
end
end
end
end
end
end end

Datei anzeigen

@ -163,26 +163,7 @@ after_initialize do
CustomWizard::Wizard.register_styles CustomWizard::Wizard.register_styles
CustomWizard::CustomField.list.each do |field| CustomWizard::CustomField.register_fields
self.send("register_#{field.klass}_custom_field_type", field.name, field.type.to_sym)
add_to_class(field.klass.to_sym, field.name.to_sym) do
custom_fields[field.name]
end
if field.serializers.any?
field.serializers.each do |klass|
klass = klass.to_sym
add_to_serializer(klass, field.name.to_sym) do
if klass == :topic_view
object.topic.send(field.name)
else
object.send(field.name)
end
end
end
end
end
DiscourseEvent.trigger(:custom_wizard_ready) DiscourseEvent.trigger(:custom_wizard_ready)
end end