Skip to content

Commit

Permalink
Merge pull request #25 from friendlycart/nullify-promotion-actions-pr…
Browse files Browse the repository at this point in the history
…omotion-ids

Allow destroying promotions
  • Loading branch information
mamhoff authored Oct 12, 2023
2 parents ca40b64 + 94f72be commit f714b87
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
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
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
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

0 comments on commit f714b87

Please sign in to comment.