0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-13 21:42:53 +01:00
discourse-custom-wizard/plugin.rb

158 Zeilen
5,2 KiB
Ruby

2017-09-23 04:34:07 +02:00
# name: discourse-custom-wizard
2017-10-05 02:36:46 +02:00
# about: Create custom wizards
2017-09-23 04:34:07 +02:00
# version: 0.1
# authors: Angus McLeod
2018-01-12 08:23:57 +01:00
# url: https://github.com/angusmcleod/discourse-custom-wizard
2017-09-23 04:34:07 +02:00
2017-10-13 15:02:34 +02:00
register_asset 'stylesheets/wizard_custom_admin.scss'
2017-11-01 05:21:14 +01:00
register_asset 'lib/jquery.timepicker.min.js'
register_asset 'lib/jquery.timepicker.scss'
2017-09-23 04:34:07 +02:00
2017-09-25 16:47:40 +02:00
config = Rails.application.config
2017-10-17 09:18:53 +02:00
config.assets.paths << Rails.root.join('plugins', 'discourse-custom-wizard', 'assets', 'javascripts')
config.assets.paths << Rails.root.join('plugins', 'discourse-custom-wizard', 'assets', 'stylesheets', 'wizard')
2017-09-25 16:47:40 +02:00
2017-11-01 12:47:27 +01:00
if Rails.env.production?
2017-12-03 08:57:54 +01:00
config.assets.precompile += %w{
wizard-custom-lib.js
wizard-custom.js
wizard-plugin.js
stylesheets/wizard/wizard_custom.scss
2018-02-08 05:30:55 +01:00
stylesheets/wizard/wizard_composer.scss
stylesheets/wizard/wizard_variables.scss
2017-12-03 08:57:54 +01:00
stylesheets/wizard/wizard_custom_mobile.scss
}
2017-11-01 12:47:27 +01:00
end
2017-09-23 04:34:07 +02:00
after_initialize do
2017-10-22 05:37:58 +02:00
UserHistory.actions[:custom_wizard_step] = 1000
2017-10-15 05:58:22 +02:00
2017-10-17 09:18:53 +02:00
require_dependency 'application_controller'
2017-09-23 04:34:07 +02:00
module ::CustomWizard
class Engine < ::Rails::Engine
2017-10-17 09:18:53 +02:00
engine_name 'custom_wizard'
2017-09-23 04:34:07 +02:00
isolate_namespace CustomWizard
end
end
CustomWizard::Engine.routes.draw do
get ':wizard_id' => 'wizard#index'
2017-11-01 05:21:14 +01:00
put ':wizard_id/skip' => 'wizard#skip'
2017-10-22 05:37:58 +02:00
get ':wizard_id/steps' => 'wizard#index'
get ':wizard_id/steps/:step_id' => 'wizard#index'
put ':wizard_id/steps/:step_id' => 'steps#update'
2017-09-23 04:34:07 +02:00
end
require_dependency 'admin_constraint'
Discourse::Application.routes.append do
2017-10-13 15:02:34 +02:00
mount ::CustomWizard::Engine, at: 'w'
2017-09-23 04:34:07 +02:00
2017-09-25 16:47:40 +02:00
scope module: 'custom_wizard', constraints: AdminConstraint.new do
2017-10-05 02:36:46 +02:00
get 'admin/wizards' => 'admin#index'
get 'admin/wizards/field-types' => 'admin#field_types'
2017-09-25 16:47:40 +02:00
get 'admin/wizards/custom' => 'admin#index'
get 'admin/wizards/custom/new' => 'admin#index'
2017-10-05 02:36:46 +02:00
get 'admin/wizards/custom/all' => 'admin#custom_wizards'
get 'admin/wizards/custom/:wizard_id' => 'admin#find_wizard'
2017-09-25 16:47:40 +02:00
put 'admin/wizards/custom/save' => 'admin#save'
delete 'admin/wizards/custom/remove' => 'admin#remove'
2017-10-05 02:36:46 +02:00
get 'admin/wizards/submissions' => 'admin#index'
2017-10-22 05:37:58 +02:00
get 'admin/wizards/submissions/:wizard_id' => 'admin#submissions'
2017-10-05 02:36:46 +02:00
end
end
2017-11-01 05:21:14 +01:00
load File.expand_path('../jobs/clear_after_time_wizard.rb', __FILE__)
load File.expand_path('../jobs/set_after_time_wizard.rb', __FILE__)
2017-10-15 05:58:22 +02:00
load File.expand_path('../lib/builder.rb', __FILE__)
load File.expand_path('../lib/field.rb', __FILE__)
load File.expand_path('../lib/step_updater.rb', __FILE__)
load File.expand_path('../lib/template.rb', __FILE__)
load File.expand_path('../lib/wizard.rb', __FILE__)
load File.expand_path('../lib/wizard_edits.rb', __FILE__)
load File.expand_path('../controllers/wizard.rb', __FILE__)
load File.expand_path('../controllers/steps.rb', __FILE__)
load File.expand_path('../controllers/admin.rb', __FILE__)
2017-11-01 05:21:14 +01:00
::UsersController.class_eval do
def wizard_path
if custom_wizard_redirect = $redis.get('custom_wizard_redirect')
"#{Discourse.base_url}/w/#{custom_wizard_redirect.dasherize}"
2017-11-01 05:21:14 +01:00
else
"#{Discourse.base_url}/wizard"
end
end
end
module InvitesControllerCustomWizard
def path(url)
if Wizard.user_requires_completion?(@user)
2018-05-14 04:11:11 +02:00
wizard_id = $redis.get('custom_wizard_redirect')
if wizard_id && url != '/'
CustomWizard::Wizard.set_redirect(@user, wizard_id, url)
url = "/w/#{wizard_id.dasherize}"
2017-11-01 05:21:14 +01:00
end
end
super(url)
end
private def post_process_invite(user)
super(user)
@user = user
end
end
require_dependency 'invites_controller'
class ::InvitesController
prepend InvitesControllerCustomWizard
end
class ::ApplicationController
before_action :redirect_to_wizard_if_required, if: :current_user
def redirect_to_wizard_if_required
@wizard_id ||= current_user.custom_fields['redirect_to_wizard']
@excluded_routes ||= SiteSetting.wizard_redirect_exclude_paths.split('|') + ['/w/']
url = request.referer || request.original_url
if request.format === 'text/html' && !@excluded_routes.any? { |str| /#{str}/ =~ url } && @wizard_id
if request.referer !~ /\/w\// && request.referer !~ /\/invites\//
CustomWizard::Wizard.set_redirect(current_user, @wizard_id, request.referer)
end
redirect_to "/w/#{@wizard_id.dasherize}"
2017-11-01 05:21:14 +01:00
end
end
end
add_to_serializer(:current_user, :redirect_to_wizard) { object.custom_fields['redirect_to_wizard'] }
2017-11-03 14:45:08 +01:00
## TODO limit this to the first admin
SiteSerializer.class_eval do
2017-11-22 10:34:21 +01:00
attributes :complete_custom_wizard
def include_wizard_required?
scope.is_admin? && Wizard.new(scope.user).requires_completion?
end
2017-11-22 10:34:21 +01:00
def complete_custom_wizard
2017-11-22 14:06:09 +01:00
if scope.user && requires_completion = CustomWizard::Wizard.prompt_completion(scope.user)
2017-11-22 10:34:21 +01:00
requires_completion.map { |w| { name: w[:name], url: "/w/#{w[:id]}" } }
end
end
def include_complete_custom_wizard?
complete_custom_wizard.present?
end
end
2018-01-16 08:13:50 +01:00
DiscourseEvent.on(:user_approved) do |user|
if wizard_id = CustomWizard::Wizard.after_signup
user.custom_fields['redirect_to_wizard'] = wizard_id
user.save_custom_fields(true)
end
end
2018-01-16 08:13:50 +01:00
DiscourseEvent.trigger(:custom_wizard_ready)
2017-09-23 04:34:07 +02:00
end