plugin.rb 4.5 KB

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