wizard.rb 1.8 KB

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