0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-23 01:40:29 +01:00
discourse-custom-wizard/plugin.rb

169 Zeilen
5,7 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
2020-04-14 14:12:43 +02:00
# version: 0.2
2017-09-23 04:34:07 +02:00
# 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
2020-04-01 07:03:26 +02:00
register_asset 'stylesheets/common/wizard-admin.scss'
register_asset 'stylesheets/common/wizard-mapper.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
enabled_site_setting :custom_wizard_enabled
2017-09-25 16:47:40 +02:00
config = Rails.application.config
2019-12-05 09:05:21 +01:00
plugin_asset_path = "#{Rails.root}/plugins/discourse-custom-wizard/assets"
config.assets.paths << "#{plugin_asset_path}/javascripts"
config.assets.paths << "#{plugin_asset_path}/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{
2020-05-14 07:00:23 +02:00
wizard-preload.js
2019-10-09 01:09:30 +02:00
wizard-custom-guest.js
2017-12-03 08:57:54 +01:00
wizard-custom-lib.js
wizard-custom.js
wizard-plugin.js
2019-01-18 01:34:52 +01:00
wizard-custom-start.js
2019-01-29 01:42:23 +01:00
wizard-raw-templates.js.erb
2020-04-03 10:49:36 +02:00
stylesheets/wizard/wizard_autocomplete.scss
2017-12-03 08:57:54 +01:00
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
2020-04-03 10:49:36 +02:00
stylesheets/wizard/wizard_locations.scss
2020-04-02 15:21:19 +02:00
stylesheets/wizard/wizard_events.scss
2017-12-03 08:57:54 +01:00
}
2017-11-01 12:47:27 +01:00
end
2019-02-20 07:27:17 +01:00
if respond_to?(:register_svg_icon)
2020-03-30 01:53:28 +02:00
register_svg_icon "far-calendar"
2019-02-20 07:27:17 +01:00
register_svg_icon "chevron-right"
register_svg_icon "chevron-left"
end
2019-01-14 03:03:42 +01:00
2017-09-23 04:34:07 +02:00
after_initialize do
2020-03-30 08:16:03 +02:00
%w[
../lib/custom_wizard/engine.rb
../config/routes.rb
2020-04-13 14:17:22 +02:00
../controllers/custom_wizard/admin/admin.rb
../controllers/custom_wizard/admin/wizard.rb
../controllers/custom_wizard/admin/submissions.rb
../controllers/custom_wizard/admin/api.rb
2020-04-15 02:46:44 +02:00
../controllers/custom_wizard/admin/logs.rb
2020-03-30 08:16:03 +02:00
../controllers/custom_wizard/wizard.rb
../controllers/custom_wizard/steps.rb
../controllers/custom_wizard/transfer.rb
../jobs/clear_after_time_wizard.rb
../jobs/refresh_api_access_token.rb
../jobs/set_after_time_wizard.rb
../lib/custom_wizard/action_result.rb
2020-04-15 02:46:44 +02:00
../lib/custom_wizard/action.rb
2020-03-30 08:16:03 +02:00
../lib/custom_wizard/builder.rb
../lib/custom_wizard/field.rb
../lib/custom_wizard/mapper.rb
2020-04-15 02:46:44 +02:00
../lib/custom_wizard/log.rb
2020-03-30 08:16:03 +02:00
../lib/custom_wizard/step_updater.rb
2020-04-13 14:17:22 +02:00
../lib/custom_wizard/validator.rb
2020-03-30 08:16:03 +02:00
../lib/custom_wizard/wizard.rb
../lib/custom_wizard/api/api.rb
../lib/custom_wizard/api/authorization.rb
../lib/custom_wizard/api/endpoint.rb
../lib/custom_wizard/api/log_entry.rb
../serializers/custom_wizard/api/authorization_serializer.rb
../serializers/custom_wizard/api/basic_endpoint_serializer.rb
../serializers/custom_wizard/api/endpoint_serializer.rb
../serializers/custom_wizard/api/log_serializer.rb
../serializers/custom_wizard/api_serializer.rb
../serializers/custom_wizard/basic_api_serializer.rb
2020-04-13 14:17:22 +02:00
../serializers/custom_wizard/basic_wizard_serializer.rb
2020-03-30 08:16:03 +02:00
../serializers/custom_wizard/wizard_field_serializer.rb
../serializers/custom_wizard/wizard_step_serializer.rb
../serializers/custom_wizard/wizard_serializer.rb
2020-04-15 02:46:44 +02:00
../serializers/custom_wizard/log_serializer.rb
2020-04-19 08:06:18 +02:00
../extensions/extra_locales_controller.rb
../extensions/invites_controller.rb
../extensions/wizard_field.rb
../extensions/wizard_step.rb
].each do |path|
load File.expand_path(path, __FILE__)
end
2019-12-05 09:05:21 +01:00
add_class_method(:wizard, :user_requires_completion?) do |user|
wizard_result = self.new(user).requires_completion?
return wizard_result if wizard_result
2019-12-05 09:05:21 +01:00
custom_redirect = false
2019-12-05 09:05:21 +01:00
if user &&
user.first_seen_at.blank? &&
2020-03-30 08:16:03 +02:00
wizard = CustomWizard::Wizard.after_signup(user)
2017-10-15 05:58:22 +02:00
2020-03-30 08:16:03 +02:00
if !wizard.completed?
2019-12-05 09:05:21 +01:00
custom_redirect = true
2020-04-13 14:17:22 +02:00
CustomWizard::Wizard.set_wizard_redirect(wizard.id, user)
end
end
2019-12-05 09:05:21 +01:00
!!custom_redirect
end
2019-12-05 09:05:21 +01:00
add_to_class(:users_controller, :wizard_path) do
if custom_wizard_redirect = current_user.custom_fields['redirect_to_wizard']
"#{Discourse.base_url}/w/#{custom_wizard_redirect.dasherize}"
else
"#{Discourse.base_url}/wizard"
2017-11-01 05:21:14 +01:00
end
end
2019-12-05 09:05:21 +01:00
add_to_serializer(:current_user, :redirect_to_wizard) do
object.custom_fields['redirect_to_wizard']
end
2018-01-16 08:13:50 +01:00
2020-03-30 01:53:28 +02:00
on(:user_approved) do |user|
2020-04-19 08:42:44 +02:00
if wizard = CustomWizard::Wizard.after_signup(user)
CustomWizard::Wizard.set_wizard_redirect(wizard.id, user)
end
end
2020-04-19 08:06:18 +02:00
add_to_class(:application_controller, :redirect_to_wizard_if_required) do
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_submission_redirect(current_user, wizard_id, request.referer)
end
2020-04-19 08:06:18 +02:00
if CustomWizard::Wizard.exists?(wizard_id)
redirect_to "/w/#{wizard_id.dasherize}"
end
end
end
add_to_serializer(:site, :include_wizard_required?) do
scope.is_admin? && Wizard.new(scope.user).requires_completion?
end
add_to_serializer(:site, :complete_custom_wizard) do
if scope.user && requires_completion = CustomWizard::Wizard.prompt_completion(scope.user)
requires_completion.map {|w| { name: w[:name], url: "/w/#{w[:id]}"} }
end
end
add_to_serializer(:site, :include_complete_custom_wizard?) do
complete_custom_wizard.present?
end
add_model_callback(:application_controller, :before_action) do
redirect_to_wizard_if_required if current_user
end
::ExtraLocalesController.prepend ExtraLocalesControllerCustomWizard
::InvitesController.prepend InvitesControllerCustomWizard
::Wizard::Field.prepend CustomWizardFieldExtension
::Wizard::Step.prepend CustomWizardStepExtension
2020-04-19 08:06:18 +02:00
2018-01-16 08:13:50 +01:00
DiscourseEvent.trigger(:custom_wizard_ready)
2017-09-23 04:34:07 +02:00
end