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

174 Zeilen
4,1 KiB
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
2020-04-13 14:17:22 +02:00
class CustomWizard::AdminWizardController < CustomWizard::AdminController
before_action :find_wizard, only: [:show, :remove]
2021-03-11 07:30:15 +01:00
2020-04-13 14:17:22 +02:00
def index
render_json_dump(
wizard_list: ActiveModel::ArraySerializer.new(
2020-11-03 01:24:20 +01:00
CustomWizard::Wizard.list(current_user),
2020-04-13 14:17:22 +02:00
each_serializer: CustomWizard::BasicWizardSerializer
),
2020-10-20 07:40:23 +02:00
field_types: CustomWizard::Field.types,
2021-01-27 06:08:26 +01:00
realtime_validations: CustomWizard::RealtimeValidation.types,
2020-10-20 07:40:23 +02:00
custom_fields: custom_field_list
2020-04-13 14:17:22 +02:00
)
end
2021-03-11 07:30:15 +01:00
2020-04-13 14:17:22 +02:00
def show
params.require(:wizard_id)
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
if data = CustomWizard::Template.find(params[:wizard_id].underscore)
2020-04-13 14:17:22 +02:00
render json: data.as_json
else
render json: { none: true }
end
end
2021-03-11 07:30:15 +01:00
2020-04-13 14:17:22 +02:00
def remove
2020-10-31 08:05:50 +01:00
if CustomWizard::Template.remove(@wizard.id)
render json: success_json
else
render json: failed_json
end
2020-04-13 14:17:22 +02:00
end
def save
2020-11-03 01:24:20 +01:00
template = CustomWizard::Template.new(save_wizard_params.to_h)
wizard_id = template.save(create: params[:create])
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
if template.errors.any?
render json: failed_json.merge(backend_validation_error: template.errors.full_messages.join("\n\n"))
2021-03-11 07:30:15 +01:00
else
2020-11-03 01:24:20 +01:00
render json: success_json.merge(wizard_id: wizard_id)
2020-04-13 14:17:22 +02:00
end
end
2021-03-11 07:30:15 +01:00
2020-04-13 14:17:22 +02:00
private
2021-03-11 07:30:15 +01:00
2020-04-13 14:17:22 +02:00
def mapped_params
[
:type,
:connector,
:output,
:output_type,
:output_connector,
pairs: [
:index,
:key,
:key_type,
:value,
:value_type,
:connector,
value: [],
key: [],
],
output: [],
]
end
2021-01-30 18:46:04 +01:00
2020-04-13 14:17:22 +02:00
def save_wizard_params
params.require(:wizard).permit(
:id,
:name,
:background,
:save_submissions,
:multiple_submissions,
:after_signup,
:after_time,
:after_time_scheduled,
:required,
:prompt_completion,
:restart_on_revisit,
:resume_on_revisit,
2020-04-13 14:17:22 +02:00
:theme_id,
permitted: mapped_params,
steps: [
:id,
:index,
2020-04-13 14:17:22 +02:00
:title,
:key,
:banner,
2022-08-02 13:39:56 +02:00
:banner_upload_id,
2020-04-13 14:17:22 +02:00
:raw_description,
:required_data_message,
:force_final,
2020-04-13 14:17:22 +02:00
required_data: mapped_params,
permitted_params: mapped_params,
condition: mapped_params,
2020-04-13 14:17:22 +02:00
fields: [
:id,
:index,
2020-04-13 14:17:22 +02:00
:label,
:image,
2022-08-02 13:39:56 +02:00
:image_upload_id,
2020-04-13 14:17:22 +02:00
:description,
:required,
:key,
:type,
:min_length,
:max_length,
:char_counter,
2020-04-13 14:17:22 +02:00
:file_types,
2020-07-16 05:26:56 +02:00
:format,
2020-04-13 14:17:22 +02:00
:limit,
:property,
:preview_template,
:placeholder,
2022-10-26 10:04:50 +02:00
:can_create_tag,
2020-04-13 14:17:22 +02:00
prefill: mapped_params,
2021-01-30 18:46:04 +01:00
content: mapped_params,
condition: mapped_params,
index: mapped_params,
2021-01-30 18:46:04 +01:00
validations: {},
tag_groups: [],
2020-04-13 14:17:22 +02:00
]
],
actions: [
:id,
:run_after,
:type,
:code,
:skip_redirect,
:suppress_notifications,
2020-04-14 01:39:21 +02:00
:post,
2020-04-15 03:23:41 +02:00
:post_builder,
2020-04-14 01:39:21 +02:00
:post_template,
:notification_level,
2020-05-30 21:26:14 +02:00
:api,
:api_endpoint,
:api_body,
2020-07-20 05:06:36 +02:00
:wizard_user,
2020-04-13 14:17:22 +02:00
title: mapped_params,
category: mapped_params,
tags: mapped_params,
custom_fields: mapped_params,
2020-07-20 06:26:11 +02:00
visible: mapped_params,
2020-04-13 14:17:22 +02:00
required: mapped_params,
recipient: mapped_params,
categories: mapped_params,
mute_remainder: mapped_params,
2020-04-13 14:17:22 +02:00
profile_updates: mapped_params,
2020-04-29 03:47:08 +02:00
group: mapped_params,
url: mapped_params,
name: mapped_params,
slug: mapped_params,
color: mapped_params,
text_color: mapped_params,
parent_category_id: mapped_params,
2020-07-16 07:25:06 +02:00
permissions: mapped_params,
full_name: mapped_params,
bio_raw: mapped_params,
2020-07-16 09:50:09 +02:00
usernames: mapped_params,
owner_usernames: mapped_params,
2020-07-16 07:25:06 +02:00
grant_trust_level: mapped_params,
mentionable_level: mapped_params,
messageable_level: mapped_params,
visibility_level: mapped_params,
2022-09-19 12:09:34 +02:00
members_visibility_level: mapped_params,
2022-10-10 14:04:55 +02:00
add_event: mapped_params,
add_location: mapped_params
2020-04-13 14:17:22 +02:00
]
)
end
end