Skip to content

Commit 1742f04

Browse files
authored
Merge pull request #6332 from mamhoff/extract-calculator-rounding
Refactor: Move rounding to promotion calculator module
2 parents 8ed18e0 + 02d04da commit 1742f04

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

promotions/app/models/concerns/solidus_promotions/calculators/promotion_calculator.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ module PromotionCalculator
66
def description
77
self.class.human_attribute_name(:description)
88
end
9+
10+
private
11+
12+
def round_to_currency(number, currency)
13+
currency_exponent = ::Money::Currency.find(currency).exponent
14+
number.round(currency_exponent)
15+
end
916
end
1017
end
1118
end

promotions/app/models/solidus_promotions/calculators/percent.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ class Percent < Spree::Calculator
1010
preference :percent, :decimal, default: 0
1111

1212
def compute(object)
13-
preferred_currency = object.order.currency
14-
currency_exponent = ::Money::Currency.find(preferred_currency).exponent
15-
(object.discountable_amount * preferred_percent / 100).round(currency_exponent)
13+
round_to_currency(object.discountable_amount * preferred_percent / 100, object.order.currency)
1614
end
1715
end
1816
end

promotions/app/models/solidus_promotions/calculators/tiered_percent.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ def compute_item(object)
3434
end
3535

3636
if preferred_currency.casecmp(order.currency).zero?
37-
currency_exponent = ::Money::Currency.find(preferred_currency).exponent
38-
(object.amount * (percent || preferred_base_percent) / 100).round(currency_exponent)
37+
round_to_currency(object.amount * (percent || preferred_base_percent) / 100, preferred_currency)
3938
else
4039
0
4140
end

promotions/app/models/solidus_promotions/calculators/tiered_percent_on_eligible_item_quantity.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ def compute_line_item(line_item)
2323
eligible_line_items_quantity_total(order) >= value
2424
end
2525
if preferred_currency.casecmp(order.currency).zero?
26-
currency_exponent = ::Money::Currency.find(preferred_currency).exponent
27-
(line_item.discountable_amount * (percent || preferred_base_percent) / 100).round(currency_exponent)
26+
round_to_currency(line_item.discountable_amount * (percent || preferred_base_percent) / 100, preferred_currency)
2827
else
2928
0
3029
end

0 commit comments

Comments
 (0)