0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-24 10:20:28 +01:00
discourse-custom-wizard/lib/custom_wizard/extensions/custom_field/serializer.rb

42 Zeilen
945 B
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
module CustomWizardCustomFieldSerializer
def attributes(*args)
hash = super
2021-03-11 07:30:15 +01:00
if cw_fields_enabled?
@cw_klass = get_cw_class
2021-03-11 07:30:15 +01:00
if cw_fields.any?
cw_fields.each do |field|
if @cw_klass == "topic_view"
hash[field[:name].to_sym] = object.topic.custom_fields["#{field[:name]}"]
else
hash[field[:name].to_sym] = object.custom_fields["#{field[:name]}"]
end
end
end
end
2021-03-11 07:30:15 +01:00
hash
end
2021-03-11 07:30:15 +01:00
private
2021-03-11 07:30:15 +01:00
def cw_fields_enabled?
SiteSetting.custom_wizard_enabled && CustomWizard::CustomField.enabled?
end
def cw_fields
CustomWizard::CustomField.list_by(:serializers, @cw_klass)
end
2021-03-11 07:30:15 +01:00
def get_cw_class
2024-10-16 13:52:03 +02:00
self
.class
.ancestors
.map { |klass| klass.to_s.underscore.gsub("_serializer", "") }
.select { |klass| CustomWizard::CustomField.serializers.include?(klass) }
.first
end
2021-03-11 07:30:15 +01:00
end