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

65 Zeilen
1,4 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
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
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)
2020-11-09 11:44:32 +01:00
CustomWizard::CustomField::ATTRS.each do |attr|
2020-11-10 01:56:11 +01:00
field_data[attr] = saved_field.send(attr)
2020-11-09 11:44:32 +01:00
end
2020-11-10 01:56:11 +01:00
field_id = saved_field.id
end
2021-03-11 07:30:15 +01:00
2020-11-10 01:56:11 +01:00
CustomWizard::CustomField::ATTRS.each do |attr|
field_data[attr] = field_params[attr]
2020-10-17 03:31:07 +02:00
end
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
field_errors = field.errors.any? ?
field.errors.full_messages.join("\n\n") :
I18n.t("wizard.custom_field.error.save_default", name: field.name)
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
params.required(:custom_field)
.permit(
:id,
2020-10-20 07:40:23 +02:00
:name,
:klass,
2020-10-20 07:40:23 +02:00
:type,
serializers: []
2020-11-10 01:56:11 +01:00
)
2020-10-17 03:31:07 +02:00
end
2021-03-11 07:30:15 +01:00
end