0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-22 17:30:29 +01:00
discourse-custom-wizard/controllers/custom_wizard/wizard.rb

73 Zeilen
1,8 KiB
Ruby

2017-09-25 16:47:40 +02:00
class CustomWizard::WizardController < ::ApplicationController
2017-10-17 09:18:53 +02:00
prepend_view_path(Rails.root.join('plugins', 'discourse-custom-wizard', 'views'))
layout 'wizard'
2020-11-03 01:24:20 +01:00
before_action :ensure_plugin_enabled
2017-10-17 09:18:53 +02:00
helper_method :wizard_page_title
2018-08-20 01:08:28 +02:00
helper_method :theme_ids
def wizard
2020-04-13 14:17:22 +02:00
CustomWizard::Wizard.create(params[:wizard_id].underscore, current_user)
end
2017-10-17 09:18:53 +02:00
def wizard_page_title
wizard ? (wizard.name || wizard.id) : I18n.t('wizard.custom_title')
2017-09-25 16:47:40 +02:00
end
2018-08-20 01:08:28 +02:00
def theme_ids
wizard ? [wizard.theme_id] : nil
end
2017-09-25 16:47:40 +02:00
def index
2017-09-23 04:34:07 +02:00
respond_to do |format|
format.json do
2020-04-13 14:17:22 +02:00
builder = CustomWizard::Builder.new(params[:wizard_id].underscore, current_user)
2017-11-01 05:21:14 +01:00
if builder.wizard.present?
2020-04-13 14:17:22 +02:00
builder_opts = {}
2020-10-31 08:05:50 +01:00
builder_opts[:reset] = params[:reset]
2020-04-13 14:17:22 +02:00
built_wizard = builder.build(builder_opts, params)
render_serialized(built_wizard, ::CustomWizard::WizardSerializer, root: false)
2017-10-22 05:37:58 +02:00
else
render json: { error: I18n.t('wizard.none') }
end
2017-09-23 04:34:07 +02:00
end
format.html {}
end
end
2017-11-01 05:21:14 +01:00
def skip
params.require(:wizard_id)
2020-11-03 01:24:20 +01:00
if wizard.required && !wizard.completed? && wizard.permitted?
2017-11-01 05:21:14 +01:00
return render json: { error: I18n.t('wizard.no_skip') }
end
result = success_json
2020-04-13 14:17:22 +02:00
user = current_user
2019-06-19 07:23:10 +02:00
if user
2020-11-03 01:24:20 +01:00
submission = wizard.current_submission
2019-06-19 07:23:10 +02:00
if submission && submission['redirect_to']
result.merge!(redirect_to: submission['redirect_to'])
end
2017-11-01 05:21:14 +01:00
2020-04-13 14:17:22 +02:00
if user.custom_fields['redirect_to_wizard'] === wizard.id
2019-06-19 07:23:10 +02:00
user.custom_fields.delete('redirect_to_wizard')
user.save_custom_fields(true)
end
2017-11-01 05:21:14 +01:00
end
render json: result
end
2020-11-03 01:24:20 +01:00
private
def ensure_plugin_enabled
unless SiteSetting.custom_wizard_enabled
redirect_to path("/")
end
end
2017-09-23 04:34:07 +02:00
end