diff --git a/app/models/solidus_friendly_promotions/discount_chooser.rb b/app/models/solidus_friendly_promotions/discount_chooser.rb deleted file mode 100644 index 5e478e84..00000000 --- a/app/models/solidus_friendly_promotions/discount_chooser.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module SolidusFriendlyPromotions - class DiscountChooser - attr_reader :item - - def initialize(item) - @item = item - end - - def call(discounts) - Array.wrap( - discounts.min_by do |discount| - [discount.amount, -discount.source&.id.to_i] - end - ) - end - end -end diff --git a/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/choose_discounts.rb b/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/choose_discounts.rb new file mode 100644 index 00000000..9121a47a --- /dev/null +++ b/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/choose_discounts.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module SolidusFriendlyPromotions + class FriendlyPromotionAdjuster + class ChooseDiscounts + attr_reader :discounts + + def initialize(discounts) + @discounts = discounts + end + + def call + Array.wrap( + discounts.min_by do |discount| + [discount.amount, -discount.source&.id.to_i] + end + ) + end + end + end +end diff --git a/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/discount_order.rb b/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/discount_order.rb index f342bedf..ed58011a 100644 --- a/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/discount_order.rb +++ b/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/discount_order.rb @@ -34,7 +34,7 @@ def adjust_line_items(promotions) line_item.variant.product.promotionable? end.map do |line_item| discounts = generate_discounts(promotions, line_item) - chosen_item_discounts = SolidusFriendlyPromotions.config.discount_chooser_class.new(line_item).call(discounts) + chosen_item_discounts = SolidusFriendlyPromotions.config.discount_chooser_class.new(discounts).call [line_item, chosen_item_discounts] end end @@ -42,7 +42,7 @@ def adjust_line_items(promotions) def adjust_shipments(promotions) order.shipments.map do |shipment| discounts = generate_discounts(promotions, shipment) - chosen_item_discounts = SolidusFriendlyPromotions.config.discount_chooser_class.new(shipment).call(discounts) + chosen_item_discounts = SolidusFriendlyPromotions.config.discount_chooser_class.new(discounts).call [shipment, chosen_item_discounts] end end @@ -50,7 +50,7 @@ def adjust_shipments(promotions) def adjust_shipping_rates(promotions) order.shipments.flat_map(&:shipping_rates).select(&:cost).map do |rate| discounts = generate_discounts(promotions, rate) - chosen_item_discounts = SolidusFriendlyPromotions.config.discount_chooser_class.new(rate).call(discounts) + chosen_item_discounts = SolidusFriendlyPromotions.config.discount_chooser_class.new(discounts).call [rate, chosen_item_discounts] end end diff --git a/lib/generators/solidus_friendly_promotions/install/templates/initializer.rb b/lib/generators/solidus_friendly_promotions/install/templates/initializer.rb index b5666f47..0632d153 100644 --- a/lib/generators/solidus_friendly_promotions/install/templates/initializer.rb +++ b/lib/generators/solidus_friendly_promotions/install/templates/initializer.rb @@ -58,7 +58,7 @@ SolidusFriendlyPromotions.configure do |config| # This class chooses which promotion should apply to a line item in case # that more than one promotion is eligible. - config.discount_chooser_class = "SolidusFriendlyPromotions::DiscountChooser" + config.discount_chooser_class = "SolidusFriendlyPromotions::FriendlyPromotionAdjuster::ChooseDiscounts" # How many promotions should be displayed on the index page in the admin. config.promotions_per_page = 25 diff --git a/spec/lib/solidus_friendly_promotions/configuration_spec.rb b/spec/lib/solidus_friendly_promotions/configuration_spec.rb index 136f18f1..83a0d105 100644 --- a/spec/lib/solidus_friendly_promotions/configuration_spec.rb +++ b/spec/lib/solidus_friendly_promotions/configuration_spec.rb @@ -15,7 +15,7 @@ describe ".promotion_chooser_class" do it "is the promotion chooser" do - expect(subject.discount_chooser_class).to eq(SolidusFriendlyPromotions::DiscountChooser) + expect(subject.discount_chooser_class).to eq(SolidusFriendlyPromotions::FriendlyPromotionAdjuster::ChooseDiscounts) end end diff --git a/spec/models/solidus_friendly_promotions/friendly_promotion_adjuster/choose_discounts_spec.rb b/spec/models/solidus_friendly_promotions/friendly_promotion_adjuster/choose_discounts_spec.rb new file mode 100644 index 00000000..b8357c05 --- /dev/null +++ b/spec/models/solidus_friendly_promotions/friendly_promotion_adjuster/choose_discounts_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe SolidusFriendlyPromotions::FriendlyPromotionAdjuster::ChooseDiscounts do + subject { described_class.new(discounts).call } + + let(:source_1) { create(:friendly_promotion, :with_adjustable_action).actions.first } + let(:source_2) { create(:friendly_promotion, :with_adjustable_action).actions.first } + let(:good_discount) { SolidusFriendlyPromotions::ItemDiscount.new(amount: -2, source: source_1) } + let(:bad_discount) { SolidusFriendlyPromotions::ItemDiscount.new(amount: -1, source: source_2) } + + let(:discounts) do + [ + good_discount, + bad_discount + ] + end + + it { is_expected.to contain_exactly(good_discount) } +end