From 74fd5bebe59d142344de7faf25f47a162cc03b04 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Mon, 2 Oct 2023 15:10:00 +0200 Subject: [PATCH] Add Spree::{LineItem,Shipment,ShippingRate}#discountable_amount This little module allows us to remove the delegators in a future commit. They are quite clunky to work with. --- .../line_item_decorator.rb | 7 ++++ .../shipment_decorator.rb | 7 ++++ .../shipping_rate_decorator.rb | 5 ++- .../discountable_amount.rb | 21 +++++++++++ .../spec/models/spree/line_item_spec.rb | 37 +++++++++++++++++++ .../spec/models/spree/shipment_spec.rb | 24 ++++++++++++ .../spec/models/spree/shipping_rate_spec.rb | 32 ++++++++++++++++ 7 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 friendly_promotions/app/decorators/models/solidus_friendly_promotions/line_item_decorator.rb create mode 100644 friendly_promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb create mode 100644 friendly_promotions/app/models/solidus_friendly_promotions/discountable_amount.rb create mode 100644 friendly_promotions/spec/models/spree/line_item_spec.rb create mode 100644 friendly_promotions/spec/models/spree/shipment_spec.rb diff --git a/friendly_promotions/app/decorators/models/solidus_friendly_promotions/line_item_decorator.rb b/friendly_promotions/app/decorators/models/solidus_friendly_promotions/line_item_decorator.rb new file mode 100644 index 00000000000..686d0686f48 --- /dev/null +++ b/friendly_promotions/app/decorators/models/solidus_friendly_promotions/line_item_decorator.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module SolidusFriendlyPromotions + module LineItemDecorator + Spree::LineItem.prepend SolidusFriendlyPromotions::DiscountableAmount + end +end diff --git a/friendly_promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb b/friendly_promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb new file mode 100644 index 00000000000..8772f6d53ae --- /dev/null +++ b/friendly_promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module SolidusFriendlyPromotions + module ShipmentDecorator + Spree::Shipment.prepend SolidusFriendlyPromotions::DiscountableAmount + end +end diff --git a/friendly_promotions/app/decorators/models/solidus_friendly_promotions/shipping_rate_decorator.rb b/friendly_promotions/app/decorators/models/solidus_friendly_promotions/shipping_rate_decorator.rb index 66cf3cf5cbf..b39a4c40a64 100644 --- a/friendly_promotions/app/decorators/models/solidus_friendly_promotions/shipping_rate_decorator.rb +++ b/friendly_promotions/app/decorators/models/solidus_friendly_promotions/shipping_rate_decorator.rb @@ -22,7 +22,8 @@ def total_before_tax def promo_total discounts.sum(&:amount) end + + Spree::ShippingRate.prepend SolidusFriendlyPromotions::DiscountableAmount + Spree::ShippingRate.prepend self end end - -Spree::ShippingRate.prepend(SolidusFriendlyPromotions::ShippingRateDecorator) diff --git a/friendly_promotions/app/models/solidus_friendly_promotions/discountable_amount.rb b/friendly_promotions/app/models/solidus_friendly_promotions/discountable_amount.rb new file mode 100644 index 00000000000..e67e60d6ee2 --- /dev/null +++ b/friendly_promotions/app/models/solidus_friendly_promotions/discountable_amount.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module SolidusFriendlyPromotions + module DiscountableAmount + def discountable_amount + amount + current_discounts.sum(&:amount) + end + + def current_discounts + @current_discounts ||= [] + end + + def current_discounts=(args) + @current_discounts = args + end + + def reset_current_discounts + @current_discounts = [] + end + end +end diff --git a/friendly_promotions/spec/models/spree/line_item_spec.rb b/friendly_promotions/spec/models/spree/line_item_spec.rb new file mode 100644 index 00000000000..44fc87336c9 --- /dev/null +++ b/friendly_promotions/spec/models/spree/line_item_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Spree::LineItem do + describe "#discountable_amount" do + let(:discounts) { [] } + let(:line_item) { Spree::LineItem.new(price: 10, quantity: 2, current_discounts: discounts) } + + subject(:discountable_amount) { line_item.discountable_amount } + + it { is_expected.to eq(20) } + + context "with a proposed discount" do + let(:discounts) do + [ + SolidusFriendlyPromotions::ItemDiscount.new(item: double, amount: -2, label: "Foo", source: double) + ] + end + + it { is_expected.to eq(18) } + end + end + + describe "#reset_current_discounts" do + let(:line_item) { Spree::LineItem.new } + + subject { line_item.reset_current_discounts } + before do + line_item.current_discounts << SolidusFriendlyPromotions::ItemDiscount.new(item: double, amount: -2, label: "Foo", source: double) + end + + it "resets the current discounts to an empty array" do + expect { subject }.to change { line_item.current_discounts.length }.from(1).to(0) + end + end +end diff --git a/friendly_promotions/spec/models/spree/shipment_spec.rb b/friendly_promotions/spec/models/spree/shipment_spec.rb new file mode 100644 index 00000000000..65133bd1c70 --- /dev/null +++ b/friendly_promotions/spec/models/spree/shipment_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe Spree::Shipment do + describe "#discountable_amount" do + let(:discounts) { [] } + let(:shipment) { Spree::Shipment.new(amount: 20, current_discounts: discounts) } + + subject(:discountable_amount) { shipment.discountable_amount } + + it { is_expected.to eq(20) } + + context "with a proposed discount" do + let(:discounts) do + [ + SolidusFriendlyPromotions::ItemDiscount.new(item: double, amount: -2, label: "Foo", source: double) + ] + end + + it { is_expected.to eq(18) } + end + end +end diff --git a/friendly_promotions/spec/models/spree/shipping_rate_spec.rb b/friendly_promotions/spec/models/spree/shipping_rate_spec.rb index 05a770710eb..63a12db3d92 100644 --- a/friendly_promotions/spec/models/spree/shipping_rate_spec.rb +++ b/friendly_promotions/spec/models/spree/shipping_rate_spec.rb @@ -43,4 +43,36 @@ it { is_expected.to eq(Spree::Money.new("0")) } end + + describe "#discountable_amount" do + let(:discounts) { [] } + let(:shipping_rate) { Spree::ShippingRate.new(amount: 20, current_discounts: discounts) } + + subject(:discountable_amount) { shipping_rate.discountable_amount } + + it { is_expected.to eq(20) } + + context "with a proposed discount" do + let(:discounts) do + [ + SolidusFriendlyPromotions::ItemDiscount.new(item: double, amount: -2, label: "Foo", source: double) + ] + end + + it { is_expected.to eq(18) } + end + end + + describe "#reset_current_discounts" do + let(:shipping_rate) { Spree::ShippingRate.new } + + subject { shipping_rate.reset_current_discounts } + before do + shipping_rate.current_discounts << SolidusFriendlyPromotions::ItemDiscount.new(item: double, amount: -2, label: "Foo", source: double) + end + + it "resets the current discounts to an empty array" do + expect { subject }.to change { shipping_rate.current_discounts.length }.from(1).to(0) + end + end end