Skip to content

Commit

Permalink
Merge pull request solidusio#5753 from mamhoff/move-shipping-promotio…
Browse files Browse the repository at this point in the history
…n-handling-to-legacy-promotions

Move shipping promotion handling to legacy promotions
  • Loading branch information
kennyadsl authored May 21, 2024
2 parents 64f747e + 4a91b73 commit 098260d
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 25 deletions.
5 changes: 0 additions & 5 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,6 @@ def create_shipments_for_line_item(line_item)
end
end

def apply_shipping_promotions
Spree::Config.promotions.shipping_promotion_handler_class.new(self).activate
recalculate
end

# Clean shipments and make order back to address state (or to whatever state
# is set by restart_checkout_flow in case of state machine modifications)
def check_shipments_and_restart_checkout
Expand Down
2 changes: 2 additions & 0 deletions core/lib/spree/core/null_promotion_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class NullPromotionConfiguration < Spree::Preferences::Configuration
# the standard promotion finder class
# Spree::NullPromotionHandler.
class_name_attribute :shipping_promotion_handler_class, default: 'Spree::NullPromotionHandler'
deprecate :shipping_promotion_handler_class, deprecator: Spree.deprecator
deprecate :shipping_promotion_handler_class=, deprecator: Spree.deprecator

# Allows providing a different promotion advertiser.
# @!attribute [rw] advertiser_class
Expand Down
1 change: 0 additions & 1 deletion core/lib/spree/core/state_machines/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ def define_state_machine!
before_transition to: :delivery, do: :ensure_shipping_address
before_transition to: :delivery, do: :create_proposed_shipments
before_transition to: :delivery, do: :ensure_available_shipping_rates
before_transition from: :delivery, do: :apply_shipping_promotions
end

before_transition to: :resumed, do: :ensure_line_item_variants_are_not_deleted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
end

it "uses the null promotion handler as the shipping promo handler" do
expect(config.shipping_promotion_handler_class).to eq Spree::NullPromotionHandler
Spree.deprecator.silence do
expect(config.shipping_promotion_handler_class).to eq Spree::NullPromotionHandler
end
end

it "uses the null promotion advertiser class by default" do
Expand Down
6 changes: 0 additions & 6 deletions core/spec/models/spree/order/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,9 @@ def assert_state_changed(order, from, to)

before do
order.state = 'delivery'
allow(order).to receive(:apply_shipping_promotions)
allow(order).to receive(:ensure_available_shipping_rates) { true }
end

it "attempts to apply free shipping promotions" do
expect(order).to receive(:apply_shipping_promotions)
order.next!
end

context "with payment required" do
before do
allow(order).to receive_messages payment_required?: true
Expand Down
12 changes: 0 additions & 12 deletions core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -719,18 +719,6 @@ def merge!(other_order, user = nil)
end
end

context "#apply_shipping_promotions" do
it "calls out to the Shipping promotion handler" do
expect_any_instance_of(Spree::PromotionHandler::Shipping).to(
receive(:activate)
).and_call_original

expect(order.recalculator).to receive(:recalculate).and_call_original

order.apply_shipping_promotions
end
end

context "#products" do
before :each do
@variant1 = mock_model(Spree::Variant, product: "product1")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module SolidusLegacyPromotions
module OrderDecorator
def self.prepended(base)
base.state_machine do
if states[:delivery]
before_transition from: :delivery, do: :apply_shipping_promotions
end
end
end

def apply_shipping_promotions
::Spree::Config.promotions.shipping_promotion_handler_class.new(self).activate
recalculate
end

::Spree::Order.prepend(self)
end
end
21 changes: 21 additions & 0 deletions legacy_promotions/spec/models/spree/order/checkout_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Spree::Order, type: :model do
let!(:store) { create(:store) }
let(:order) { create(:order, store: store) }

context "from delivery", partial_double_verification: false do
before do
order.state = 'delivery'
allow(order).to receive(:apply_shipping_promotions)
allow(order).to receive(:ensure_available_shipping_rates) { true }
end

it "attempts to apply free shipping promotions" do
expect(order).to receive(:apply_shipping_promotions)
order.next!
end
end
end
19 changes: 19 additions & 0 deletions legacy_promotions/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Spree::Order do
let(:order) { create(:order) }

context "#apply_shipping_promotions" do
it "calls out to the Shipping promotion handler" do
expect_any_instance_of(Spree::PromotionHandler::Shipping).to(
receive(:activate)
).and_call_original

expect(order.recalculator).to receive(:recalculate).and_call_original

order.apply_shipping_promotions
end
end
end

0 comments on commit 098260d

Please sign in to comment.