diff --git a/backend/app/controllers/spree/admin/promotion_actions_controller.rb b/backend/app/controllers/spree/admin/promotion_actions_controller.rb index 6cd3c74c4af..289ce075b40 100644 --- a/backend/app/controllers/spree/admin/promotion_actions_controller.rb +++ b/backend/app/controllers/spree/admin/promotion_actions_controller.rb @@ -17,7 +17,7 @@ def create def destroy @promotion_action = @promotion.promotion_actions.find(params[:id]) - if @promotion_action.destroy + if @promotion_action.discard flash[:success] = t('spree.successfully_removed', resource: t('spree.promotion_action')) end respond_to do |format| diff --git a/core/app/models/spree/promotion/actions/create_adjustment.rb b/core/app/models/spree/promotion/actions/create_adjustment.rb index 10b2da1a44e..00d14e38ede 100644 --- a/core/app/models/spree/promotion/actions/create_adjustment.rb +++ b/core/app/models/spree/promotion/actions/create_adjustment.rb @@ -11,6 +11,7 @@ class CreateAdjustment < PromotionAction before_validation :ensure_action_has_calculator before_destroy :remove_adjustments_from_incomplete_orders + before_discard :remove_adjustments_from_incomplete_orders # Creates the adjustment related to a promotion for the order passed # through options hash diff --git a/core/app/models/spree/promotion/actions/create_item_adjustments.rb b/core/app/models/spree/promotion/actions/create_item_adjustments.rb index a90cf93d5f2..387ad9a7d75 100644 --- a/core/app/models/spree/promotion/actions/create_item_adjustments.rb +++ b/core/app/models/spree/promotion/actions/create_item_adjustments.rb @@ -11,6 +11,7 @@ class CreateItemAdjustments < PromotionAction before_validation :ensure_action_has_calculator before_destroy :remove_adjustments_from_incomplete_orders + before_discard :remove_adjustments_from_incomplete_orders def perform(payload = {}) order = payload[:order] diff --git a/core/app/models/spree/promotion_action.rb b/core/app/models/spree/promotion_action.rb index 9a425bd3306..cb2082790f4 100644 --- a/core/app/models/spree/promotion_action.rb +++ b/core/app/models/spree/promotion_action.rb @@ -1,3 +1,5 @@ +require 'discard' + module Spree # Base class for all types of promotion action. # @@ -5,6 +7,10 @@ module Spree # by an event and determined to be eligible. class PromotionAction < Spree::Base acts_as_paranoid + include Spree::ParanoiaDeprecations + + include Discard::Model + self.discard_column = :deleted_at belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_actions diff --git a/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb b/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb index 858785bc85d..19a7d8920d3 100644 --- a/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb +++ b/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb @@ -78,7 +78,7 @@ end end - context "#paranoia_destroy" do + shared_examples "destroying adjustments from incomplete orders" do before(:each) do action.calculator = Spree::Calculator::FlatRate.new(preferred_amount: 10) promotion.promotion_actions = [action] @@ -87,7 +87,7 @@ context "when order is not complete" do it "should not keep the adjustment" do action.perform(payload) - action.paranoia_destroy + subject expect(order.adjustments.count).to eq(0) end end @@ -99,7 +99,7 @@ before(:each) do action.perform(payload) - action.paranoia_destroy + subject end it "should keep the adjustment" do @@ -111,4 +111,14 @@ end end end + + context "#discard" do + subject { action.discard } + it_should_behave_like "destroying adjustments from incomplete orders" + end + + context "#paranoia_destroy" do + subject { Spree::Deprecation.silence { action.paranoia_destroy } } + it_should_behave_like "destroying adjustments from incomplete orders" + end end diff --git a/core/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb b/core/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb index bd44ba659d9..5ebc9e251c9 100644 --- a/core/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb +++ b/core/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb @@ -138,7 +138,7 @@ module Spree end end - context "#paranoia_destroy" do + shared_examples "destroying adjustments from incomplete orders" do let!(:action) { promotion.actions.first } let(:other_action) { other_promotion.actions.first } let(:promotion) { create(:promotion, :with_line_item_adjustment) } @@ -151,7 +151,7 @@ module Spree order.adjustments.create!(label: 'Check', amount: 0, order: order, source: action) expect { - action.paranoia_destroy + subject }.to change { Adjustment.count }.by(-1) end end @@ -165,7 +165,7 @@ module Spree expect { expect { - action.paranoia_destroy + subject }.not_to change { adjustment.reload.source_id } }.not_to change { Spree::Adjustment.count } @@ -177,10 +177,20 @@ module Spree order.adjustments.create!(label: "Check", amount: 0, order: order, source: action) expect { - action.paranoia_destroy + subject }.not_to change { other_action.adjustments.count } end end end + + describe "#discard" do + subject { action.discard } + it_should_behave_like "destroying adjustments from incomplete orders" + end + + describe "#paranoia_destroy" do + subject { Spree::Deprecation.silence { action.paranoia_destroy } } + it_should_behave_like "destroying adjustments from incomplete orders" + end end end diff --git a/frontend/spec/features/coupon_code_spec.rb b/frontend/spec/features/coupon_code_spec.rb index a066d13ff30..4ee25d4a99b 100644 --- a/frontend/spec/features/coupon_code_spec.rb +++ b/frontend/spec/features/coupon_code_spec.rb @@ -174,13 +174,12 @@ def create_basic_coupon_promotion(code) calculator = Spree::Calculator::FlatPercentItemTotal.new calculator.preferred_flat_percent = 100 - action = Spree::Promotion::Actions::CreateAdjustment.new - action.calculator = calculator - action.promotion = promotion - action.save! + promotion.promotion_actions.first.discard - promotion.promotion_actions = [action] - promotion.save! + Spree::Promotion::Actions::CreateAdjustment.create!( + calculator: calculator, + promotion: promotion + ) create(:product, name: "Spree Mug", price: 10) end