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

169 Zeilen
4 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,
2021-08-10 11:00:42 +02:00
custom_fields: custom_field_list,
subscribed: CustomWizard::Subscription.subscribed?,
subscription: CustomWizard::Subscription.type
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(errors: template.errors.full_messages)
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,
:theme_id,
permitted: mapped_params,
steps: [
:id,
:index,
2020-04-13 14:17:22 +02:00
:title,
:key,
:banner,
: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,
: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,
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: {},
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,
members_visibility_level: mapped_params
2020-04-13 14:17:22 +02:00
]
)
end
end