0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-10-18 20:02:38 +02:00
discourse-custom-wizard/app/controllers/custom_wizard/admin/custom_fields.rb

61 Zeilen
1,5 KiB
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
2020-10-20 07:40:23 +02:00
class CustomWizard::AdminCustomFieldsController < CustomWizard::AdminController
2024-10-16 14:18:23 +02:00
requires_plugin "discourse-custom-wizard"
2020-10-17 03:31:07 +02:00
def index
2024-10-16 13:52:03 +02:00
render_json_dump(custom_fields: custom_field_list)
2020-10-17 03:31:07 +02:00
end
2021-03-11 07:30:15 +01:00
def update
2020-11-10 01:56:11 +01:00
errors = []
field_id = nil
field_data = {}
2021-03-11 07:30:15 +01:00
2020-11-10 01:56:11 +01:00
if saved_field = CustomWizard::CustomField.find(field_params[:id].to_i)
2024-10-16 13:52:03 +02:00
CustomWizard::CustomField::ATTRS.each { |attr| field_data[attr] = saved_field.send(attr) }
2020-11-10 01:56:11 +01:00
field_id = saved_field.id
end
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
CustomWizard::CustomField::ATTRS.each { |attr| field_data[attr] = field_params[attr] }
2021-03-11 07:30:15 +01:00
2020-11-10 01:56:11 +01:00
field = CustomWizard::CustomField.new(field_id, field_data)
2021-03-11 07:30:15 +01:00
PluginStoreRow.transaction do
2020-11-10 01:56:11 +01:00
unless field.save
2024-10-16 13:52:03 +02:00
field_errors =
(
if field.errors.any?
field.errors.full_messages.join("\n\n")
else
I18n.t("wizard.custom_field.error.save_default", name: field.name)
end
)
2020-11-10 01:56:11 +01:00
errors << field_errors
raise ActiveRecord::Rollback.new
2020-10-17 03:31:07 +02:00
end
end
2021-03-11 07:30:15 +01:00
2020-11-10 01:56:11 +01:00
if errors.any?
render json: failed_json.merge(messages: errors)
else
render json: success_json
end
2020-10-17 03:31:07 +02:00
end
2021-03-11 07:30:15 +01:00
2020-11-09 11:44:32 +01:00
def destroy
params.require(:name)
2021-03-11 07:30:15 +01:00
2020-11-09 11:44:32 +01:00
if CustomWizard::CustomField.destroy(params[:name])
render json: success_json
else
render json: failed_json
end
end
2021-03-11 07:30:15 +01:00
2020-10-20 07:40:23 +02:00
private
2021-03-11 07:30:15 +01:00
2020-11-10 01:56:11 +01:00
def field_params
2024-10-16 13:52:03 +02:00
params.required(:custom_field).permit(:id, :name, :klass, :type, serializers: [])
2020-10-17 03:31:07 +02:00
end
2021-03-11 07:30:15 +01:00
end