plugin.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_custom_mobile.scss
  19. }
  20. end
  21. after_initialize do
  22. UserHistory.actions[:custom_wizard_step] = 1000
  23. require_dependency 'application_controller'
  24. module ::CustomWizard
  25. class Engine < ::Rails::Engine
  26. engine_name 'custom_wizard'
  27. isolate_namespace CustomWizard
  28. end
  29. end
  30. CustomWizard::Engine.routes.draw do
  31. get ':wizard_id' => 'wizard#index'
  32. put ':wizard_id/skip' => 'wizard#skip'
  33. get ':wizard_id/steps' => 'wizard#index'
  34. get ':wizard_id/steps/:step_id' => 'wizard#index'
  35. put ':wizard_id/steps/:step_id' => 'steps#update'
  36. end
  37. require_dependency 'admin_constraint'
  38. Discourse::Application.routes.append do
  39. mount ::CustomWizard::Engine, at: 'w'
  40. scope module: 'custom_wizard', constraints: AdminConstraint.new do
  41. get 'admin/wizards' => 'admin#index'
  42. get 'admin/wizards/field-types' => 'admin#field_types'
  43. get 'admin/wizards/custom' => 'admin#index'
  44. get 'admin/wizards/custom/new' => 'admin#index'
  45. get 'admin/wizards/custom/all' => 'admin#custom_wizards'
  46. get 'admin/wizards/custom/:wizard_id' => 'admin#find_wizard'
  47. put 'admin/wizards/custom/save' => 'admin#save'
  48. delete 'admin/wizards/custom/remove' => 'admin#remove'
  49. get 'admin/wizards/submissions' => 'admin#index'
  50. get 'admin/wizards/submissions/:wizard_id' => 'admin#submissions'
  51. end
  52. end
  53. load File.expand_path('../jobs/clear_after_time_wizard.rb', __FILE__)
  54. load File.expand_path('../jobs/set_after_time_wizard.rb', __FILE__)
  55. load File.expand_path('../lib/builder.rb', __FILE__)
  56. load File.expand_path('../lib/field.rb', __FILE__)
  57. load File.expand_path('../lib/step_updater.rb', __FILE__)
  58. load File.expand_path('../lib/template.rb', __FILE__)
  59. load File.expand_path('../lib/wizard.rb', __FILE__)
  60. load File.expand_path('../lib/wizard_edits.rb', __FILE__)
  61. load File.expand_path('../controllers/wizard.rb', __FILE__)
  62. load File.expand_path('../controllers/steps.rb', __FILE__)
  63. load File.expand_path('../controllers/admin.rb', __FILE__)
  64. ::UsersController.class_eval do
  65. def wizard_path
  66. if custom_wizard_redirect = $redis.get('custom_wizard_redirect')
  67. "#{Discourse.base_url}/w/#{custom_wizard_redirect}"
  68. else
  69. "#{Discourse.base_url}/wizard"
  70. end
  71. end
  72. end
  73. module InvitesControllerCustomWizard
  74. def path(url)
  75. if Wizard.user_requires_completion?(@user)
  76. wizard_path = $redis.get('custom_wizard_redirect')
  77. unless url === '/'
  78. PluginStore.set("#{wizard_path.underscore}_submissions", @user.id, [{ redirect_to: url }])
  79. end
  80. url = "/w/#{wizard_path}"
  81. end
  82. super(url)
  83. end
  84. private def post_process_invite(user)
  85. super(user)
  86. @user = user
  87. end
  88. end
  89. require_dependency 'invites_controller'
  90. class ::InvitesController
  91. prepend InvitesControllerCustomWizard
  92. end
  93. class ::ApplicationController
  94. before_action :redirect_to_wizard_if_required, if: :current_user
  95. def redirect_to_wizard_if_required
  96. @wizard_id ||= current_user.custom_fields['redirect_to_wizard']
  97. if @wizard_id && request.referer !~ /w/ && request.referer !~ /admin/
  98. redirect_to "/w/#{@wizard_id}"
  99. end
  100. end
  101. end
  102. add_to_serializer(:current_user, :redirect_to_wizard) { object.custom_fields['redirect_to_wizard'] }
  103. ## TODO limit this to the first admin
  104. SiteSerializer.class_eval do
  105. attributes :complete_custom_wizard
  106. def include_wizard_required?
  107. scope.is_admin? && Wizard.new(scope.user).requires_completion?
  108. end
  109. def complete_custom_wizard
  110. if scope.user && requires_completion = CustomWizard::Wizard.prompt_completion(scope.user)
  111. requires_completion.map { |w| { name: w[:name], url: "/w/#{w[:id]}" } }
  112. end
  113. end
  114. def include_complete_custom_wizard?
  115. complete_custom_wizard.present?
  116. end
  117. end
  118. end