plugin.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # name: discourse-custom-wizard
  2. # about: Create custom wizards
  3. # version: 0.1
  4. # authors: Angus McLeod
  5. # url: https://github.com/angusmcleod/discourse-custom-wizard
  6. register_asset 'stylesheets/wizard_custom_admin.scss'
  7. register_asset 'lib/jquery.timepicker.min.js'
  8. register_asset 'lib/jquery.timepicker.scss'
  9. config = Rails.application.config
  10. config.assets.paths << Rails.root.join('plugins', 'discourse-custom-wizard', 'assets', 'javascripts')
  11. config.assets.paths << Rails.root.join('plugins', 'discourse-custom-wizard', 'assets', 'stylesheets', 'wizard')
  12. if Rails.env.production?
  13. config.assets.precompile += %w{
  14. wizard-custom-lib.js
  15. wizard-custom.js
  16. wizard-plugin.js
  17. stylesheets/wizard/wizard_custom.scss
  18. stylesheets/wizard/wizard_composer.scss
  19. stylesheets/wizard/wizard_variables.scss
  20. stylesheets/wizard/wizard_custom_mobile.scss
  21. }
  22. end
  23. after_initialize do
  24. UserHistory.actions[:custom_wizard_step] = 1000
  25. require_dependency 'application_controller'
  26. module ::CustomWizard
  27. class Engine < ::Rails::Engine
  28. engine_name 'custom_wizard'
  29. isolate_namespace CustomWizard
  30. end
  31. end
  32. CustomWizard::Engine.routes.draw do
  33. get ':wizard_id' => 'wizard#index'
  34. put ':wizard_id/skip' => 'wizard#skip'
  35. get ':wizard_id/steps' => 'wizard#index'
  36. get ':wizard_id/steps/:step_id' => 'wizard#index'
  37. put ':wizard_id/steps/:step_id' => 'steps#update'
  38. end
  39. require_dependency 'admin_constraint'
  40. Discourse::Application.routes.append do
  41. mount ::CustomWizard::Engine, at: 'w'
  42. scope module: 'custom_wizard', constraints: AdminConstraint.new do
  43. get 'admin/wizards' => 'admin#index'
  44. get 'admin/wizards/field-types' => 'admin#field_types'
  45. get 'admin/wizards/custom' => 'admin#index'
  46. get 'admin/wizards/custom/new' => 'admin#index'
  47. get 'admin/wizards/custom/all' => 'admin#custom_wizards'
  48. get 'admin/wizards/custom/:wizard_id' => 'admin#find_wizard'
  49. put 'admin/wizards/custom/save' => 'admin#save'
  50. delete 'admin/wizards/custom/remove' => 'admin#remove'
  51. get 'admin/wizards/submissions' => 'admin#index'
  52. get 'admin/wizards/submissions/:wizard_id' => 'admin#submissions'
  53. end
  54. end
  55. load File.expand_path('../jobs/clear_after_time_wizard.rb', __FILE__)
  56. load File.expand_path('../jobs/set_after_time_wizard.rb', __FILE__)
  57. load File.expand_path('../lib/builder.rb', __FILE__)
  58. load File.expand_path('../lib/field.rb', __FILE__)
  59. load File.expand_path('../lib/step_updater.rb', __FILE__)
  60. load File.expand_path('../lib/template.rb', __FILE__)
  61. load File.expand_path('../lib/wizard.rb', __FILE__)
  62. load File.expand_path('../lib/wizard_edits.rb', __FILE__)
  63. load File.expand_path('../controllers/wizard.rb', __FILE__)
  64. load File.expand_path('../controllers/steps.rb', __FILE__)
  65. load File.expand_path('../controllers/admin.rb', __FILE__)
  66. ::UsersController.class_eval do
  67. def wizard_path
  68. if custom_wizard_redirect = current_user.custom_fields['redirect_to_wizard']
  69. "#{Discourse.base_url}/w/#{custom_wizard_redirect.dasherize}"
  70. else
  71. "#{Discourse.base_url}/wizard"
  72. end
  73. end
  74. end
  75. module InvitesControllerCustomWizard
  76. def path(url)
  77. if Wizard.user_requires_completion?(@user)
  78. wizard_id = @user.custom_fields['custom_wizard_redirect']
  79. if wizard_id && url != '/'
  80. CustomWizard::Wizard.set_submission_redirect(@user, wizard_id, url)
  81. url = "/w/#{wizard_id.dasherize}"
  82. end
  83. end
  84. super(url)
  85. end
  86. private def post_process_invite(user)
  87. super(user)
  88. @user = user
  89. end
  90. end
  91. require_dependency 'invites_controller'
  92. class ::InvitesController
  93. prepend InvitesControllerCustomWizard
  94. end
  95. class ::ApplicationController
  96. before_action :redirect_to_wizard_if_required, if: :current_user
  97. def redirect_to_wizard_if_required
  98. wizard_id = current_user.custom_fields['redirect_to_wizard']
  99. @excluded_routes ||= SiteSetting.wizard_redirect_exclude_paths.split('|') + ['/w/']
  100. url = request.referer || request.original_url
  101. if request.format === 'text/html' && !@excluded_routes.any? { |str| /#{str}/ =~ url } && wizard_id
  102. if request.referer !~ /\/w\// && request.referer !~ /\/invites\//
  103. CustomWizard::Wizard.set_submission_redirect(current_user, wizard_id, request.referer)
  104. end
  105. redirect_to "/w/#{wizard_id.dasherize}"
  106. end
  107. end
  108. end
  109. add_to_serializer(:current_user, :redirect_to_wizard) { object.custom_fields['redirect_to_wizard'] }
  110. ## TODO limit this to the first admin
  111. SiteSerializer.class_eval do
  112. attributes :complete_custom_wizard
  113. def include_wizard_required?
  114. scope.is_admin? && Wizard.new(scope.user).requires_completion?
  115. end
  116. def complete_custom_wizard
  117. if scope.user && requires_completion = CustomWizard::Wizard.prompt_completion(scope.user)
  118. requires_completion.map { |w| { name: w[:name], url: "/w/#{w[:id]}" } }
  119. end
  120. end
  121. def include_complete_custom_wizard?
  122. complete_custom_wizard.present?
  123. end
  124. end
  125. DiscourseEvent.on(:user_approved) do |user|
  126. if wizard_id = CustomWizard::Wizard.after_signup
  127. CustomWizard::Wizard.set_wizard_redirect(user, wizard_id)
  128. end
  129. end
  130. DiscourseEvent.trigger(:custom_wizard_ready)
  131. end