From 705a7994b296f423556ea18fc19e0f0eb68f2d79 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 21 Nov 2024 14:21:08 +0100 Subject: [PATCH] Make Unauthorized Redirect patch autoloadable --- .../spree_admin_base_controller_decorator.rb | 25 +++++++++++++++++++ lib/alchemy/solidus/engine.rb | 4 --- .../spree_admin_unauthorized_redirect.rb | 9 ------- 3 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 app/decorators/controllers/alchemy/solidus/spree_admin_base_controller_decorator.rb delete mode 100644 lib/alchemy/solidus/spree_admin_unauthorized_redirect.rb diff --git a/app/decorators/controllers/alchemy/solidus/spree_admin_base_controller_decorator.rb b/app/decorators/controllers/alchemy/solidus/spree_admin_base_controller_decorator.rb new file mode 100644 index 0000000..8cff8b4 --- /dev/null +++ b/app/decorators/controllers/alchemy/solidus/spree_admin_base_controller_decorator.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeAdminBaseControllerDecorator + def self.prepended(base) + if Alchemy.user_class_name == "::Alchemy::User" + base.unauthorized_redirect = -> do + if spree_current_user + flash[:error] = I18n.t('spree.authorization_failure') + redirect_to spree.root_path + else + store_location + redirect_to Alchemy.login_path + end + end + end + end + + if defined?(::Spree::Admin::BaseController) + ::Spree::Admin::BaseController.prepend self + end + end + end +end diff --git a/lib/alchemy/solidus/engine.rb b/lib/alchemy/solidus/engine.rb index ab3de68..b898f28 100644 --- a/lib/alchemy/solidus/engine.rb +++ b/lib/alchemy/solidus/engine.rb @@ -25,10 +25,6 @@ class Engine < ::Rails::Engine Alchemy.register_ability ::Spree::Ability ::Spree::Ability.register_ability ::Alchemy::Permissions - if Alchemy.user_class_name == "::Alchemy::User" - require "alchemy/solidus/spree_admin_unauthorized_redirect" - end - if SolidusSupport.frontend_available? # Allows to render Alchemy content within Solidus' controller views diff --git a/lib/alchemy/solidus/spree_admin_unauthorized_redirect.rb b/lib/alchemy/solidus/spree_admin_unauthorized_redirect.rb deleted file mode 100644 index e680f4c..0000000 --- a/lib/alchemy/solidus/spree_admin_unauthorized_redirect.rb +++ /dev/null @@ -1,9 +0,0 @@ -Spree::Admin::BaseController.unauthorized_redirect = -> do - if spree_current_user - flash[:error] = I18n.t('spree.authorization_failure') - redirect_to spree.root_path - else - store_location - redirect_to Alchemy.login_path - end -end