wizard.rb 1.6 KB

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