wizard.rb 1.7 KB

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