diff --git a/README.md b/README.md index 2ad3647..9306762 100644 --- a/README.md +++ b/README.md @@ -194,32 +194,6 @@ $ bundle exec rake alchemy:install and follow the on screen instructions. -### Render Alchemy Content in Solidus Layout - -~~If you plan to render the Alchemy site in the Solidus layout add the following -to your initializer:~~ - -```ruby -# config/initializers/alchemy.rb -require 'alchemy/solidus/use_solidus_layout' -``` - -**NOTE:** Since v2.5.2 this is done automatically for you. If you upgraded from an older version you can safely remove this from your initializers. - -### Render Alchemy Content in Solidus views - -~~If you plan to render Alchemy content in your Solidus views (ie. a global header -or footer section), you need to include the Alchemy view helpers and language -store in your Solidus controllers with the following addition to your -initializer:~~ - -```ruby -# config/initializers/alchemy.rb -require 'alchemy/solidus/alchemy_in_solidus' -``` - -**NOTE:** Since v2.5.2 this is done automatically for you. If you upgraded from an older version you can safely remove this from your initializers. - ### Routing For routing you have a few options. diff --git a/alchemy-solidus.gemspec b/alchemy-solidus.gemspec index 99e5126..361b69e 100644 --- a/alchemy-solidus.gemspec +++ b/alchemy-solidus.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |gem| gem.add_dependency("alchemy_cms", [">= 7.2.0", "< 8"]) gem.add_dependency("solidus_core", [">= 4.0.0", "< 5"]) gem.add_dependency("solidus_backend", [">= 4.0.0", "< 5"]) - gem.add_dependency("solidus_support", [">= 0.1.1", "< 1"]) + gem.add_dependency("solidus_support", [">= 0.14.0", "< 1"]) gem.add_dependency("deface", ["~> 1.0"]) gem.add_development_dependency("rspec-rails", ["~> 6.0"]) diff --git a/app/patches/controllers/alchemy/solidus/alchemy_base_controller_patch.rb b/app/patches/controllers/alchemy/solidus/alchemy_base_controller_patch.rb new file mode 100644 index 0000000..fa93f49 --- /dev/null +++ b/app/patches/controllers/alchemy/solidus/alchemy_base_controller_patch.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module AlchemyBaseControllerPatch + 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/patches/controllers/alchemy/solidus/application_controller_patch.rb b/app/patches/controllers/alchemy/solidus/application_controller_patch.rb new file mode 100644 index 0000000..83951d2 --- /dev/null +++ b/app/patches/controllers/alchemy/solidus/application_controller_patch.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module ApplicationControllerPatch + 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/app/patches/controllers/alchemy/solidus/spree_user_confirmations_controller_patch.rb b/app/patches/controllers/alchemy/solidus/spree_user_confirmations_controller_patch.rb new file mode 100644 index 0000000..202139d --- /dev/null +++ b/app/patches/controllers/alchemy/solidus/spree_user_confirmations_controller_patch.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeUserConfirmationsControllerPatch + 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/patches/controllers/alchemy/solidus/spree_user_passwords_controller_patch.rb b/app/patches/controllers/alchemy/solidus/spree_user_passwords_controller_patch.rb new file mode 100644 index 0000000..aba773a --- /dev/null +++ b/app/patches/controllers/alchemy/solidus/spree_user_passwords_controller_patch.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeUserPasswordsControllerPatch + 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/patches/controllers/alchemy/solidus/spree_user_registrations_controller_patch.rb b/app/patches/controllers/alchemy/solidus/spree_user_registrations_controller_patch.rb new file mode 100644 index 0000000..a6dba3d --- /dev/null +++ b/app/patches/controllers/alchemy/solidus/spree_user_registrations_controller_patch.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeUserRegistrationsControllerPatch + 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/patches/controllers/alchemy/solidus/spree_user_sessions_controller_patch.rb b/app/patches/controllers/alchemy/solidus/spree_user_sessions_controller_patch.rb new file mode 100644 index 0000000..840fd46 --- /dev/null +++ b/app/patches/controllers/alchemy/solidus/spree_user_sessions_controller_patch.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeUserSessionsControllerPatch + def self.prepended(base) + base.include Alchemy::ControllerActions + end + + if defined?(::Spree::UserSessionsController) + ::Spree::UserSessionsController.prepend self + end + end + end +end diff --git a/app/patches/helpers/alchemy/solidus/alchemy_base_helper_patch.rb b/app/patches/helpers/alchemy/solidus/alchemy_base_helper_patch.rb new file mode 100644 index 0000000..a33c75c --- /dev/null +++ b/app/patches/helpers/alchemy/solidus/alchemy_base_helper_patch.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module AlchemyBaseHelperPatch + 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/alchemy_user_extension.rb b/app/patches/models/alchemy/solidus/alchemy_user_patch.rb similarity index 63% rename from lib/alchemy/solidus/alchemy_user_extension.rb rename to app/patches/models/alchemy/solidus/alchemy_user_patch.rb index e236e18..62a9caa 100644 --- a/lib/alchemy/solidus/alchemy_user_extension.rb +++ b/app/patches/models/alchemy/solidus/alchemy_user_patch.rb @@ -1,7 +1,7 @@ module Alchemy module Solidus - module AlchemyUserExtension - def self.included(klass) + module AlchemyUserPatch + 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/app/decorators/models/alchemy/solidus/spree_product_decorator.rb b/app/patches/models/alchemy/solidus/spree_product_patch.rb similarity index 96% rename from app/decorators/models/alchemy/solidus/spree_product_decorator.rb rename to app/patches/models/alchemy/solidus/spree_product_patch.rb index 2044bdb..0eee24a 100644 --- a/app/decorators/models/alchemy/solidus/spree_product_decorator.rb +++ b/app/patches/models/alchemy/solidus/spree_product_patch.rb @@ -2,7 +2,7 @@ module Alchemy module Solidus - module SpreeProductDecorator + module SpreeProductPatch def self.prepended(base) base.include Alchemy::Solidus::TouchAlchemyIngredients base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeProduct", as: :related_object, dependent: :nullify diff --git a/app/decorators/models/alchemy/solidus/spree_taxon_decorator.rb b/app/patches/models/alchemy/solidus/spree_taxon_patch.rb similarity index 91% rename from app/decorators/models/alchemy/solidus/spree_taxon_decorator.rb rename to app/patches/models/alchemy/solidus/spree_taxon_patch.rb index 3f8fcc7..fd12e75 100644 --- a/app/decorators/models/alchemy/solidus/spree_taxon_decorator.rb +++ b/app/patches/models/alchemy/solidus/spree_taxon_patch.rb @@ -2,7 +2,7 @@ module Alchemy module Solidus - module SpreeTaxonDecorator + module SpreeTaxonPatch def self.prepended(base) base.include Alchemy::Solidus::TouchAlchemyIngredients base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeTaxon", as: :related_object, dependent: :nullify diff --git a/lib/alchemy/solidus/spree_user_extension.rb b/app/patches/models/alchemy/solidus/spree_user_patch.rb similarity index 50% rename from lib/alchemy/solidus/spree_user_extension.rb rename to app/patches/models/alchemy/solidus/spree_user_patch.rb index 8706923..1e8fc1a 100644 --- a/lib/alchemy/solidus/spree_user_extension.rb +++ b/app/patches/models/alchemy/solidus/spree_user_patch.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 SpreeUserPatch + 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/app/decorators/models/alchemy/solidus/spree_variant_decorator.rb b/app/patches/models/alchemy/solidus/spree_variant_patch.rb similarity index 91% rename from app/decorators/models/alchemy/solidus/spree_variant_decorator.rb rename to app/patches/models/alchemy/solidus/spree_variant_patch.rb index 2712854..957e3ce 100644 --- a/app/decorators/models/alchemy/solidus/spree_variant_decorator.rb +++ b/app/patches/models/alchemy/solidus/spree_variant_patch.rb @@ -2,7 +2,7 @@ module Alchemy module Solidus - module SpreeVariantDecorator + module SpreeVariantPatch def self.prepended(base) base.include Alchemy::Solidus::TouchAlchemyIngredients base.has_many :alchemy_ingredients, class_name: "Alchemy::Ingredients::SpreeVariant", as: :related_object, dependent: :nullify diff --git a/lib/alchemy/solidus/alchemy_in_solidus.rb b/lib/alchemy/solidus/alchemy_in_solidus.rb deleted file mode 100644 index 2efbe06..0000000 --- a/lib/alchemy/solidus/alchemy_in_solidus.rb +++ /dev/null @@ -1,17 +0,0 @@ -# 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 - 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 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 8b9308c..25bddb2 100644 --- a/lib/alchemy/solidus/engine.rb +++ b/lib/alchemy/solidus/engine.rb @@ -25,26 +25,21 @@ 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" - Alchemy::User.include Alchemy::Solidus::AlchemyUserExtension - ApplicationController.include Alchemy::Solidus::CurrentUserHelpers - require "alchemy/solidus/spree_admin_unauthorized_redirect" - end - 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 + end - # Allows to use Solidus helpers within Alchemys controller views - require_dependency "alchemy/solidus/use_solidus_layout" + initializer "alchemy_solidus.patches" do |app| + if Alchemy.gem_version < Gem::Version.new("7.4.0") && SolidusSupport.backend_available? + app.config.to_prepare do + Alchemy::Solidus::SpreeAdminBaseControllerPatch + end + end end end end 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/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 -) diff --git a/lib/patches/backend/controllers/alchemy/solidus/spree_admin_base_controller_patch.rb b/lib/patches/backend/controllers/alchemy/solidus/spree_admin_base_controller_patch.rb new file mode 100644 index 0000000..e86f1c7 --- /dev/null +++ b/lib/patches/backend/controllers/alchemy/solidus/spree_admin_base_controller_patch.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeAdminBaseControllerPatch + 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/patches/frontend/controllers/alchemy/solidus/spree_store_controller_patch.rb b/lib/patches/frontend/controllers/alchemy/solidus/spree_store_controller_patch.rb new file mode 100644 index 0000000..c8a5053 --- /dev/null +++ b/lib/patches/frontend/controllers/alchemy/solidus/spree_store_controller_patch.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Alchemy + module Solidus + module SpreeStoreControllerPatch + # 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 diff --git a/spec/controllers/spree/store_controller_spec.rb b/spec/controllers/spree/store_controller_spec.rb new file mode 100644 index 0000000..fde8c6f --- /dev/null +++ b/spec/controllers/spree/store_controller_spec.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe Spree::StoreController do + it "includes Alchemy modules" do + expect(described_class.ancestors).to include(Alchemy::ConfigurationMethods) + expect(described_class.ancestors).to include(Alchemy::ControllerActions) + end +end diff --git a/spec/models/spree/user_extension_spec.rb b/spec/models/spree/user_extension_spec.rb index 48df90d..5ce990d 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::SpreeUserPatch, 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::SpreeUserPatch end end