Skip to content

Commit

Permalink
Refactor ChoosePromotions service class
Browse files Browse the repository at this point in the history
This class suffered of unnecessary variables, and can also live with the
service classes for the FriendlyPromotionAdjuster.

It also could use a spec.
  • Loading branch information
mamhoff committed Nov 6, 2023
1 parent fa1c440 commit 4403912
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 24 deletions.
19 changes: 0 additions & 19 deletions app/models/solidus_friendly_promotions/discount_chooser.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ 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

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

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/solidus_friendly_promotions/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4403912

Please sign in to comment.