Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-09 11:52:54 +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
|
||||
|
||||
if all_fields_saved
|
||||
CustomWizard::CustomField.register_fields
|
||||
render json: success_json
|
||||
else
|
||||
render json: error_json
|
||||
|
|
|
@ -80,31 +80,13 @@ 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]
|
||||
def self.list_by(attr, value)
|
||||
self.list.select do |cf|
|
||||
if attr == 'serializers'
|
||||
cf.send(attr).include?(value)
|
||||
else
|
||||
cf.send(attr) == value
|
||||
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
|
27
plugin.rb
27
plugin.rb
|
@ -162,8 +162,31 @@ after_initialize do
|
|||
::Wizard::Step.prepend CustomWizardStepExtension
|
||||
|
||||
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)
|
||||
end
|
||||
|
|
Laden …
In neuem Issue referenzieren