wizard.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. class CustomWizard::WizardController < ::ApplicationController
  2. prepend_view_path(Rails.root.join('plugins', 'discourse-custom-wizard', 'views'))
  3. layout 'wizard'
  4. helper_method :wizard_page_title
  5. def wizard_page_title
  6. wizard = PluginStore.get('custom_wizard', params[:wizard_id].underscore)
  7. wizard ? (wizard['name'] || wizard['id']) : I18n.t('wizard.custom_title')
  8. end
  9. def index
  10. respond_to do |format|
  11. format.json do
  12. builder = CustomWizard::Builder.new(current_user, params[:wizard_id].underscore)
  13. if builder.wizard.present?
  14. wizard = builder.build
  15. render_serialized(wizard, WizardSerializer)
  16. else
  17. render json: { error: I18n.t('wizard.none') }
  18. end
  19. end
  20. format.html {}
  21. end
  22. end
  23. ## clean up if user skips wizard
  24. def skip
  25. wizard_id = params[:wizard_id]
  26. wizard = PluginStore.get('custom_wizard', wizard_id.underscore)
  27. if wizard['required']
  28. return render json: { error: I18n.t('wizard.no_skip') }
  29. end
  30. user = current_user
  31. result = success_json
  32. submission = Array.wrap(PluginStore.get("#{wizard_id}_submissions", user.id)).last
  33. if submission && submission['redirect_to']
  34. result.merge!(redirect_to: submission['redirect_to'])
  35. end
  36. if submission && !wizard['save_submissions']
  37. PluginStore.remove("#{wizard['id']}_submissions", user.id)
  38. end
  39. if user.custom_fields['redirect_to_wizard'] === wizard_id
  40. user.custom_fields.delete('redirect_to_wizard')
  41. user.save_custom_fields(true)
  42. end
  43. render json: result
  44. end
  45. end