0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-25 02:30:28 +01:00
discourse-custom-wizard/lib/custom_wizard/builder.rb

343 Zeilen
9,2 KiB
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
2017-09-23 04:34:07 +02:00
class CustomWizard::Builder
attr_accessor :wizard, :updater, :template
2017-10-13 15:02:34 +02:00
2023-01-18 19:53:36 +01:00
def initialize(wizard_id, user = nil, guest_id = nil)
@template = CustomWizard::Template.create(wizard_id)
return nil if @template.nil?
2023-01-18 19:53:36 +01:00
@wizard = CustomWizard::Wizard.new(template.data, user, guest_id)
2017-10-05 02:36:46 +02:00
end
def self.sorted_handlers
@sorted_handlers ||= []
end
def self.step_handlers
sorted_handlers.map { |h| { wizard_id: h[:wizard_id], block: h[:block] } }
end
def self.add_step_handler(priority = 0, wizard_id, &block)
sorted_handlers << { priority: priority, wizard_id: wizard_id, block: block }
@sorted_handlers.sort_by! { |h| -h[:priority] }
2017-09-23 04:34:07 +02:00
end
2021-03-11 07:30:15 +01:00
def build(build_opts = {}, params = {})
2020-04-13 14:17:22 +02:00
return nil if !SiteSetting.custom_wizard_enabled || !@wizard
return @wizard if !@wizard.can_access? && !build_opts[:force]
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
build_opts[:reset] = build_opts[:reset] || @wizard.restart_on_revisit
@template.steps.each do |step_template|
next if !check_condition(step_template)
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
@wizard.append_step(step_template["id"]) do |step|
step = check_if_permitted(step, step_template)
next if !step.permitted
save_permitted_params(step_template, params)
step = add_step_attributes(step, step_template)
step = append_step_fields(step, step_template, build_opts)
2017-09-23 04:34:07 +02:00
step.on_update do |updater|
@updater = updater
@submission = @wizard.current_submission
@submission.fields.merge!(@updater.submission)
2021-03-11 07:30:15 +01:00
@updater.validate
next if @updater.errors.any?
2017-10-05 02:36:46 +02:00
apply_step_handlers
next if @updater.errors.any?
2017-10-13 15:02:34 +02:00
run_step_actions
2021-03-11 07:30:15 +01:00
if @updater.errors.empty?
route_to = @submission.route_to
@submission.route_to = nil
@submission.save
2021-03-11 07:30:15 +01:00
@wizard.update!
@updater.result[:redirect_on_next] = route_to if route_to
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
true
else
false
2018-05-20 03:57:34 +02:00
end
end
end
end
2021-03-11 07:30:15 +01:00
@wizard.update!
CustomWizard::Submission.cleanup_incomplete_submissions(@wizard)
2018-05-20 03:57:34 +02:00
@wizard
end
2017-10-17 15:17:53 +02:00
2021-09-07 14:06:13 +02:00
def check_condition(template)
2024-10-16 13:52:03 +02:00
if template["condition"].present?
result =
CustomWizard::Mapper.new(
inputs: template["condition"],
user: @wizard.user,
data: @wizard.current_submission&.fields_and_meta,
opts: {
multiple: true,
},
).perform
2021-09-07 14:06:13 +02:00
result.any?
else
true
end
end
private
def mapper
2024-10-16 13:52:03 +02:00
CustomWizard::Mapper.new(user: @wizard.user, data: @wizard.current_submission&.fields_and_meta)
end
def append_field(step, step_template, field_template, build_opts)
params = {
2024-10-16 13:52:03 +02:00
id: field_template["id"],
type: field_template["type"],
required: field_template["required"],
}
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
%w[
label
description
image
key
validations
min_length
max_length
char_counter
tag_groups
].each { |key| params[key.to_sym] = field_template[key] if field_template[key] }
2020-10-31 08:05:50 +01:00
params[:value] = prefill_field(field_template, step_template)
2021-03-11 07:30:15 +01:00
if !build_opts[:reset] && (submission = @wizard.current_submission).present?
2024-10-16 13:52:03 +02:00
params[:value] = submission.fields[field_template["id"]] if submission.fields[
field_template["id"]
]
end
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
if field_template["type"] === "group" && params[:value].present?
2020-03-30 08:16:03 +02:00
params[:value] = params[:value].first
end
2024-10-16 13:52:03 +02:00
params[:value] = standardise_boolean(params[:value]) if field_template["type"] === "checkbox"
2024-10-16 13:52:03 +02:00
params[:file_types] = field_template["file_types"] if field_template["type"] === "upload"
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
if %w[date time date_time].include?(field_template["type"])
params[:format] = field_template["format"]
2020-07-16 05:26:56 +02:00
end
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
if %w[category tag topic].include?(field_template["type"])
params[:limit] = field_template["limit"]
end
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
if field_template["type"] === "tag"
params[:can_create_tag] = standardise_boolean(field_template["can_create_tag"])
2022-10-26 10:04:50 +02:00
end
2024-10-16 13:52:03 +02:00
params[:property] = field_template["property"] if field_template["type"] === "category"
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
params[:category] = field_template["category"] if field_template["type"] === "topic"
2024-10-16 13:52:03 +02:00
if (content_inputs = field_template["content"]).present?
content =
CustomWizard::Mapper.new(
inputs: content_inputs,
user: @wizard.user,
data: @wizard.current_submission&.fields_and_meta,
opts: {
with_type: true,
},
).perform
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
if content.present? && content[:result].present?
if content[:type] == "association"
content[:result] = content[:result].map { |item| { id: item[:key], name: item[:value] } }
2020-04-06 10:36:38 +02:00
end
2021-03-11 07:30:15 +01:00
2020-04-19 08:11:07 +02:00
params[:content] = content[:result]
2020-04-06 10:36:38 +02:00
end
2020-03-24 10:35:46 +01:00
end
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
if field_template["index"].present?
index =
CustomWizard::Mapper.new(
inputs: field_template["index"],
user: @wizard.user,
data: @wizard.current_submission&.fields_and_meta,
).perform
params[:index] = index.to_i unless index.nil?
end
2024-10-16 13:52:03 +02:00
if field_template["description"].present?
params[:description] = mapper.interpolate(
2024-10-16 13:52:03 +02:00
field_template["description"],
2023-01-18 19:53:36 +01:00
user: @wizard.user,
value: true,
wizard: true,
2024-10-16 13:52:03 +02:00
template: true,
)
end
2024-10-16 13:52:03 +02:00
if field_template["preview_template"].present?
preview_template =
mapper.interpolate(
field_template["preview_template"],
user: @wizard.user,
value: true,
wizard: true,
template: true,
)
params[:preview_template] = PrettyText.cook(preview_template)
end
2024-10-16 13:52:03 +02:00
if field_template["placeholder"].present?
2021-07-05 08:22:29 +02:00
params[:placeholder] = mapper.interpolate(
2024-10-16 13:52:03 +02:00
field_template["placeholder"],
2023-01-18 19:53:36 +01:00
user: @wizard.user,
2021-07-05 08:22:29 +02:00
value: true,
wizard: true,
2024-10-16 13:52:03 +02:00
template: true,
2021-07-05 08:22:29 +02:00
)
end
2018-07-04 05:48:03 +02:00
field = step.add_field(params)
end
2021-03-11 07:30:15 +01:00
2020-03-21 18:30:11 +01:00
def prefill_field(field_template, step_template)
2024-10-16 13:52:03 +02:00
if (prefill = field_template["prefill"]).present?
2020-03-30 08:16:03 +02:00
CustomWizard::Mapper.new(
inputs: prefill,
user: @wizard.user,
2024-10-16 13:52:03 +02:00
data: @wizard.current_submission&.fields_and_meta,
2020-04-06 10:36:38 +02:00
).perform
end
end
def check_if_permitted(step, step_template)
step.permitted = true
2024-10-16 13:52:03 +02:00
step = ensure_required_data(step, step_template) if step_template["required_data"]
2018-05-20 03:57:34 +02:00
if !step.permitted
2024-10-16 13:52:03 +02:00
if step_template["required_data_message"]
step.permitted_message = step_template["required_data_message"]
end
end
step
end
def add_step_attributes(step, step_template)
2024-10-16 13:52:03 +02:00
%w[index title banner key force_final].each do |attr|
step.send("#{attr}=", step_template[attr]) if step_template[attr]
end
2024-10-16 13:52:03 +02:00
if step_template["description"]
step.description =
mapper.interpolate(
step_template["description"],
user: @wizard.user,
value: true,
wizard: true,
template: true,
)
step.description = PrettyText.cook(step.description)
end
step
end
def append_step_fields(step, step_template, build_opts)
2024-10-16 13:52:03 +02:00
if step_template["fields"] && step_template["fields"].length
step_template["fields"].each do |field_template|
next if !check_condition(field_template)
append_field(step, step_template, field_template, build_opts)
end
2018-05-20 03:57:34 +02:00
end
step.update_field_order!
step
end
def standardise_boolean(value)
ActiveRecord::Type::Boolean.new.cast(value)
2017-09-23 04:34:07 +02:00
end
2021-03-11 07:30:15 +01:00
def save_permitted_params(step_template, params)
2024-10-16 13:52:03 +02:00
return if step_template["permitted_params"].blank?
2024-10-16 13:52:03 +02:00
permitted_params = step_template["permitted_params"]
2020-10-31 08:05:50 +01:00
permitted_data = {}
submission_key = nil
params_key = nil
submission = @wizard.current_submission
2020-10-31 08:05:50 +01:00
permitted_params.each do |pp|
2024-10-16 13:52:03 +02:00
pair = pp["pairs"].first
params_key = pair["key"].to_sym
submission_key = pair["value"].to_sym
2020-10-31 08:05:50 +01:00
if submission_key && params_key && params[params_key].present?
submission.permitted_param_keys << submission_key.to_s
submission.fields[submission_key] = params[params_key]
end
2020-10-31 08:05:50 +01:00
end
submission.save
2020-10-31 08:05:50 +01:00
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
def ensure_required_data(step, step_template)
2024-10-16 13:52:03 +02:00
step_template["required_data"].each do |required|
pairs = required["pairs"].select { |pair| pair["key"].present? && pair["value"].present? }
2021-03-11 07:30:15 +01:00
if pairs.any? && !@wizard.current_submission.present?
2020-10-31 08:05:50 +01:00
step.permitted = false
break
end
2021-03-11 07:30:15 +01:00
2024-10-16 13:52:03 +02:00
pairs.each { |pair| pair["key"] = @wizard.current_submission.fields[pair["key"]] }
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
if !mapper.validate_pairs(pairs)
step.permitted = false
break
end
end
2021-03-11 07:30:15 +01:00
2020-10-31 08:05:50 +01:00
step
end
def apply_step_handlers
CustomWizard::Builder.step_handlers.each do |handler|
2024-10-16 13:52:03 +02:00
handler[:block].call(self) if handler[:wizard_id] == @wizard.id
end
end
def run_step_actions
if @template.actions.present?
@template.actions.each do |action_template|
2024-10-16 13:52:03 +02:00
if action_template["run_after"] === updater.step.id
result =
CustomWizard::Action.new(
action: action_template,
wizard: @wizard,
submission: @submission,
).perform
@submission = result.submission if result.success?
end
end
end
end
2017-09-23 04:34:07 +02:00
end