Skip to content
This repository has been archived by the owner on Feb 19, 2025. It is now read-only.

Allow destroying promotions #25

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/models/solidus_friendly_promotions/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module SolidusFriendlyPromotions
class Promotion < Spree::Base
belongs_to :category, class_name: "SolidusFriendlyPromotions::PromotionCategory",
foreign_key: :promotion_category_id, optional: true
has_many :rules, class_name: "SolidusFriendlyPromotions::PromotionRule"
has_many :actions, class_name: "SolidusFriendlyPromotions::PromotionAction"
has_many :codes, class_name: "SolidusFriendlyPromotions::PromotionCode"
has_many :code_batches, class_name: "SolidusFriendlyPromotions::PromotionCodeBatch"
has_many :rules, class_name: "SolidusFriendlyPromotions::PromotionRule", dependent: :destroy
has_many :actions, class_name: "SolidusFriendlyPromotions::PromotionAction", dependent: :nullify
has_many :codes, class_name: "SolidusFriendlyPromotions::PromotionCode", dependent: :destroy
has_many :code_batches, class_name: "SolidusFriendlyPromotions::PromotionCodeBatch", dependent: :destroy

validates :name, :customer_label, presence: true
validates :path, uniqueness: {allow_blank: true, case_sensitive: true}
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20231011154553_allow_null_promotion_ids.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AllowNullPromotionIds < ActiveRecord::Migration[7.0]
def up
change_column_null :friendly_promotion_actions, :promotion_id, true
end

def down
change_column_null :friendly_promotion_actions, :promotion_id, false
end
end
12 changes: 12 additions & 0 deletions spec/models/solidus_friendly_promotions/promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
end
end

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

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
end
end

describe ".ordered_lanes" do
subject { described_class.ordered_lanes }

Expand Down