0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-23 09:50:28 +01:00
discourse-custom-wizard/controllers/custom_wizard/admin/custom_fields.rb

49 Zeilen
1,2 KiB
Ruby

2020-10-20 07:40:23 +02:00
class CustomWizard::AdminCustomFieldsController < CustomWizard::AdminController
2020-10-17 03:31:07 +02:00
def index
2020-10-20 07:40:23 +02:00
render_json_dump(custom_field_list)
2020-10-17 03:31:07 +02:00
end
def update
fields_to_save = []
2020-10-17 03:31:07 +02:00
custom_field_params[:custom_fields].each do |field_param|
field_id = nil
field_data = {}
2020-10-17 03:31:07 +02:00
if saved_field = CustomWizard::CustomField.find(field_param[:name])
CustomWizard::CustomField::ATTRS.each do |attr|
field_data[attr] = field_param[attr] || saved_field.send(attr)
end
field_id = saved_field.id
2020-10-17 03:31:07 +02:00
end
fields_to_save.push(CustomWizard::CustomField.new(field_id, field_data))
2020-10-17 03:31:07 +02:00
end
PluginStoreRow.transaction do
fields_to_save.each do |field|
unless field.save
raise ActiveRecord::Rollback.new,
field.errors.any? ?
field.errors.full_messages.join("\n\n") :
I18n.t("wizard.custom_field.error.save_default", name: field.name)
end
2020-10-17 03:31:07 +02:00
end
end
render json: success_json
2020-10-17 03:31:07 +02:00
end
2020-10-20 07:40:23 +02:00
private
def custom_field_params
params.permit(
custom_fields: [
:name,
:klass,
2020-10-20 07:40:23 +02:00
:type,
serializers: []
]
2020-10-17 03:31:07 +02:00
)
end
end