From 0ade23442b6a609e977917f9006ac9b1fd92cc4b Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Mon, 2 Oct 2023 15:10:00 +0200 Subject: [PATCH 1/4] 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 +++++++++++++++++++ promotions/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 promotions/app/decorators/models/solidus_friendly_promotions/line_item_decorator.rb create mode 100644 promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb create mode 100644 promotions/app/models/solidus_friendly_promotions/discountable_amount.rb create mode 100644 promotions/spec/models/spree/line_item_spec.rb create mode 100644 promotions/spec/models/spree/shipment_spec.rb diff --git a/promotions/app/decorators/models/solidus_friendly_promotions/line_item_decorator.rb b/promotions/app/decorators/models/solidus_friendly_promotions/line_item_decorator.rb new file mode 100644 index 00000000..686d0686 --- /dev/null +++ b/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/promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb b/promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb new file mode 100644 index 00000000..8772f6d5 --- /dev/null +++ b/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/promotions/app/decorators/models/solidus_friendly_promotions/shipping_rate_decorator.rb b/promotions/app/decorators/models/solidus_friendly_promotions/shipping_rate_decorator.rb index 66cf3cf5..b39a4c40 100644 --- a/promotions/app/decorators/models/solidus_friendly_promotions/shipping_rate_decorator.rb +++ b/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/promotions/app/models/solidus_friendly_promotions/discountable_amount.rb b/promotions/app/models/solidus_friendly_promotions/discountable_amount.rb new file mode 100644 index 00000000..e67e60d6 --- /dev/null +++ b/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/promotions/spec/models/spree/line_item_spec.rb b/promotions/spec/models/spree/line_item_spec.rb new file mode 100644 index 00000000..44fc8733 --- /dev/null +++ b/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/promotions/spec/models/spree/shipment_spec.rb b/promotions/spec/models/spree/shipment_spec.rb new file mode 100644 index 00000000..65133bd1 --- /dev/null +++ b/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/promotions/spec/models/spree/shipping_rate_spec.rb b/promotions/spec/models/spree/shipping_rate_spec.rb index 05a77071..63a12db3 100644 --- a/promotions/spec/models/spree/shipping_rate_spec.rb +++ b/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 From 39df75374a35caf7d5e7c761c043614df86812df Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Mon, 2 Oct 2023 20:04:47 +0200 Subject: [PATCH 2/4] Provide API for resetting order discounts on order and shipment The order can be called to reset discounts on all leaf items. This is what does it. --- .../order_decorator.rb | 5 +++++ .../shipment_decorator.rb | 7 +++++++ promotions/spec/models/spree/order_spec.rb | 14 ++++++++++++++ promotions/spec/models/spree/shipment_spec.rb | 15 +++++++++++++++ 4 files changed, 41 insertions(+) diff --git a/promotions/app/decorators/models/solidus_friendly_promotions/order_decorator.rb b/promotions/app/decorators/models/solidus_friendly_promotions/order_decorator.rb index 23e13c39..a70817c8 100644 --- a/promotions/app/decorators/models/solidus_friendly_promotions/order_decorator.rb +++ b/promotions/app/decorators/models/solidus_friendly_promotions/order_decorator.rb @@ -20,6 +20,11 @@ def ensure_promotions_eligible super end + def reset_current_discounts + line_items.each(&:reset_current_discounts) + shipments.each(&:reset_current_discounts) + end + Spree::Order.prepend self end end diff --git a/promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb b/promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb index 8772f6d5..cca74442 100644 --- a/promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb +++ b/promotions/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb @@ -3,5 +3,12 @@ module SolidusFriendlyPromotions module ShipmentDecorator Spree::Shipment.prepend SolidusFriendlyPromotions::DiscountableAmount + + def reset_current_discounts + super + shipping_rates.each(&:reset_current_discounts) + end + + Spree::Shipment.prepend self end end diff --git a/promotions/spec/models/spree/order_spec.rb b/promotions/spec/models/spree/order_spec.rb index 51bfa5af..15ee8d76 100644 --- a/promotions/spec/models/spree/order_spec.rb +++ b/promotions/spec/models/spree/order_spec.rb @@ -5,4 +5,18 @@ RSpec.describe Spree::Order do it { is_expected.to have_many :friendly_promotions } it { is_expected.to have_many :friendly_order_promotions } + + describe "#reset_current_discounts" do + let(:line_item) { Spree::LineItem.new } + let(:shipment) { Spree::Shipment.new } + let(:order) { Spree::Order.new(shipments: [shipment], line_items: [line_item]) } + + subject { order.reset_current_discounts } + + it "resets the current discounts on all line items and shipments" do + expect(line_item).to receive(:reset_current_discounts) + expect(shipment).to receive(:reset_current_discounts) + subject + end + end end diff --git a/promotions/spec/models/spree/shipment_spec.rb b/promotions/spec/models/spree/shipment_spec.rb index 65133bd1..06a2f3df 100644 --- a/promotions/spec/models/spree/shipment_spec.rb +++ b/promotions/spec/models/spree/shipment_spec.rb @@ -20,5 +20,20 @@ it { is_expected.to eq(18) } end + + describe "#reset_current_discounts" do + let(:shipping_rate) { Spree::ShippingRate.new } + let(:shipment) { Spree::Shipment.new(shipping_rates: [shipping_rate]) } + + subject { shipment.reset_current_discounts } + before do + shipment.current_discounts << SolidusFriendlyPromotions::ItemDiscount.new(item: double, amount: -2, label: "Foo", source: double) + end + + it "resets the current discounts to an empty array and resets current discounts on all shipping rates" do + expect(shipping_rate).to receive(:reset_current_discounts) + expect { subject }.to change { shipment.current_discounts.length }.from(1).to(0) + end + end end end From 5bc7e5aab3a7b6e5fb7429cf7d645023a31e18ab Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Mon, 2 Oct 2023 20:05:31 +0200 Subject: [PATCH 3/4] Use Spree::{Order;LineItem;ShippingRate} in all models It's less change to all rules and actions if we just use the core Solidus models to store discounts. --- .../actions/adjust_line_item.rb | 2 +- .../actions/adjust_shipment.rb | 2 +- .../friendly_promotion_discounter.rb | 6 +++-- .../order_discounter.rb | 24 +++++++++---------- .../rules/first_order.rb | 2 +- .../rules/first_repeat_purchase_since.rb | 2 +- .../rules/item_total.rb | 2 +- .../rules/line_item_option_value.rb | 2 +- .../rules/line_item_product.rb | 2 +- .../rules/line_item_taxon.rb | 2 +- .../rules/nth_order.rb | 2 +- .../rules/one_use_per_user.rb | 2 +- .../rules/option_value.rb | 2 +- .../rules/product.rb | 2 +- .../rules/shipping_method.rb | 2 +- .../rules/store.rb | 2 +- .../rules/taxon.rb | 2 +- .../solidus_friendly_promotions/rules/user.rb | 2 +- .../rules/user_logged_in.rb | 2 +- .../rules/user_role.rb | 2 +- .../actions/adjust_shipment_spec.rb | 6 ++--- .../calculators/distributed_amount_spec.rb | 3 +-- .../calculators/percent_spec.rb | 2 +- .../calculators/tiered_flat_rate_spec.rb | 5 ++-- .../distributed_amounts_handler_spec.rb | 6 +---- .../promotion_action_spec.rb | 3 +-- .../rules/first_repeat_purchase_since_spec.rb | 4 ++-- .../rules/nth_order_spec.rb | 2 +- .../rules/option_value_spec.rb | 4 ++-- 29 files changed, 48 insertions(+), 53 deletions(-) diff --git a/promotions/app/models/solidus_friendly_promotions/actions/adjust_line_item.rb b/promotions/app/models/solidus_friendly_promotions/actions/adjust_line_item.rb index 0e2bc9ce..394d0b66 100644 --- a/promotions/app/models/solidus_friendly_promotions/actions/adjust_line_item.rb +++ b/promotions/app/models/solidus_friendly_promotions/actions/adjust_line_item.rb @@ -4,7 +4,7 @@ module SolidusFriendlyPromotions module Actions class AdjustLineItem < PromotionAction def can_discount?(object) - object.is_a? Discountable::LineItem + object.is_a? Spree::LineItem end def available_calculators diff --git a/promotions/app/models/solidus_friendly_promotions/actions/adjust_shipment.rb b/promotions/app/models/solidus_friendly_promotions/actions/adjust_shipment.rb index d52354ab..82d73533 100644 --- a/promotions/app/models/solidus_friendly_promotions/actions/adjust_shipment.rb +++ b/promotions/app/models/solidus_friendly_promotions/actions/adjust_shipment.rb @@ -4,7 +4,7 @@ module SolidusFriendlyPromotions module Actions class AdjustShipment < PromotionAction def can_discount?(object) - object.is_a?(Discountable::Shipment) || object.is_a?(Discountable::ShippingRate) + object.is_a?(Spree::Shipment) || object.is_a?(Spree::ShippingRate) end def available_calculators diff --git a/promotions/app/models/solidus_friendly_promotions/friendly_promotion_discounter.rb b/promotions/app/models/solidus_friendly_promotions/friendly_promotion_discounter.rb index e3945e1e..9c1d7b4e 100644 --- a/promotions/app/models/solidus_friendly_promotions/friendly_promotion_discounter.rb +++ b/promotions/app/models/solidus_friendly_promotions/friendly_promotion_discounter.rb @@ -5,13 +5,15 @@ class FriendlyPromotionDiscounter attr_reader :order, :promotions def initialize(order) - @order = Discountable::Order.new(order) + @order = order @promotions = PromotionEligibility.new(promotable: order, possible_promotions: possible_promotions).call end def call return nil if order.shipped? + order.reset_current_discounts + SolidusFriendlyPromotions::Promotion.ordered_lanes.each do |lane, _index| lane_promotions = promotions.select { |promotion| promotion.lane == lane } item_discounter = ItemDiscounter.new(promotions: lane_promotions) @@ -19,7 +21,7 @@ def call shipment_discounts = adjust_shipments(item_discounter) shipping_rate_discounts = adjust_shipping_rates(item_discounter) (line_item_discounts + shipment_discounts + shipping_rate_discounts).each do |item, chosen_discounts| - item.discounts.concat(chosen_discounts) + item.current_discounts.concat(chosen_discounts) end end diff --git a/promotions/app/models/solidus_friendly_promotions/order_discounter.rb b/promotions/app/models/solidus_friendly_promotions/order_discounter.rb index d0cfb204..7792c65a 100644 --- a/promotions/app/models/solidus_friendly_promotions/order_discounter.rb +++ b/promotions/app/models/solidus_friendly_promotions/order_discounter.rb @@ -9,19 +9,18 @@ def initialize(order) def call discountable_order = FriendlyPromotionDiscounter.new(order).call - discountable_order.line_items.each do |discountable_line_item| - update_adjustments(discountable_line_item.line_item, discountable_line_item.discounts) + discountable_order.line_items.each do |line_item| + update_adjustments(line_item, line_item.current_discounts) end - discountable_order.shipments.each do |discountable_shipment| - update_adjustments(discountable_shipment.shipment, discountable_shipment.discounts) + discountable_order.shipments.each do |shipment| + update_adjustments(shipment, shipment.current_discounts) end - discountable_order.shipments.flat_map(&:shipping_rates).each do |discountable_shipping_rate| - spree_shipping_rate = discountable_shipping_rate.shipping_rate - spree_shipping_rate.discounts = discountable_shipping_rate.discounts.map do |discount| + discountable_order.shipments.flat_map(&:shipping_rates).each do |shipping_rate| + shipping_rate.discounts = shipping_rate.current_discounts.map do |discount| SolidusFriendlyPromotions::ShippingRateDiscount.new( - shipping_rate: spree_shipping_rate, + shipping_rate: shipping_rate, amount: discount.amount, label: discount.label ) @@ -42,17 +41,18 @@ def call # # @private # @param [#adjustments] item a {Spree::LineItem} or {Spree::Shipment} - # @param [Array] taxed_items a list of calculated discounts for an item + # @param [Array] item_discounts a list of calculated discounts for an item # @return [void] - def update_adjustments(item, taxed_items) + def update_adjustments(item, item_discounts) promotion_adjustments = item.adjustments.select(&:friendly_promotion?) - active_adjustments = taxed_items.map do |tax_item| - update_adjustment(item, tax_item) + active_adjustments = item_discounts.map do |item_discount| + update_adjustment(item, item_discount) end item.update(promo_total: active_adjustments.sum(&:amount)) # Remove any tax adjustments tied to rates which no longer match. unmatched_adjustments = promotion_adjustments - active_adjustments + item.adjustments.destroy(unmatched_adjustments) end diff --git a/promotions/app/models/solidus_friendly_promotions/rules/first_order.rb b/promotions/app/models/solidus_friendly_promotions/rules/first_order.rb index 9d6f81f9..65ccd992 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/first_order.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/first_order.rb @@ -6,7 +6,7 @@ class FirstOrder < PromotionRule attr_reader :user, :email def applicable?(promotable) - promotable.is_a?(Discountable::Order) + promotable.is_a?(Spree::Order) end def eligible?(order, options = {}) diff --git a/promotions/app/models/solidus_friendly_promotions/rules/first_repeat_purchase_since.rb b/promotions/app/models/solidus_friendly_promotions/rules/first_repeat_purchase_since.rb index 2f64e454..dfca9645 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/first_repeat_purchase_since.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/first_repeat_purchase_since.rb @@ -8,7 +8,7 @@ class FirstRepeatPurchaseSince < PromotionRule # This promotion is applicable to orders only. def applicable?(promotable) - promotable.is_a?(Discountable::Order) + promotable.is_a?(Spree::Order) end # This is never eligible if the order does not have a user, and that user does not have any previous completed orders. diff --git a/promotions/app/models/solidus_friendly_promotions/rules/item_total.rb b/promotions/app/models/solidus_friendly_promotions/rules/item_total.rb index 55fc42ec..12df8523 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/item_total.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/item_total.rb @@ -27,7 +27,7 @@ def self.operator_options end def applicable?(promotable) - promotable.is_a?(Discountable::Order) + promotable.is_a?(Spree::Order) end def eligible?(order, _options = {}) diff --git a/promotions/app/models/solidus_friendly_promotions/rules/line_item_option_value.rb b/promotions/app/models/solidus_friendly_promotions/rules/line_item_option_value.rb index a914702a..b8252820 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/line_item_option_value.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/line_item_option_value.rb @@ -6,7 +6,7 @@ class LineItemOptionValue < PromotionRule preference :eligible_values, :hash def applicable?(promotable) - promotable.is_a?(Discountable::LineItem) + promotable.is_a?(Spree::LineItem) end def eligible?(line_item, _options = {}) diff --git a/promotions/app/models/solidus_friendly_promotions/rules/line_item_product.rb b/promotions/app/models/solidus_friendly_promotions/rules/line_item_product.rb index fb16c2fd..b24e8e55 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/line_item_product.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/line_item_product.rb @@ -17,7 +17,7 @@ class LineItemProduct < PromotionRule preference :match_policy, :string, default: MATCH_POLICIES.first def applicable?(promotable) - promotable.is_a?(Discountable::LineItem) + promotable.is_a?(Spree::LineItem) end def eligible?(line_item, _options = {}) diff --git a/promotions/app/models/solidus_friendly_promotions/rules/line_item_taxon.rb b/promotions/app/models/solidus_friendly_promotions/rules/line_item_taxon.rb index 204a12e2..e0fd27b6 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/line_item_taxon.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/line_item_taxon.rb @@ -13,7 +13,7 @@ class LineItemTaxon < PromotionRule preference :match_policy, :string, default: MATCH_POLICIES.first def applicable?(promotable) - promotable.is_a?(Discountable::LineItem) + promotable.is_a?(Spree::LineItem) end def eligible?(line_item, _options = {}) diff --git a/promotions/app/models/solidus_friendly_promotions/rules/nth_order.rb b/promotions/app/models/solidus_friendly_promotions/rules/nth_order.rb index 041cbc53..04cb592b 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/nth_order.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/nth_order.rb @@ -10,7 +10,7 @@ class NthOrder < PromotionRule # This promotion is applicable to orders only. def applicable?(promotable) - promotable.is_a?(Discountable::Order) + promotable.is_a?(Spree::Order) end # This is never eligible if the order does not have a user, and that user does not have any previous completed orders. diff --git a/promotions/app/models/solidus_friendly_promotions/rules/one_use_per_user.rb b/promotions/app/models/solidus_friendly_promotions/rules/one_use_per_user.rb index 8393c1ff..5c9d13a8 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/one_use_per_user.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/one_use_per_user.rb @@ -4,7 +4,7 @@ module SolidusFriendlyPromotions module Rules class OneUsePerUser < PromotionRule def applicable?(promotable) - promotable.is_a?(Discountable::Order) + promotable.is_a?(Spree::Order) end def eligible?(order, _options = {}) diff --git a/promotions/app/models/solidus_friendly_promotions/rules/option_value.rb b/promotions/app/models/solidus_friendly_promotions/rules/option_value.rb index 9cd76948..f8b25aa0 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/option_value.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/option_value.rb @@ -6,7 +6,7 @@ class OptionValue < PromotionRule preference :eligible_values, :hash def applicable?(promotable) - promotable.is_a?(Discountable::Order) + promotable.is_a?(Spree::Order) end def eligible?(order, _options = {}) diff --git a/promotions/app/models/solidus_friendly_promotions/rules/product.rb b/promotions/app/models/solidus_friendly_promotions/rules/product.rb index 02fadb69..c7a5d837 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/product.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/product.rb @@ -29,7 +29,7 @@ def eligible_products end def applicable?(promotable) - promotable.is_a?(Discountable::Order) + promotable.is_a?(Spree::Order) end def eligible?(order, _options = {}) diff --git a/promotions/app/models/solidus_friendly_promotions/rules/shipping_method.rb b/promotions/app/models/solidus_friendly_promotions/rules/shipping_method.rb index dce6d5af..eb745f76 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/shipping_method.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/shipping_method.rb @@ -6,7 +6,7 @@ class ShippingMethod < PromotionRule preference :shipping_method_ids, type: :array, default: [] def applicable?(promotable) - promotable.is_a?(Discountable::Shipment) || promotable.is_a?(Discountable::ShippingRate) + promotable.is_a?(Spree::Shipment) || promotable.is_a?(Spree::ShippingRate) end def eligible?(promotable) diff --git a/promotions/app/models/solidus_friendly_promotions/rules/store.rb b/promotions/app/models/solidus_friendly_promotions/rules/store.rb index 182e9f49..3c087849 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/store.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/store.rb @@ -13,7 +13,7 @@ def preload_relations end def applicable?(promotable) - promotable.is_a?(Discountable::Order) + promotable.is_a?(Spree::Order) end def eligible?(order, _options = {}) diff --git a/promotions/app/models/solidus_friendly_promotions/rules/taxon.rb b/promotions/app/models/solidus_friendly_promotions/rules/taxon.rb index 9f20c285..9130cadc 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/taxon.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/taxon.rb @@ -17,7 +17,7 @@ def preload_relations preference :match_policy, :string, default: MATCH_POLICIES.first def applicable?(promotable) - promotable.is_a?(Discountable::Order) + promotable.is_a?(Spree::Order) end def eligible?(order, _options = {}) diff --git a/promotions/app/models/solidus_friendly_promotions/rules/user.rb b/promotions/app/models/solidus_friendly_promotions/rules/user.rb index 7c5a170f..1cb7e9fd 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/user.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/user.rb @@ -14,7 +14,7 @@ def preload_relations end def applicable?(promotable) - promotable.is_a?(Discountable::Order) + promotable.is_a?(Spree::Order) end def eligible?(order, _options = {}) diff --git a/promotions/app/models/solidus_friendly_promotions/rules/user_logged_in.rb b/promotions/app/models/solidus_friendly_promotions/rules/user_logged_in.rb index a866884b..5be6e2ea 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/user_logged_in.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/user_logged_in.rb @@ -4,7 +4,7 @@ module SolidusFriendlyPromotions module Rules class UserLoggedIn < PromotionRule def applicable?(promotable) - promotable.is_a?(Discountable::Order) + promotable.is_a?(Spree::Order) end def eligible?(order, _options = {}) diff --git a/promotions/app/models/solidus_friendly_promotions/rules/user_role.rb b/promotions/app/models/solidus_friendly_promotions/rules/user_role.rb index 08354c58..e8df12f7 100644 --- a/promotions/app/models/solidus_friendly_promotions/rules/user_role.rb +++ b/promotions/app/models/solidus_friendly_promotions/rules/user_role.rb @@ -9,7 +9,7 @@ class UserRole < PromotionRule preference :match_policy, default: MATCH_POLICIES.first def applicable?(promotable) - promotable.is_a?(Discountable::Order) + promotable.is_a?(Spree::Order) end def eligible?(order, _options = {}) diff --git a/promotions/spec/models/solidus_friendly_promotions/actions/adjust_shipment_spec.rb b/promotions/spec/models/solidus_friendly_promotions/actions/adjust_shipment_spec.rb index 887349b3..eb1709de 100644 --- a/promotions/spec/models/solidus_friendly_promotions/actions/adjust_shipment_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/actions/adjust_shipment_spec.rb @@ -15,19 +15,19 @@ subject { action.can_discount?(promotable) } context "with a line item" do - let(:promotable) { SolidusFriendlyPromotions::Discountable::LineItem.new(Spree::Order.new, order: double) } + let(:promotable) { Spree::Order.new } it { is_expected.to be false } end context "with a shipment" do - let(:promotable) { SolidusFriendlyPromotions::Discountable::Shipment.new(Spree::Shipment.new, order: double) } + let(:promotable) { Spree::Shipment.new } it { is_expected.to be true } end context "with a shipping rate" do - let(:promotable) { SolidusFriendlyPromotions::Discountable::ShippingRate.new(Spree::ShippingRate.new, shipment: double) } + let(:promotable) { Spree::ShippingRate.new } it { is_expected.to be true } end diff --git a/promotions/spec/models/solidus_friendly_promotions/calculators/distributed_amount_spec.rb b/promotions/spec/models/solidus_friendly_promotions/calculators/distributed_amount_spec.rb index 1f4cc920..65aca311 100644 --- a/promotions/spec/models/solidus_friendly_promotions/calculators/distributed_amount_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/calculators/distributed_amount_spec.rb @@ -10,8 +10,7 @@ end let(:rules) { [] } let(:action) { SolidusFriendlyPromotions::Actions::AdjustLineItem.create(calculator: calculator) } - let(:spree_order) { create(:order_with_line_items, line_items_attributes: line_items_attributes) } - let(:order) { SolidusFriendlyPromotions::Discountable::Order.new(spree_order) } + let(:order) { create(:order_with_line_items, line_items_attributes: line_items_attributes) } let(:currency) { "USD" } context "applied to an order" do diff --git a/promotions/spec/models/solidus_friendly_promotions/calculators/percent_spec.rb b/promotions/spec/models/solidus_friendly_promotions/calculators/percent_spec.rb index fe98125d..025c3578 100644 --- a/promotions/spec/models/solidus_friendly_promotions/calculators/percent_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/calculators/percent_spec.rb @@ -7,7 +7,7 @@ context "compute" do let(:currency) { "USD" } let(:order) { double(currency: currency) } - let(:line_item) { double("SolidusFriendlyPromotions::Discountable::LineItem", discountable_amount: 100, order: order) } + let(:line_item) { double("Spree::LineItem", discountable_amount: 100, order: order) } before { subject.preferred_percent = 15 } diff --git a/promotions/spec/models/solidus_friendly_promotions/calculators/tiered_flat_rate_spec.rb b/promotions/spec/models/solidus_friendly_promotions/calculators/tiered_flat_rate_spec.rb index 490dac04..ee438e6c 100644 --- a/promotions/spec/models/solidus_friendly_promotions/calculators/tiered_flat_rate_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/calculators/tiered_flat_rate_spec.rb @@ -75,7 +75,7 @@ line_items_price: amount ) end - let(:line_item) { SolidusFriendlyPromotions::Discountable::LineItem.new(order.line_items.first, order: order) } + let(:line_item) { order.line_items.first } let(:preferred_currency) { "USD" } before do @@ -109,8 +109,7 @@ context "with a shipment" do subject { calculator.compute(shipment) } - let(:spree_shipment) { Spree::Shipment.new(order: order, amount: shipping_cost) } - let(:shipment) { SolidusFriendlyPromotions::Discountable::Shipment.new(spree_shipment, order: order) } + let(:shipment) { Spree::Shipment.new(order: order, amount: shipping_cost) } let(:line_item_count) { 1 } let(:amount) { 10 } diff --git a/promotions/spec/models/solidus_friendly_promotions/distributed_amounts_handler_spec.rb b/promotions/spec/models/solidus_friendly_promotions/distributed_amounts_handler_spec.rb index ee267f90..f3d24490 100644 --- a/promotions/spec/models/solidus_friendly_promotions/distributed_amounts_handler_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/distributed_amounts_handler_spec.rb @@ -3,17 +3,13 @@ require "spec_helper" RSpec.describe SolidusFriendlyPromotions::DistributedAmountsHandler, type: :model do - let(:spree_order) do + let(:order) do FactoryBot.create( :order_with_line_items, line_items_attributes: line_items_attributes ) end - let(:order) do - SolidusFriendlyPromotions::Discountable::Order.new(spree_order) - end - let(:handler) { described_class.new(order.line_items, total_amount) } diff --git a/promotions/spec/models/solidus_friendly_promotions/promotion_action_spec.rb b/promotions/spec/models/solidus_friendly_promotions/promotion_action_spec.rb index 940f8060..64f039db 100644 --- a/promotions/spec/models/solidus_friendly_promotions/promotion_action_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/promotion_action_spec.rb @@ -22,8 +22,7 @@ let(:variant) { create(:variant) } let(:order) { create(:order) } - let(:discountable) { SolidusFriendlyPromotions::Discountable::LineItem.new(line_item, order: SolidusFriendlyPromotions::Discountable::Order.new(order)) } - let(:line_item) { Spree::LineItem.new(order: order, variant: variant, price: 10) } + let(:discountable) { Spree::LineItem.new(order: order, variant: variant, price: 10) } let(:promotion) { SolidusFriendlyPromotions::Promotion.new(name: "20 Perzent off") } let(:action) { described_class.new(promotion: promotion) } diff --git a/promotions/spec/models/solidus_friendly_promotions/rules/first_repeat_purchase_since_spec.rb b/promotions/spec/models/solidus_friendly_promotions/rules/first_repeat_purchase_since_spec.rb index 8edd838e..e23fd17e 100644 --- a/promotions/spec/models/solidus_friendly_promotions/rules/first_repeat_purchase_since_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/rules/first_repeat_purchase_since_spec.rb @@ -7,13 +7,13 @@ subject { described_class.new.applicable?(promotable) } context "when the promotable is an order" do - let(:promotable) { SolidusFriendlyPromotions::Discountable::Order.new(Spree::Order.new) } + let(:promotable) { Spree::Order.new } it { is_expected.to be true } end context "when the promotable is not a order" do - let(:promotable) { SolidusFriendlyPromotions::Discountable::LineItem.new(Spree::LineItem.new, order: double) } + let(:promotable) { Spree::LineItem.new } it { is_expected.to be false } end diff --git a/promotions/spec/models/solidus_friendly_promotions/rules/nth_order_spec.rb b/promotions/spec/models/solidus_friendly_promotions/rules/nth_order_spec.rb index a85d4fec..d2464d35 100644 --- a/promotions/spec/models/solidus_friendly_promotions/rules/nth_order_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/rules/nth_order_spec.rb @@ -7,7 +7,7 @@ subject { described_class.new.applicable?(promotable) } context "when the promotable is an order" do - let(:promotable) { SolidusFriendlyPromotions::Discountable::Order.new(Spree::Order.new) } + let(:promotable) { Spree::Order.new } it { is_expected.to be true } end diff --git a/promotions/spec/models/solidus_friendly_promotions/rules/option_value_spec.rb b/promotions/spec/models/solidus_friendly_promotions/rules/option_value_spec.rb index 4e29b4c1..51930937 100644 --- a/promotions/spec/models/solidus_friendly_promotions/rules/option_value_spec.rb +++ b/promotions/spec/models/solidus_friendly_promotions/rules/option_value_spec.rb @@ -18,13 +18,13 @@ subject { rule.applicable?(promotable) } context "when promotable is an order" do - let(:promotable) { SolidusFriendlyPromotions::Discountable::Order.new(Spree::Order.new) } + let(:promotable) { Spree::Order.new } it { is_expected.to be true } end context "when promotable is not an order" do - let(:promotable) { SolidusFriendlyPromotions::Discountable::LineItem.new(Spree::LineItem.new, order: double) } + let(:promotable) { Spree::LineItem.new } it { is_expected.to be false } end From b79111deae45e24e0054611f6cd2dffa8b86c4ce Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Mon, 2 Oct 2023 20:06:56 +0200 Subject: [PATCH 4/4] Remove Delegators We don't need these delegators, as they are somewhat unwieldy. --- .../discountable/line_item.rb | 23 ---------- .../discountable/order.rb | 19 -------- .../discountable/shipment.rb | 24 ---------- .../discountable/shipping_rate.rb | 23 ---------- .../discountable/line_item_spec.rb | 34 -------------- .../discountable/order_spec.rb | 39 ---------------- .../discountable/shipment_spec.rb | 45 ------------------- .../discountable/shipping_rate_spec.rb | 35 --------------- 8 files changed, 242 deletions(-) delete mode 100644 promotions/app/models/solidus_friendly_promotions/discountable/line_item.rb delete mode 100644 promotions/app/models/solidus_friendly_promotions/discountable/order.rb delete mode 100644 promotions/app/models/solidus_friendly_promotions/discountable/shipment.rb delete mode 100644 promotions/app/models/solidus_friendly_promotions/discountable/shipping_rate.rb delete mode 100644 promotions/spec/models/solidus_friendly_promotions/discountable/line_item_spec.rb delete mode 100644 promotions/spec/models/solidus_friendly_promotions/discountable/order_spec.rb delete mode 100644 promotions/spec/models/solidus_friendly_promotions/discountable/shipment_spec.rb delete mode 100644 promotions/spec/models/solidus_friendly_promotions/discountable/shipping_rate_spec.rb diff --git a/promotions/app/models/solidus_friendly_promotions/discountable/line_item.rb b/promotions/app/models/solidus_friendly_promotions/discountable/line_item.rb deleted file mode 100644 index f732710d..00000000 --- a/promotions/app/models/solidus_friendly_promotions/discountable/line_item.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -module SolidusFriendlyPromotions - module Discountable - class LineItem < SimpleDelegator - attr_reader :discounts, :order - - def initialize(line_item, order:) - super(line_item) - @order = order - @discounts = [] - end - - def line_item - __getobj__ - end - - def discountable_amount - amount + discounts.sum(&:amount) - end - end - end -end diff --git a/promotions/app/models/solidus_friendly_promotions/discountable/order.rb b/promotions/app/models/solidus_friendly_promotions/discountable/order.rb deleted file mode 100644 index ef2bd8c1..00000000 --- a/promotions/app/models/solidus_friendly_promotions/discountable/order.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module SolidusFriendlyPromotions - module Discountable - class Order < SimpleDelegator - attr_reader :line_items, :shipments - - def initialize(order) - super - @line_items = order.line_items.map { |line_item| LineItem.new(line_item, order: self) } - @shipments = order.shipments.map { |shipment| Shipment.new(shipment, order: self) } - end - - def order - __getobj__ - end - end - end -end diff --git a/promotions/app/models/solidus_friendly_promotions/discountable/shipment.rb b/promotions/app/models/solidus_friendly_promotions/discountable/shipment.rb deleted file mode 100644 index e16c5a96..00000000 --- a/promotions/app/models/solidus_friendly_promotions/discountable/shipment.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -module SolidusFriendlyPromotions - module Discountable - class Shipment < SimpleDelegator - attr_reader :discounts, :shipping_rates, :order - - def initialize(shipment, order:) - super(shipment) - @order = order - @discounts = [] - @shipping_rates = shipment.shipping_rates.map { |shipping_rate| ShippingRate.new(shipping_rate, shipment: self) } - end - - def shipment - __getobj__ - end - - def discountable_amount - amount + discounts.sum(&:amount) - end - end - end -end diff --git a/promotions/app/models/solidus_friendly_promotions/discountable/shipping_rate.rb b/promotions/app/models/solidus_friendly_promotions/discountable/shipping_rate.rb deleted file mode 100644 index 77a6ca46..00000000 --- a/promotions/app/models/solidus_friendly_promotions/discountable/shipping_rate.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -module SolidusFriendlyPromotions - module Discountable - class ShippingRate < SimpleDelegator - attr_reader :discounts, :shipment - - def initialize(shipping_rate, shipment:) - super(shipping_rate) - @shipment = shipment - @discounts = [] - end - - def shipping_rate - __getobj__ - end - - def discountable_amount - amount + discounts.sum(&:amount) - end - end - end -end diff --git a/promotions/spec/models/solidus_friendly_promotions/discountable/line_item_spec.rb b/promotions/spec/models/solidus_friendly_promotions/discountable/line_item_spec.rb deleted file mode 100644 index c107b821..00000000 --- a/promotions/spec/models/solidus_friendly_promotions/discountable/line_item_spec.rb +++ /dev/null @@ -1,34 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -RSpec.describe SolidusFriendlyPromotions::Discountable::LineItem do - let(:discountable_order) { double(SolidusFriendlyPromotions::Discountable::Order) } - let(:spree_line_item) { build(:line_item, price: 10, quantity: 2) } - - subject(:discountable_line_item) { described_class.new(spree_line_item, order: discountable_order) } - - describe "#order" do - subject { discountable_line_item.order } - - it { is_expected.to eq(discountable_order) } - end - - describe "#discountable_amount" do - subject(:discountable_amount) { discountable_line_item.discountable_amount } - - context "with no discounts" do - it { is_expected.to eq(20) } - end - - context "with discounts" do - let(:discount) { SolidusFriendlyPromotions::ItemDiscount.new(amount: -4) } - - before do - discountable_line_item.discounts << discount - end - - it { is_expected.to eq(16) } - end - end -end diff --git a/promotions/spec/models/solidus_friendly_promotions/discountable/order_spec.rb b/promotions/spec/models/solidus_friendly_promotions/discountable/order_spec.rb deleted file mode 100644 index 637b8a05..00000000 --- a/promotions/spec/models/solidus_friendly_promotions/discountable/order_spec.rb +++ /dev/null @@ -1,39 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -RSpec.describe SolidusFriendlyPromotions::Discountable::Order do - subject(:discountable_order) { described_class.new(spree_order) } - - let(:spree_order) { Spree::Order.new } - - describe "#line_items" do - let(:spree_order) { create(:order_with_line_items) } - subject(:line_items) { discountable_order.line_items } - - specify "are converted into Discountable Line Items" do - line_items.each do |line_item| - expect(line_item).to be_a(SolidusFriendlyPromotions::Discountable::LineItem) - end - end - end - - describe "#shipments" do - let(:spree_order) { create(:order_ready_to_ship) } - subject(:shipments) { discountable_order.shipments } - - specify "are converted into Discountable Shipments" do - shipments.each do |shipment| - expect(shipment).to be_a(SolidusFriendlyPromotions::Discountable::Shipment) - end - end - end - - describe "delegation" do - let(:spree_order) { Spree::Order.new(email: "yoda@example.com") } - - it "forwards order attributes" do - expect(subject.email).to eq("yoda@example.com") - end - end -end diff --git a/promotions/spec/models/solidus_friendly_promotions/discountable/shipment_spec.rb b/promotions/spec/models/solidus_friendly_promotions/discountable/shipment_spec.rb deleted file mode 100644 index 96e58f1f..00000000 --- a/promotions/spec/models/solidus_friendly_promotions/discountable/shipment_spec.rb +++ /dev/null @@ -1,45 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -RSpec.describe SolidusFriendlyPromotions::Discountable::Shipment do - let(:discountable_order) { double(SolidusFriendlyPromotions::Discountable::Order) } - - let(:spree_shipment) { build(:shipment, amount: 20) } - - subject(:discountable_shipment) { described_class.new(spree_shipment, order: discountable_order) } - - describe "#order" do - subject { discountable_shipment.order } - - it { is_expected.to eq(discountable_order) } - end - - describe "#discountable_amount" do - subject(:discountable_amount) { discountable_shipment.discountable_amount } - - context "with no discounts" do - it { is_expected.to eq(20) } - end - - context "with discounts" do - let(:discount) { SolidusFriendlyPromotions::ItemDiscount.new(amount: -4) } - - before do - discountable_shipment.discounts << discount - end - - it { is_expected.to eq(16) } - end - end - - describe "#shipping_rates" do - subject(:shipping_rates) { discountable_shipment.shipping_rates } - - specify "are converted into Discountable Shipments" do - shipping_rates.each do |shipping_rate| - expect(shipping_rate).to be_a(SolidusFriendlyPromotions::Discountable::ShippingRate) - end - end - end -end diff --git a/promotions/spec/models/solidus_friendly_promotions/discountable/shipping_rate_spec.rb b/promotions/spec/models/solidus_friendly_promotions/discountable/shipping_rate_spec.rb deleted file mode 100644 index f6c6d778..00000000 --- a/promotions/spec/models/solidus_friendly_promotions/discountable/shipping_rate_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -RSpec.describe SolidusFriendlyPromotions::Discountable::ShippingRate do - let(:discountable_shipment) { double(SolidusFriendlyPromotions::Discountable::Shipment) } - - let(:spree_shipping_rate) { build(:shipping_rate, amount: 20) } - - subject(:discountable_shipping_rate) { described_class.new(spree_shipping_rate, shipment: discountable_shipment) } - - describe "#shipment" do - subject { discountable_shipping_rate.shipment } - - it { is_expected.to eq(discountable_shipment) } - end - - describe "#discountable_amount" do - subject(:discountable_amount) { discountable_shipping_rate.discountable_amount } - - context "with no discounts" do - it { is_expected.to eq(20) } - end - - context "with discounts" do - let(:discount) { SolidusFriendlyPromotions::ItemDiscount.new(amount: -4) } - - before do - discountable_shipping_rate.discounts << discount - end - - it { is_expected.to eq(16) } - end - end -end