Skip to content

Commit

Permalink
Actually destroy promotion actions
Browse files Browse the repository at this point in the history
We now protect against destroying promotion actions that have been
applied to a complete order, so we can stop the dependent: :nullify
thing.
  • Loading branch information
mamhoff committed Jan 25, 2024
1 parent 72f8070 commit 4f29bcb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/models/solidus_friendly_promotions/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Promotion < Spree::Base
foreign_key: :promotion_category_id, optional: true
belongs_to :original_promotion, class_name: "Spree::Promotion", optional: true
has_many :rules, class_name: "SolidusFriendlyPromotions::PromotionRule", dependent: :destroy
has_many :actions, class_name: "SolidusFriendlyPromotions::PromotionAction", dependent: :nullify
has_many :actions, class_name: "SolidusFriendlyPromotions::PromotionAction", dependent: :destroy
has_many :codes, class_name: "SolidusFriendlyPromotions::PromotionCode", dependent: :destroy
has_many :code_batches, class_name: "SolidusFriendlyPromotions::PromotionCodeBatch", dependent: :destroy
has_many :order_promotions, class_name: "SolidusFriendlyPromotions::OrderPromotion", dependent: :destroy
Expand Down
18 changes: 15 additions & 3 deletions spec/models/solidus_friendly_promotions/promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,26 @@
end

describe "#destroy" do
let!(:promotion) { create(:friendly_promotion, :with_adjustable_action) }
let!(:promotion) { create(:friendly_promotion, :with_adjustable_action, apply_automatically: true) }

subject { promotion.destroy! }

it "destroys the promotion and nullifies the action" do
expect { subject }.to change { SolidusFriendlyPromotions::Promotion.count }.by(-1)
expect(SolidusFriendlyPromotions::PromotionAction.count).to eq(1)
expect(SolidusFriendlyPromotions::PromotionAction.first.promotion_id).to be nil
expect(SolidusFriendlyPromotions::PromotionAction.count).to be_zero
end

context "when the promotion has been applied to a complete order" do
let(:order) { create(:order_ready_to_complete) }

before do
order.recalculate
order.complete!
end

it "raises an error" do
expect { subject }.to raise_exception(ActiveRecord::RecordNotDestroyed)
end
end
end

Expand Down

0 comments on commit 4f29bcb

Please sign in to comment.