From 71181d63091c70fada66d73422e357c0ab82073a Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 21 Nov 2024 09:26:38 +0100 Subject: [PATCH 01/10] Use Alchemy::Solidus namespace for decorator constants In a world where decorators are managed with Zeitwerk, we want to make sure the naming of our decorators does not conflict with the naming of decorators in other gems. --- .../solidus/spree_product_decorator.rb | 29 +++++++++++++++++++ .../alchemy/solidus/spree_taxon_decorator.rb | 14 +++++++++ .../solidus/spree_variant_decorator.rb | 14 +++++++++ .../models/spree/spree_product_decorator.rb | 27 ----------------- .../models/spree/spree_taxon_decorator.rb | 12 -------- .../models/spree/spree_variant_decorator.rb | 12 -------- 6 files changed, 57 insertions(+), 51 deletions(-) create mode 100644 app/decorators/models/alchemy/solidus/spree_product_decorator.rb create mode 100644 app/decorators/models/alchemy/solidus/spree_taxon_decorator.rb create mode 100644 app/decorators/models/alchemy/solidus/spree_variant_decorator.rb delete mode 100644 app/decorators/models/spree/spree_product_decorator.rb delete mode 100644 app/decorators/models/spree/spree_taxon_decorator.rb delete mode 100644 app/decorators/models/spree/spree_variant_decorator.rb diff --git a/app/decorators/models/alchemy/solidus/spree_product_decorator.rb b/app/decorators/models/alchemy/solidus/spree_product_decorator.rb new file mode 100644 index 0000000..c377071 --- /dev/null +++ b/app/decorators/models/alchemy/solidus/spree_product_decorator.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeProductDecorator + def self.prepended(base) + base.include AlchemySolidus::TouchAlchemyIngredients + base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeProduct", as: :related_object, dependent: :nullify + end + + private + + # Overwritten Solidus' default behavior + # + # The Solidus implementation did not trigger `touch` on taxons, but + # updated the `updated_at` timestamp in an `update_all`. + # + # Since we want to invalidate ingredient spree taxons cache as well + # we need to use `touch` here and use the `after_touch` callback of + # Spree::Taxon + # + def touch_taxons + taxons.each(&:touch) + end + + ::Spree::Product.prepend self + end + end +end diff --git a/app/decorators/models/alchemy/solidus/spree_taxon_decorator.rb b/app/decorators/models/alchemy/solidus/spree_taxon_decorator.rb new file mode 100644 index 0000000..68f7094 --- /dev/null +++ b/app/decorators/models/alchemy/solidus/spree_taxon_decorator.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeTaxonDecorator + def self.prepended(base) + base.include AlchemySolidus::TouchAlchemyIngredients + base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeTaxon", as: :related_object, dependent: :nullify + end + + ::Spree::Taxon.prepend self + end + end +end diff --git a/app/decorators/models/alchemy/solidus/spree_variant_decorator.rb b/app/decorators/models/alchemy/solidus/spree_variant_decorator.rb new file mode 100644 index 0000000..ab2ecc2 --- /dev/null +++ b/app/decorators/models/alchemy/solidus/spree_variant_decorator.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeVariantDecorator + def self.prepended(base) + base.include AlchemySolidus::TouchAlchemyIngredients + base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeVariant", as: :related_object, dependent: :nullify + end + + ::Spree::Variant.prepend self + end + end +end diff --git a/app/decorators/models/spree/spree_product_decorator.rb b/app/decorators/models/spree/spree_product_decorator.rb deleted file mode 100644 index 6df1b3f..0000000 --- a/app/decorators/models/spree/spree_product_decorator.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -module Spree - module SpreeProductDecorator - def self.prepended(base) - base.include AlchemySolidus::TouchAlchemyIngredients - base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeProduct", as: :related_object, dependent: :nullify - end - - private - - # Overwritten Solidus' default behavior - # - # The Solidus implementation did not trigger `touch` on taxons, but - # updated the `updated_at` timestamp in an `update_all`. - # - # Since we want to invalidate ingredient spree taxons cache as well - # we need to use `touch` here and use the `after_touch` callback of - # Spree::Taxon - # - def touch_taxons - taxons.each(&:touch) - end - - ::Spree::Product.prepend self - end -end diff --git a/app/decorators/models/spree/spree_taxon_decorator.rb b/app/decorators/models/spree/spree_taxon_decorator.rb deleted file mode 100644 index a8866d5..0000000 --- a/app/decorators/models/spree/spree_taxon_decorator.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -module Spree - module SpreeTaxonDecorator - def self.prepended(base) - base.include AlchemySolidus::TouchAlchemyIngredients - base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeTaxon", as: :related_object, dependent: :nullify - end - - ::Spree::Taxon.prepend self - end -end diff --git a/app/decorators/models/spree/spree_variant_decorator.rb b/app/decorators/models/spree/spree_variant_decorator.rb deleted file mode 100644 index 688842c..0000000 --- a/app/decorators/models/spree/spree_variant_decorator.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -module Spree - module SpreeVariantDecorator - def self.prepended(base) - base.include AlchemySolidus::TouchAlchemyIngredients - base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeVariant", as: :related_object, dependent: :nullify - end - - ::Spree::Variant.prepend self - end -end From e8e81060ccd2c8a2d12978c6a1d43dd79e750d87 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 21 Nov 2024 11:40:21 +0100 Subject: [PATCH 02/10] Rename AlchemySolidus::TouchAlchemyIngredients If our namespace should be Alchemy::Solidus, then let's rename this file, too. --- .../solidus/spree_product_decorator.rb | 2 +- .../alchemy/solidus/spree_taxon_decorator.rb | 2 +- .../solidus/spree_variant_decorator.rb | 2 +- .../solidus/touch_alchemy_ingredients.rb | 20 +++++++++++++++++++ .../touch_alchemy_ingredients.rb | 18 ----------------- 5 files changed, 23 insertions(+), 21 deletions(-) create mode 100644 app/models/alchemy/solidus/touch_alchemy_ingredients.rb delete mode 100644 app/models/alchemy_solidus/touch_alchemy_ingredients.rb diff --git a/app/decorators/models/alchemy/solidus/spree_product_decorator.rb b/app/decorators/models/alchemy/solidus/spree_product_decorator.rb index c377071..2044bdb 100644 --- a/app/decorators/models/alchemy/solidus/spree_product_decorator.rb +++ b/app/decorators/models/alchemy/solidus/spree_product_decorator.rb @@ -4,7 +4,7 @@ module Alchemy module Solidus module SpreeProductDecorator def self.prepended(base) - base.include AlchemySolidus::TouchAlchemyIngredients + base.include Alchemy::Solidus::TouchAlchemyIngredients base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeProduct", as: :related_object, dependent: :nullify end diff --git a/app/decorators/models/alchemy/solidus/spree_taxon_decorator.rb b/app/decorators/models/alchemy/solidus/spree_taxon_decorator.rb index 68f7094..3f8fcc7 100644 --- a/app/decorators/models/alchemy/solidus/spree_taxon_decorator.rb +++ b/app/decorators/models/alchemy/solidus/spree_taxon_decorator.rb @@ -4,7 +4,7 @@ module Alchemy module Solidus module SpreeTaxonDecorator def self.prepended(base) - base.include AlchemySolidus::TouchAlchemyIngredients + base.include Alchemy::Solidus::TouchAlchemyIngredients base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeTaxon", as: :related_object, dependent: :nullify end diff --git a/app/decorators/models/alchemy/solidus/spree_variant_decorator.rb b/app/decorators/models/alchemy/solidus/spree_variant_decorator.rb index ab2ecc2..2712854 100644 --- a/app/decorators/models/alchemy/solidus/spree_variant_decorator.rb +++ b/app/decorators/models/alchemy/solidus/spree_variant_decorator.rb @@ -4,7 +4,7 @@ module Alchemy module Solidus module SpreeVariantDecorator def self.prepended(base) - base.include AlchemySolidus::TouchAlchemyIngredients + base.include Alchemy::Solidus::TouchAlchemyIngredients base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeVariant", as: :related_object, dependent: :nullify end diff --git a/app/models/alchemy/solidus/touch_alchemy_ingredients.rb b/app/models/alchemy/solidus/touch_alchemy_ingredients.rb new file mode 100644 index 0000000..3a2453f --- /dev/null +++ b/app/models/alchemy/solidus/touch_alchemy_ingredients.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module TouchAlchemyIngredients + extend ActiveSupport::Concern + + included do + after_update :touch_alchemy_ingredients + after_touch :touch_alchemy_ingredients + end + + private + + def touch_alchemy_ingredients + alchemy_ingredients.each(&:touch) + end + end + end +end diff --git a/app/models/alchemy_solidus/touch_alchemy_ingredients.rb b/app/models/alchemy_solidus/touch_alchemy_ingredients.rb deleted file mode 100644 index 52147eb..0000000 --- a/app/models/alchemy_solidus/touch_alchemy_ingredients.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -module AlchemySolidus - module TouchAlchemyIngredients - extend ActiveSupport::Concern - - included do - after_update :touch_alchemy_ingredients - after_touch :touch_alchemy_ingredients - end - - private - - def touch_alchemy_ingredients - alchemy_ingredients.each(&:touch) - end - end -end From c2a0bd7f3f51ff445fbc2687af672141b37b78e2 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 21 Nov 2024 13:13:52 +0100 Subject: [PATCH 03/10] Refactor(Performance): Use decorator for StoreController patch --- lib/alchemy/solidus/alchemy_in_solidus.rb | 6 ------ .../solidus/spree_store_controller_decorator.rb | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 lib/controllers/frontend/alchemy/solidus/spree_store_controller_decorator.rb diff --git a/lib/alchemy/solidus/alchemy_in_solidus.rb b/lib/alchemy/solidus/alchemy_in_solidus.rb index 2efbe06..00c136a 100644 --- a/lib/alchemy/solidus/alchemy_in_solidus.rb +++ b/lib/alchemy/solidus/alchemy_in_solidus.rb @@ -1,9 +1,3 @@ -# Allows to render Alchemy content within Solidus' controller views -Spree::StoreController.include( - Alchemy::ControllerActions, - Alchemy::ConfigurationMethods -) - # Hook into SolidusAuthDevise controllers if present if defined? Spree::Auth::Engine Spree::UserPasswordsController.include Alchemy::ControllerActions diff --git a/lib/controllers/frontend/alchemy/solidus/spree_store_controller_decorator.rb b/lib/controllers/frontend/alchemy/solidus/spree_store_controller_decorator.rb new file mode 100644 index 0000000..aee5bba --- /dev/null +++ b/lib/controllers/frontend/alchemy/solidus/spree_store_controller_decorator.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeStoreControllerDecorator + # Allows to render Alchemy content within Solidus' controller views + def self.prepended(base) + base.include Alchemy::ControllerActions + base.include Alchemy::ConfigurationMethods + end + + if defined?(::Spree::StoreController) + ::Spree::StoreController.prepend self + end + end + end +end From 4e27c5d1b4fa288db02593b8474fce51a1cb057f Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 21 Nov 2024 13:19:26 +0100 Subject: [PATCH 04/10] Make Devise controller patches autoloadable --- ...ree_user_confirmations_controller_decorator.rb | 15 +++++++++++++++ .../spree_user_passwords_controller_decorator.rb | 15 +++++++++++++++ ...ree_user_registrations_controller_decorator.rb | 15 +++++++++++++++ .../spree_user_sessions_controller_decorator.rb | 15 +++++++++++++++ lib/alchemy/solidus/alchemy_in_solidus.rb | 8 -------- 5 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 app/decorators/controllers/alchemy/solidus/spree_user_confirmations_controller_decorator.rb create mode 100644 app/decorators/controllers/alchemy/solidus/spree_user_passwords_controller_decorator.rb create mode 100644 app/decorators/controllers/alchemy/solidus/spree_user_registrations_controller_decorator.rb create mode 100644 app/decorators/controllers/alchemy/solidus/spree_user_sessions_controller_decorator.rb diff --git a/app/decorators/controllers/alchemy/solidus/spree_user_confirmations_controller_decorator.rb b/app/decorators/controllers/alchemy/solidus/spree_user_confirmations_controller_decorator.rb new file mode 100644 index 0000000..84ca911 --- /dev/null +++ b/app/decorators/controllers/alchemy/solidus/spree_user_confirmations_controller_decorator.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeUserConfirmationsControllerDecorator + def self.prepended(base) + base.include Alchemy::ControllerActions + end + + if defined?(::Spree::UserConfirmationsController) + ::Spree::UserConfirmationsController.prepend self + end + end + end +end diff --git a/app/decorators/controllers/alchemy/solidus/spree_user_passwords_controller_decorator.rb b/app/decorators/controllers/alchemy/solidus/spree_user_passwords_controller_decorator.rb new file mode 100644 index 0000000..95da958 --- /dev/null +++ b/app/decorators/controllers/alchemy/solidus/spree_user_passwords_controller_decorator.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeUserPasswordsControllerDecorator + def self.prepended(base) + base.include Alchemy::ControllerActions + end + + if defined?(::Spree::UserPasswordsController) + ::Spree::UserPasswordsController.prepend self + end + end + end +end diff --git a/app/decorators/controllers/alchemy/solidus/spree_user_registrations_controller_decorator.rb b/app/decorators/controllers/alchemy/solidus/spree_user_registrations_controller_decorator.rb new file mode 100644 index 0000000..1b0d6c6 --- /dev/null +++ b/app/decorators/controllers/alchemy/solidus/spree_user_registrations_controller_decorator.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeUserRegistrationsControllerDecorator + def self.prepended(base) + base.include Alchemy::ControllerActions + end + + if defined?(::Spree::UserRegistrationsController) + ::Spree::UserRegistrationsController.prepend self + end + end + end +end diff --git a/app/decorators/controllers/alchemy/solidus/spree_user_sessions_controller_decorator.rb b/app/decorators/controllers/alchemy/solidus/spree_user_sessions_controller_decorator.rb new file mode 100644 index 0000000..e81b232 --- /dev/null +++ b/app/decorators/controllers/alchemy/solidus/spree_user_sessions_controller_decorator.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeUserSessionsControllerDecorator + def self.prepended(base) + base.include Alchemy::ControllerActions + end + + if defined?(::Spree::UserSessionsController) + ::Spree::UserSessionsController.prepend self + end + end + end +end diff --git a/lib/alchemy/solidus/alchemy_in_solidus.rb b/lib/alchemy/solidus/alchemy_in_solidus.rb index 00c136a..ba8301f 100644 --- a/lib/alchemy/solidus/alchemy_in_solidus.rb +++ b/lib/alchemy/solidus/alchemy_in_solidus.rb @@ -1,11 +1,3 @@ -# Hook into SolidusAuthDevise controllers if present -if defined? Spree::Auth::Engine - Spree::UserPasswordsController.include Alchemy::ControllerActions - Spree::UserConfirmationsController.include Alchemy::ControllerActions - Spree::UserRegistrationsController.include Alchemy::ControllerActions - Spree::UserSessionsController.include Alchemy::ControllerActions -end - # Do not prefix element view partials with `spree` namespace. # See https://github.com/AlchemyCMS/alchemy_cms/issues/1626 ActionView::Base.prefix_partial_path_with_controller_namespace = false From 8d08752e8a264cda8ba7932dee3f11a088eb6096 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 21 Nov 2024 13:22:45 +0100 Subject: [PATCH 05/10] Move ActionView config into initializer We don't need to keep a whole file for a single line of ActionView configuration. --- lib/alchemy/solidus/alchemy_in_solidus.rb | 3 --- lib/alchemy/solidus/engine.rb | 5 ++++- 2 files changed, 4 insertions(+), 4 deletions(-) delete mode 100644 lib/alchemy/solidus/alchemy_in_solidus.rb diff --git a/lib/alchemy/solidus/alchemy_in_solidus.rb b/lib/alchemy/solidus/alchemy_in_solidus.rb deleted file mode 100644 index ba8301f..0000000 --- a/lib/alchemy/solidus/alchemy_in_solidus.rb +++ /dev/null @@ -1,3 +0,0 @@ -# Do not prefix element view partials with `spree` namespace. -# See https://github.com/AlchemyCMS/alchemy_cms/issues/1626 -ActionView::Base.prefix_partial_path_with_controller_namespace = false diff --git a/lib/alchemy/solidus/engine.rb b/lib/alchemy/solidus/engine.rb index 8b9308c..4c526cd 100644 --- a/lib/alchemy/solidus/engine.rb +++ b/lib/alchemy/solidus/engine.rb @@ -40,7 +40,10 @@ class Engine < ::Rails::Engine if SolidusSupport.frontend_available? # Allows to render Alchemy content within Solidus' controller views - require_dependency "alchemy/solidus/alchemy_in_solidus" + + # Do not prefix element view partials with `spree` namespace. + # See https://github.com/AlchemyCMS/alchemy_cms/issues/1626 + ActionView::Base.prefix_partial_path_with_controller_namespace = false end # Allows to use Solidus helpers within Alchemys controller views From 429ffa2bbacb6f3e3d122c1e6d3812c0918a7a4b Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 21 Nov 2024 13:26:47 +0100 Subject: [PATCH 06/10] Make Spree::User extension autoloadable. --- .../models/alchemy/solidus/spree_user_decorator.rb | 12 +++++++++--- lib/alchemy/solidus/engine.rb | 5 ----- spec/models/spree/user_extension_spec.rb | 5 ++--- 3 files changed, 11 insertions(+), 11 deletions(-) rename lib/alchemy/solidus/spree_user_extension.rb => app/decorators/models/alchemy/solidus/spree_user_decorator.rb (50%) diff --git a/lib/alchemy/solidus/spree_user_extension.rb b/app/decorators/models/alchemy/solidus/spree_user_decorator.rb similarity index 50% rename from lib/alchemy/solidus/spree_user_extension.rb rename to app/decorators/models/alchemy/solidus/spree_user_decorator.rb index 8706923..65d41f6 100644 --- a/lib/alchemy/solidus/spree_user_extension.rb +++ b/app/decorators/models/alchemy/solidus/spree_user_decorator.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + module Alchemy module Solidus - module SpreeUserExtension - def self.included(klass) - klass.has_many :folded_pages, class_name: "Alchemy::FoldedPage" + module SpreeUserDecorator + def self.prepended(base) + base.has_many :folded_pages, class_name: "Alchemy::FoldedPage" end def alchemy_display_name @@ -16,6 +18,10 @@ def alchemy_roles [] end end + + if defined?(::Spree::User) + ::Spree::User.prepend self + end end end end diff --git a/lib/alchemy/solidus/engine.rb b/lib/alchemy/solidus/engine.rb index 4c526cd..bcb3abf 100644 --- a/lib/alchemy/solidus/engine.rb +++ b/lib/alchemy/solidus/engine.rb @@ -25,11 +25,6 @@ class Engine < ::Rails::Engine Alchemy.register_ability ::Spree::Ability ::Spree::Ability.register_ability ::Alchemy::Permissions - if Alchemy.user_class_name == "::Spree::User" - require "alchemy/solidus/spree_user_extension" - Spree::User.include Alchemy::Solidus::SpreeUserExtension - end - if Alchemy.user_class_name == "::Alchemy::User" require "alchemy/solidus/alchemy_user_extension" require "alchemy/solidus/current_user_helpers" diff --git a/spec/models/spree/user_extension_spec.rb b/spec/models/spree/user_extension_spec.rb index 48df90d..3d631bc 100644 --- a/spec/models/spree/user_extension_spec.rb +++ b/spec/models/spree/user_extension_spec.rb @@ -1,9 +1,8 @@ # frozen_string_literal: true require "rails_helper" -require "alchemy/solidus/spree_user_extension" -RSpec.describe Alchemy::Solidus::SpreeUserExtension, type: :model do +RSpec.describe Alchemy::Solidus::SpreeUserDecorator, type: :model do let(:spree_user) do Class.new(ActiveRecord::Base) do def self.name @@ -14,7 +13,7 @@ def has_spree_role?(_role) false end - include Alchemy::Solidus::SpreeUserExtension + include Alchemy::Solidus::SpreeUserDecorator end end From 23bd553d5440b68e61bf48b90d24971f6491fd62 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 21 Nov 2024 14:02:11 +0100 Subject: [PATCH 07/10] Make AlchemyUser extension autoloadable --- .../models/alchemy/solidus/alchemy_user_decorator.rb | 8 ++++++-- lib/alchemy/solidus/engine.rb | 2 -- 2 files changed, 6 insertions(+), 4 deletions(-) rename lib/alchemy/solidus/alchemy_user_extension.rb => app/decorators/models/alchemy/solidus/alchemy_user_decorator.rb (63%) diff --git a/lib/alchemy/solidus/alchemy_user_extension.rb b/app/decorators/models/alchemy/solidus/alchemy_user_decorator.rb similarity index 63% rename from lib/alchemy/solidus/alchemy_user_extension.rb rename to app/decorators/models/alchemy/solidus/alchemy_user_decorator.rb index e236e18..01dbafa 100644 --- a/lib/alchemy/solidus/alchemy_user_extension.rb +++ b/app/decorators/models/alchemy/solidus/alchemy_user_decorator.rb @@ -1,7 +1,7 @@ module Alchemy module Solidus - module AlchemyUserExtension - def self.included(klass) + module AlchemyUserDecorator + def self.prepended(klass) klass.include Spree::UserMethods end @@ -12,6 +12,10 @@ def spree_roles ::Spree::Role.none end end + + if defined?(::Alchemy::User) + ::Alchemy::User.prepend self + end end end end diff --git a/lib/alchemy/solidus/engine.rb b/lib/alchemy/solidus/engine.rb index bcb3abf..97c8bc1 100644 --- a/lib/alchemy/solidus/engine.rb +++ b/lib/alchemy/solidus/engine.rb @@ -26,9 +26,7 @@ class Engine < ::Rails::Engine ::Spree::Ability.register_ability ::Alchemy::Permissions if Alchemy.user_class_name == "::Alchemy::User" - require "alchemy/solidus/alchemy_user_extension" require "alchemy/solidus/current_user_helpers" - Alchemy::User.include Alchemy::Solidus::AlchemyUserExtension ApplicationController.include Alchemy::Solidus::CurrentUserHelpers require "alchemy/solidus/spree_admin_unauthorized_redirect" end From 8b128dd3dcf87aa45b19d446e89c26ce05b0faaf Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 21 Nov 2024 14:07:23 +0100 Subject: [PATCH 08/10] Make ApplicationController extension autoloadable --- .../application_controller_decorator.rb | 19 +++++++++++++++++++ lib/alchemy/solidus/current_user_helpers.rb | 9 --------- lib/alchemy/solidus/engine.rb | 2 -- 3 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 app/decorators/controllers/alchemy/solidus/application_controller_decorator.rb delete mode 100644 lib/alchemy/solidus/current_user_helpers.rb diff --git a/app/decorators/controllers/alchemy/solidus/application_controller_decorator.rb b/app/decorators/controllers/alchemy/solidus/application_controller_decorator.rb new file mode 100644 index 0000000..f30a893 --- /dev/null +++ b/app/decorators/controllers/alchemy/solidus/application_controller_decorator.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module ApplicationControllerDecorator + def spree_current_user + if Alchemy.user_class_name == "::Alchemy::User" + current_user + else + super + end + end + + if defined?(::ApplicationController) + ::ApplicationController.prepend self + end + end + end +end diff --git a/lib/alchemy/solidus/current_user_helpers.rb b/lib/alchemy/solidus/current_user_helpers.rb deleted file mode 100644 index a843057..0000000 --- a/lib/alchemy/solidus/current_user_helpers.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Alchemy - module Solidus - module CurrentUserHelpers - def spree_current_user - current_user - end - end - end -end diff --git a/lib/alchemy/solidus/engine.rb b/lib/alchemy/solidus/engine.rb index 97c8bc1..66596f8 100644 --- a/lib/alchemy/solidus/engine.rb +++ b/lib/alchemy/solidus/engine.rb @@ -26,8 +26,6 @@ class Engine < ::Rails::Engine ::Spree::Ability.register_ability ::Alchemy::Permissions if Alchemy.user_class_name == "::Alchemy::User" - require "alchemy/solidus/current_user_helpers" - ApplicationController.include Alchemy::Solidus::CurrentUserHelpers require "alchemy/solidus/spree_admin_unauthorized_redirect" end From 460808d74f07049507bbb8445d548ff6fc268c91 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 21 Nov 2024 14:12:03 +0100 Subject: [PATCH 09/10] Make Controller and Helper patches autoloadable --- .../alchemy_base_controller_decorator.rb | 22 +++++++++++++++++++ .../solidus/alchemy_base_helper_decorator.rb | 19 ++++++++++++++++ lib/alchemy/solidus/engine.rb | 3 --- lib/alchemy/solidus/use_solidus_layout.rb | 19 ---------------- 4 files changed, 41 insertions(+), 22 deletions(-) create mode 100644 app/decorators/controllers/alchemy/solidus/alchemy_base_controller_decorator.rb create mode 100644 app/decorators/helpers/alchemy/solidus/alchemy_base_helper_decorator.rb delete mode 100644 lib/alchemy/solidus/use_solidus_layout.rb diff --git a/app/decorators/controllers/alchemy/solidus/alchemy_base_controller_decorator.rb b/app/decorators/controllers/alchemy/solidus/alchemy_base_controller_decorator.rb new file mode 100644 index 0000000..2c79d4e --- /dev/null +++ b/app/decorators/controllers/alchemy/solidus/alchemy_base_controller_decorator.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module AlchemyBaseControllerDecorator + def self.prepended(base) + base.include Spree::Core::ControllerHelpers::Auth + base.include Spree::Core::ControllerHelpers::Common + base.include Spree::Core::ControllerHelpers::Order + base.include Spree::Core::ControllerHelpers::PaymentParameters + base.include Spree::Core::ControllerHelpers::Pricing + base.include Spree::Core::ControllerHelpers::Search + base.include Spree::Core::ControllerHelpers::Store + base.include Spree::Core::ControllerHelpers::StrongParameters + end + + if defined?(::Alchemy::BaseController) + ::Alchemy::BaseController.prepend self + end + end + end +end diff --git a/app/decorators/helpers/alchemy/solidus/alchemy_base_helper_decorator.rb b/app/decorators/helpers/alchemy/solidus/alchemy_base_helper_decorator.rb new file mode 100644 index 0000000..03a3f48 --- /dev/null +++ b/app/decorators/helpers/alchemy/solidus/alchemy_base_helper_decorator.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module AlchemyBaseHelperDecorator + def self.prepended(base) + base.include Spree::BaseHelper + base.include Spree::CheckoutHelper + base.include Spree::ProductsHelper + base.include Spree::StoreHelper + base.include Spree::TaxonsHelper + end + + if defined?(::Alchemy::BaseHelper) + ::Alchemy::BaseHelper.prepend self + end + end + end +end diff --git a/lib/alchemy/solidus/engine.rb b/lib/alchemy/solidus/engine.rb index 66596f8..ab3de68 100644 --- a/lib/alchemy/solidus/engine.rb +++ b/lib/alchemy/solidus/engine.rb @@ -36,9 +36,6 @@ class Engine < ::Rails::Engine # See https://github.com/AlchemyCMS/alchemy_cms/issues/1626 ActionView::Base.prefix_partial_path_with_controller_namespace = false end - - # Allows to use Solidus helpers within Alchemys controller views - require_dependency "alchemy/solidus/use_solidus_layout" end end end diff --git a/lib/alchemy/solidus/use_solidus_layout.rb b/lib/alchemy/solidus/use_solidus_layout.rb deleted file mode 100644 index c965745..0000000 --- a/lib/alchemy/solidus/use_solidus_layout.rb +++ /dev/null @@ -1,19 +0,0 @@ -# Allows to use Solidus helpers within Alchemys controller views -Alchemy::BaseHelper.include( - Spree::BaseHelper, - Spree::CheckoutHelper, - Spree::ProductsHelper, - Spree::StoreHelper, - Spree::TaxonsHelper -) - -Alchemy::BaseController.include( - Spree::Core::ControllerHelpers::Auth, - Spree::Core::ControllerHelpers::Common, - Spree::Core::ControllerHelpers::Order, - Spree::Core::ControllerHelpers::PaymentParameters, - Spree::Core::ControllerHelpers::Pricing, - Spree::Core::ControllerHelpers::Search, - Spree::Core::ControllerHelpers::Store, - Spree::Core::ControllerHelpers::StrongParameters -) From c9473a5ff393e2eeb1cb042c5e92e4d2999182a4 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 21 Nov 2024 14:21:08 +0100 Subject: [PATCH 10/10] Make Unauthorized Redirect patch autoloadable --- lib/alchemy/solidus/engine.rb | 4 --- .../spree_admin_unauthorized_redirect.rb | 9 ------- .../spree_admin_base_controller_decorator.rb | 25 +++++++++++++++++++ 3 files changed, 25 insertions(+), 13 deletions(-) delete mode 100644 lib/alchemy/solidus/spree_admin_unauthorized_redirect.rb create mode 100644 lib/controllers/backend/alchemy/solidus/spree_admin_base_controller_decorator.rb 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 diff --git a/lib/controllers/backend/alchemy/solidus/spree_admin_base_controller_decorator.rb b/lib/controllers/backend/alchemy/solidus/spree_admin_base_controller_decorator.rb new file mode 100644 index 0000000..8cff8b4 --- /dev/null +++ b/lib/controllers/backend/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