Merge pull request #4035 from kristinalim/fix/4033-remove_line_item_adjustments_when_line_item_removed

4033 Remove line item adjustments when line item removed
This commit is contained in:
Luis Ramos
2019-07-16 09:54:33 +01:00
committed by GitHub
3 changed files with 39 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
module LineItemBasedAdjustmentHandling
extend ActiveSupport::Concern
included do
has_many :adjustments_for_which_source, class_name: "Spree::Adjustment", as: :source,
dependent: :destroy
end
end

View File

@@ -3,6 +3,7 @@ require 'open_food_network/variant_and_line_item_naming'
Spree::LineItem.class_eval do
include OpenFoodNetwork::VariantAndLineItemNaming
include LineItemBasedAdjustmentHandling
has_and_belongs_to_many :option_values, join_table: 'spree_option_values_line_items', class_name: 'Spree::OptionValue'
# Redefining here to add the inverse_of option

View File

@@ -366,6 +366,8 @@ describe Spree::Order do
before do
order.add_variant v1
order.add_variant v2
order.update_distribution_charge!
end
it "removes the variant's line item" do
@@ -378,6 +380,34 @@ describe Spree::Order do
order.remove_variant v3
end.to change(order.line_items(:reload), :count).by(0)
end
context "when the item has an associated adjustment" do
let(:distributor) { create(:distributor_enterprise) }
let(:order_cycle) do
create(:order_cycle).tap do |record|
create(:exchange, variants: [v1], incoming: true)
create(:exchange, variants: [v1], incoming: false, receiver: distributor)
end
end
let(:order) { create(:order, distributor: distributor, order_cycle: order_cycle) }
it "removes the variant's line item" do
order.remove_variant v1
expect(order.line_items(:reload).map(&:variant)).to eq([v2])
end
it "removes the variant's adjustment" do
line_item = order.line_items.where(variant_id: v1.id).first
adjustment_scope = Spree::Adjustment.where(source_type: "Spree::LineItem",
source_id: line_item.id)
expect(adjustment_scope.count).to eq(1)
adjustment = adjustment_scope.first
order.remove_variant v1
expect { adjustment.reload }.to raise_error
end
end
end
describe "emptying the order" do