-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ChoosePromotions service class
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
Showing
6 changed files
with
47 additions
and
24 deletions.
There are no files selected for viewing
19 changes: 0 additions & 19 deletions
19
app/models/solidus_friendly_promotions/discount_chooser.rb
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
app/models/solidus_friendly_promotions/friendly_promotion_adjuster/choose_discounts.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
spec/models/solidus_friendly_promotions/friendly_promotion_adjuster/choose_discounts_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |