From 02c6096395288b4f5b1a03a126035bd371d4e276 Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Sun, 19 Apr 2020 16:06:18 +1000 Subject: [PATCH] Update structure --- controllers/application_controller.rb | 29 ------------ controllers/extra_locales_controller.rb | 20 -------- extensions/extra_locales_controller.rb | 16 +++++++ .../invites_controller.rb | 6 +-- plugin.rb | 47 +++++++++++++++++-- serializers/site_serializer.rb | 29 ------------ 6 files changed, 60 insertions(+), 87 deletions(-) delete mode 100644 controllers/application_controller.rb delete mode 100644 controllers/extra_locales_controller.rb create mode 100644 extensions/extra_locales_controller.rb rename {controllers => extensions}/invites_controller.rb (71%) delete mode 100644 serializers/site_serializer.rb diff --git a/controllers/application_controller.rb b/controllers/application_controller.rb deleted file mode 100644 index a9ae33ab..00000000 --- a/controllers/application_controller.rb +++ /dev/null @@ -1,29 +0,0 @@ -module CustomWizardApplicationControllerExtension - extend ActiveSupport::Concern - - def self.prepended(klass) - klass.class_eval do - before_action :redirect_to_wizard_if_required, if: :current_user - end - end - - 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_submission_redirect(current_user, wizard_id, request.referer) - end - - if CustomWizard::Wizard.exists?(wizard_id) - redirect_to "/w/#{wizard_id.dasherize}" - end - end - end -end - -class ApplicationController - prepend CustomWizardApplicationControllerExtension if SiteSetting.custom_wizard_enabled -end \ No newline at end of file diff --git a/controllers/extra_locales_controller.rb b/controllers/extra_locales_controller.rb deleted file mode 100644 index e1da1803..00000000 --- a/controllers/extra_locales_controller.rb +++ /dev/null @@ -1,20 +0,0 @@ -module CustomWizardExtraLocalesController - def show - if request.referer && URI(request.referer).path.include?('/w/') - bundle = params[:bundle] - - if params[:v]&.size == 32 - hash = ExtraLocalesController.bundle_js_hash(bundle) - immutable_for(1.year) if hash == params[:v] - end - - render plain: ExtraLocalesController.bundle_js(bundle), content_type: "application/javascript" - else - super - end - end -end - -class ExtraLocalesController - prepend CustomWizardExtraLocalesController if SiteSetting.custom_wizard_enabled -end \ No newline at end of file diff --git a/extensions/extra_locales_controller.rb b/extensions/extra_locales_controller.rb new file mode 100644 index 00000000..2e3f8a78 --- /dev/null +++ b/extensions/extra_locales_controller.rb @@ -0,0 +1,16 @@ +module ExtraLocalesControllerCustomWizard + def show + if request.referer && URI(request.referer).path.include?('/w/') + bundle = params[:bundle] + + if params[:v]&.size == 32 + hash = ::ExtraLocalesController.bundle_js_hash(bundle) + immutable_for(1.year) if hash == params[:v] + end + + render plain: ::ExtraLocalesController.bundle_js(bundle), content_type: "application/javascript" + else + super + end + end +end \ No newline at end of file diff --git a/controllers/invites_controller.rb b/extensions/invites_controller.rb similarity index 71% rename from controllers/invites_controller.rb rename to extensions/invites_controller.rb index 760100f2..1833bf38 100644 --- a/controllers/invites_controller.rb +++ b/extensions/invites_controller.rb @@ -1,6 +1,6 @@ module InvitesControllerCustomWizard def path(url) - if Wizard.user_requires_completion?(@user) + if ::Wizard.user_requires_completion?(@user) wizard_id = @user.custom_fields['custom_wizard_redirect'] if wizard_id && url != '/' @@ -15,8 +15,4 @@ module InvitesControllerCustomWizard super(user) @user = user end -end - -class InvitesController - prepend InvitesControllerCustomWizard if SiteSetting.custom_wizard_enabled end \ No newline at end of file diff --git a/plugin.rb b/plugin.rb index ea79af11..a8d81b14 100644 --- a/plugin.rb +++ b/plugin.rb @@ -52,9 +52,6 @@ after_initialize do ../controllers/custom_wizard/wizard.rb ../controllers/custom_wizard/steps.rb ../controllers/custom_wizard/transfer.rb - ../controllers/application_controller.rb - ../controllers/extra_locales_controller.rb - ../controllers/invites_controller.rb ../jobs/clear_after_time_wizard.rb ../jobs/refresh_api_access_token.rb ../jobs/set_after_time_wizard.rb @@ -83,7 +80,8 @@ after_initialize do ../serializers/custom_wizard/wizard_step_serializer.rb ../serializers/custom_wizard/wizard_serializer.rb ../serializers/custom_wizard/log_serializer.rb - ../serializers/site_serializer.rb + ../extensions/extra_locales_controller.rb + ../extensions/invites_controller.rb ].each do |path| load File.expand_path(path, __FILE__) end @@ -124,6 +122,47 @@ after_initialize do CustomWizard::Wizard.set_wizard_redirect(wizard_id, user) end end + + 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 + + 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 + + reloadable_patch do |plugin| + if enabled? + ::ExtraLocalesController.prepend ExtraLocalesControllerCustomWizard + ::InvitesController.prepend InvitesControllerCustomWizard + end + end + DiscourseEvent.trigger(:custom_wizard_ready) end diff --git a/serializers/site_serializer.rb b/serializers/site_serializer.rb deleted file mode 100644 index 8a25e032..00000000 --- a/serializers/site_serializer.rb +++ /dev/null @@ -1,29 +0,0 @@ -## TODO limit this to the first admin - -module CustomWizardSiteSerializerExtension - extend ActiveSupport::Concern - - def self.prepended(klass) - klass.class_eval do - attributes :complete_custom_wizard - end - end - - def include_wizard_required? - scope.is_admin? && Wizard.new(scope.user).requires_completion? - end - - def complete_custom_wizard - if scope.user && requires_completion = CustomWizard::Wizard.prompt_completion(scope.user) - requires_completion.map {|w| { name: w[:name], url: "/w/#{w[:id]}"} } - end - end - - def include_complete_custom_wizard? - complete_custom_wizard.present? - end -end - -class ::SiteSerializer - prepend CustomWizardSiteSerializerExtension if SiteSetting.custom_wizard_enabled -end \ No newline at end of file