1
0
Fork 0

Move to more reliable custom field metaprogramming

Dieser Commit ist enthalten in:
Angus McLeod 2020-10-23 11:53:39 +11:00
Ursprung 54d244c1d8
Commit 9d260635ca
3 geänderte Dateien mit 31 neuen und 27 gelöschten Zeilen

Datei anzeigen

@ -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

Datei anzeigen

@ -80,31 +80,13 @@ 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) else
cf.send(attr) == value
klass.define_method(field.name) do
custom_fields[field.name]
end 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
end end

Datei anzeigen

@ -162,8 +162,31 @@ after_initialize do
::Wizard::Step.prepend CustomWizardStepExtension ::Wizard::Step.prepend CustomWizardStepExtension
CustomWizard::Wizard.register_styles CustomWizard::Wizard.register_styles
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.register_fields 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