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
if all_fields_saved
CustomWizard::CustomField.register_fields
render json: success_json
else
render json: error_json

Datei anzeigen

@ -80,30 +80,12 @@ class ::CustomWizard::CustomField
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)
def self.list_by(attr, value)
self.list.select do |cf|
if attr == 'serializers'
cf.send(attr).include?(value)
else
object.send(field.name)
end
end
end
cf.send(attr) == value
end
end
end

Datei anzeigen

@ -163,7 +163,30 @@ after_initialize do
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)
end