Skip to content

Commit 0e5582f

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

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
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 = quantity_of_line_item
45+
46+
if quantity.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
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

core/spec/models/spree/unit_cancel_spec.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
end
3232

3333
describe '#compute_amount' do
34-
subject { unit_cancel.compute_amount(line_item) }
34+
subject(:amount) { unit_cancel.compute_amount }
3535

3636
let(:line_item) { inventory_unit.line_item }
37-
let!(:inventory_unit2) { create(:inventory_unit, line_item: inventory_unit.line_item) }
37+
let!(:inventory_unit2) { create :inventory_unit, line_item: inventory_unit.line_item }
3838

3939
context "all inventory on the line item are not canceled" do
4040
it "divides the line item total by the inventory units size" do
41-
expect(subject).to eq(-5.0)
41+
expect(amount).to eq(-5.0)
4242
end
4343
end
4444

@@ -55,14 +55,6 @@
5555
end
5656
end
5757

58-
context "it is called with a line item that doesnt belong to the inventory unit" do
59-
let(:line_item) { create(:line_item) }
60-
61-
it "raises an error" do
62-
expect { subject }.to raise_error RuntimeError, "Adjustable does not match line item"
63-
end
64-
end
65-
6658
context "multiple inventory units" do
6759
let(:quantity) { 4 }
6860
let(:order) { create(:order_with_line_items, line_items_attributes: [{ quantity: quantity }]) }

0 commit comments

Comments
 (0)