From 0e5582f230da1f85d5e28ed4507b19f2c011288c Mon Sep 17 00:00:00 2001 From: "Ben A. Morgan" Date: Sun, 23 Jul 2023 10:50:45 -0600 Subject: [PATCH] no need to pass line_item when a has_one will do --- core/app/models/spree/unit_cancel.rb | 28 ++++++++++++---------- core/spec/models/spree/unit_cancel_spec.rb | 14 +++-------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/core/app/models/spree/unit_cancel.rb b/core/app/models/spree/unit_cancel.rb index 480f3fe3547..53cfd586c9e 100644 --- a/core/app/models/spree/unit_cancel.rb +++ b/core/app/models/spree/unit_cancel.rb @@ -8,7 +8,9 @@ class Spree::UnitCancel < Spree::Base DEFAULT_REASON = 'Cancel' belongs_to :inventory_unit, optional: true + has_one :adjustment, as: :source, dependent: :destroy + has_one :line_item, through: :inventory_unit validates :inventory_unit, presence: true @@ -16,9 +18,9 @@ class Spree::UnitCancel < Spree::Base def adjust! raise "Adjustment is already created" if adjustment - amount = compute_amount(inventory_unit.line_item) + amount = compute_amount - self.adjustment = inventory_unit.line_item.adjustments.create!( + self.adjustment = line_item.adjustments.create!( source: self, amount: amount, order: inventory_unit.order, @@ -27,27 +29,29 @@ def adjust! finalized: true ) - inventory_unit.line_item.order.recalculate + line_item.order.recalculate adjustment end # This method is used by Adjustment#update to recalculate the cost. - def compute_amount(line_item) - raise "Adjustable does not match line item" unless line_item == inventory_unit.line_item - - -weighted_line_item_amount(line_item) + def compute_amount + -weighted_amount(line_item.total_before_tax) end private - def weighted_line_item_amount(line_item) - quantity_of_line_item = quantity_of_line_item(line_item) - raise ZeroDivisionError, "Line Item does not have any inventory units available to cancel" if quantity_of_line_item.zero? + def weighted_amount(amount) + quantity = quantity_of_line_item + + if quantity.zero? + raise ZeroDivisionError, + 'Line Item does not have any inventory units available to cancel' + end - line_item.total_before_tax / quantity_of_line_item + amount / quantity end - def quantity_of_line_item(line_item) + def quantity_of_line_item BigDecimal(line_item.inventory_units.not_canceled.reject(&:original_return_item).size) end end diff --git a/core/spec/models/spree/unit_cancel_spec.rb b/core/spec/models/spree/unit_cancel_spec.rb index 139b8e2067d..51b493dab39 100644 --- a/core/spec/models/spree/unit_cancel_spec.rb +++ b/core/spec/models/spree/unit_cancel_spec.rb @@ -31,14 +31,14 @@ end describe '#compute_amount' do - subject { unit_cancel.compute_amount(line_item) } + subject(:amount) { unit_cancel.compute_amount } let(:line_item) { inventory_unit.line_item } - let!(:inventory_unit2) { create(:inventory_unit, line_item: inventory_unit.line_item) } + let!(:inventory_unit2) { create :inventory_unit, line_item: inventory_unit.line_item } context "all inventory on the line item are not canceled" do it "divides the line item total by the inventory units size" do - expect(subject).to eq(-5.0) + expect(amount).to eq(-5.0) end end @@ -55,14 +55,6 @@ end end - context "it is called with a line item that doesnt belong to the inventory unit" do - let(:line_item) { create(:line_item) } - - it "raises an error" do - expect { subject }.to raise_error RuntimeError, "Adjustable does not match line item" - end - end - context "multiple inventory units" do let(:quantity) { 4 } let(:order) { create(:order_with_line_items, line_items_attributes: [{ quantity: quantity }]) }