Skip to content

Commit 89d91bd

Browse files
committed
no need to pass line_item when a has_one will do
1 parent 568f4ed commit 89d91bd

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

core/app/models/spree/unit_cancel.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ class Spree::UnitCancel < Spree::Base
88
DEFAULT_REASON = 'Cancel'
99

1010
belongs_to :inventory_unit, optional: true
11+
1112
has_one :adjustment, as: :source, dependent: :destroy
13+
has_one :line_item, through: :inventory_unit
1214

1315
validates :inventory_unit, presence: true
1416

1517
# Creates necessary cancel adjustments for the line item.
1618
def adjust!
1719
raise "Adjustment is already created" if adjustment
1820

19-
amount = compute_amount(inventory_unit.line_item)
21+
amount = compute_amount
2022

21-
self.adjustment = inventory_unit.line_item.adjustments.create!(
23+
self.adjustment = line_item.adjustments.create!(
2224
source: self,
2325
amount: amount,
2426
order: inventory_unit.order,
@@ -27,27 +29,29 @@ def adjust!
2729
finalized: true
2830
)
2931

30-
inventory_unit.line_item.order.recalculate
32+
line_item.order.recalculate
3133
adjustment
3234
end
3335

3436
# This method is used by Adjustment#update to recalculate the cost.
35-
def compute_amount(line_item)
36-
raise "Adjustable does not match line item" unless line_item == inventory_unit.line_item
37-
38-
-weighted_line_item_amount(line_item)
37+
def compute_amount
38+
-weighted_amount(line_item.total_before_tax)
3939
end
4040

4141
private
4242

43-
def weighted_line_item_amount(line_item)
44-
quantity_of_line_item = quantity_of_line_item(line_item)
45-
raise ZeroDivisionError, "Line Item does not have any inventory units available to cancel" if quantity_of_line_item.zero?
43+
def weighted_amount(amount)
44+
quantity_of_line_item = quantity_of_line_item
45+
46+
if quantity_of_line_item.zero?
47+
raise ZeroDivisionError,
48+
'Line Item does not have any inventory units available to cancel'
49+
end
4650

47-
line_item.total_before_tax / quantity_of_line_item
51+
amount / quantity_of_line_item
4852
end
4953

50-
def quantity_of_line_item(line_item)
54+
def quantity_of_line_item
5155
BigDecimal(line_item.inventory_units.not_canceled.reject(&:original_return_item).size)
5256
end
5357
end

0 commit comments

Comments
 (0)