From dc3e15a48917a17d69e4e66dd9170e44b7fc36eb Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Mon, 29 Jan 2024 15:41:21 +0100 Subject: [PATCH] Add promotion-specific configuration to Spree::Core::PromotionConfiguration We want to be able to move all promotion-related things out of `solidus_core` in PR #5634. However, Spree::AppConfiguration also does all the promotion-specific things, and we can't move the app configuration out of core. In #5658, we've added a promotion configuration object as a nucleus for core's promotion system's configuration, `Spree::Core::PromotionConfiguration`. This implements all the promotion-specific configuration preferences from `Spree::AppConfiguration` there. --- .../lib/spree/core/promotion_configuration.rb | 37 ++++++++++++++++++- .../core/promotion_configuration_spec.rb | 20 +++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/core/lib/spree/core/promotion_configuration.rb b/core/lib/spree/core/promotion_configuration.rb index cacbbd68e71..9b44d2f584c 100644 --- a/core/lib/spree/core/promotion_configuration.rb +++ b/core/lib/spree/core/promotion_configuration.rb @@ -2,9 +2,44 @@ module Spree module Core - class PromotionConfiguration + class PromotionConfiguration < Spree::Preferences::Configuration include Core::EnvironmentExtension + # @!attribute [rw] promotions_per_page + # @return [Integer] Promotions to show per-page in the admin (default: +15+) + preference :promotions_per_page, :integer, default: 15 + + # promotion_chooser_class allows extensions to provide their own PromotionChooser + class_name_attribute :promotion_chooser_class, default: 'Spree::PromotionChooser' + + # promotion_adjuster_class allows extensions to provide their own Promotion Adjuster + class_name_attribute :promotion_adjuster_class, default: 'Spree::Promotion::OrderAdjustmentsRecalculator' + + # Allows providing a different shipping promotion handler. + # @!attribute [rw] shipping_promotion_handler_class + # @see Spree::PromotionHandler::Shipping + # @return [Class] an object that conforms to the API of + # the standard shipping promotion handler class + # Spree::PromotionHandler::Coupon. + class_name_attribute :shipping_promotion_handler_class, default: 'Spree::PromotionHandler::Shipping' + + # Allows providing your own Mailer for promotion code batch mailer. + # + # @!attribute [rw] promotion_code_batch_mailer_class + # @return [ActionMailer::Base] an object that responds to "promotion_code_batch_finished", + # and "promotion_code_batch_errored" + # (e.g. an ActionMailer with a "promotion_code_batch_finished" method) with the same + # signature as Spree::PromotionCodeBatchMailer.promotion_code_batch_finished. + class_name_attribute :promotion_code_batch_mailer_class, default: 'Spree::PromotionCodeBatchMailer' + + # Allows providing a different coupon code handler. + # @!attribute [rw] coupon_code_handler_class + # @see Spree::PromotionHandler::Coupon + # @return [Class] an object that conforms to the API of + # the standard coupon code handler class + # Spree::PromotionHandler::Coupon. + class_name_attribute :coupon_code_handler_class, default: 'Spree::PromotionHandler::Coupon' + add_nested_class_set :calculators, default: { "Spree::Promotion::Actions::CreateAdjustment" => %w[ Spree::Calculator::FlatPercentItemTotal diff --git a/core/spec/lib/spree/core/promotion_configuration_spec.rb b/core/spec/lib/spree/core/promotion_configuration_spec.rb index 00e3cba201f..a91c9e98d9b 100644 --- a/core/spec/lib/spree/core/promotion_configuration_spec.rb +++ b/core/spec/lib/spree/core/promotion_configuration_spec.rb @@ -3,8 +3,26 @@ require "rails_helper" RSpec.describe Spree::Core::PromotionConfiguration do + subject(:config) { described_class.new } + + it "uses base searcher class by default" do + expect(config.promotion_chooser_class).to eq Spree::PromotionChooser + end + + it "uses order adjustments recalculator class by default" do + expect(config.promotion_adjuster_class).to eq Spree::Promotion::OrderAdjustmentsRecalculator + end + + it "uses promotion handler coupon class by default" do + expect(config.coupon_code_handler_class).to eq Spree::PromotionHandler::Coupon + end + + it "uses promotion handler shipping class by default" do + expect(config.shipping_promotion_handler_class).to eq Spree::PromotionHandler::Shipping + end + describe "#calculators" do - subject { described_class.new.calculators[promotion_action] } + subject { config.calculators[promotion_action] } context "for Spree::Promotion::Actions::CreateAdjustment" do let(:promotion_action) { Spree::Promotion::Actions::CreateAdjustment }