From 2c279fd02d08e527a7b73ea53ad2c31cba352d2c Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Sat, 13 Jul 2019 03:57:56 +1000 Subject: [PATCH] Remove line item adjustments if line item deleted --- .../concerns/line_item_based_adjustment_handling.rb | 8 ++++++++ app/models/spree/line_item_decorator.rb | 1 + spec/models/spree/order_spec.rb | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 app/models/concerns/line_item_based_adjustment_handling.rb diff --git a/app/models/concerns/line_item_based_adjustment_handling.rb b/app/models/concerns/line_item_based_adjustment_handling.rb new file mode 100644 index 0000000000..70aa9549c7 --- /dev/null +++ b/app/models/concerns/line_item_based_adjustment_handling.rb @@ -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 diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 710fb56426..dbac611079 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -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 diff --git a/spec/models/spree/order_spec.rb b/spec/models/spree/order_spec.rb index 09cb55d215..4327d5e782 100644 --- a/spec/models/spree/order_spec.rb +++ b/spec/models/spree/order_spec.rb @@ -398,7 +398,7 @@ describe Spree::Order do expect(order.line_items(:reload).map(&:variant)).to eq([v2]) end - pending "removes the variant's adjustment" do + 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)