Skip to content

Commit

Permalink
Feat(Promotions): Add helpers for checking activation options
Browse files Browse the repository at this point in the history
There are three ways of activating a promotion, and they are mutually
exclusive. This adds a few helper methods so we know when a promotion
can have a code added, or promotion codes added, or whether it can
change the `apply_automatically` Boolean.
  • Loading branch information
mamhoff committed Nov 18, 2024
1 parent 2632096 commit 7d86a96
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
12 changes: 12 additions & 0 deletions promotions/app/models/solidus_promotions/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ def eligibility_results
@eligibility_results ||= SolidusPromotions::EligibilityResults.new(self)
end

def can_change_apply_automatically?
path.blank? && codes.empty?
end

def can_change_path?
!apply_automatically? && codes.empty?
end

def can_change_codes?
!apply_automatically? && path.blank?
end

private

def normalize_blank_values
Expand Down
78 changes: 78 additions & 0 deletions promotions/spec/models/solidus_promotions/promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -686,4 +686,82 @@
expect(subject).to be_nil
end
end

describe "#can_change_apply_automatically?" do
subject { promotion.can_change_apply_automatically? }

let(:promotion) { create :solidus_promotion }

context "when the promotion has a path" do
before { promotion.path = "foo" }

it { is_expected.to be false }
end

context "when the promotion has a code" do
before { promotion.codes.new(value: "foo") }

it { is_expected.to be false }
end

context "when the promotion has neither a path nor a code" do
it { is_expected.to be true }
end
end

describe "#can_change_path?" do
subject { promotion.can_change_path? }

let(:promotion) { create :solidus_promotion }

context "when the promotion has a code" do
before { promotion.codes.new(value: "foo") }

it { is_expected.to be false }
end

context "when the promotion has a path" do
before { promotion.path = "foo" }

it { is_expected.to be true }
end

context "when the promotion has neither a path nor a code" do
it { is_expected.to be true }
end

context "when the promotion applies automatically" do
before { promotion.apply_automatically = true }

it { is_expected.to be false }
end
end

describe "#can_change_codes?" do
subject { promotion.can_change_codes? }

let(:promotion) { create :solidus_promotion }

context "when the promotion has a code" do
before { promotion.codes.new(value: "foo") }

it { is_expected.to be true }
end

context "when the promotion has a path" do
before { promotion.path = "foo" }

it { is_expected.to be false }
end

context "when the promotion has neither a path nor a code" do
it { is_expected.to be true }
end

context "when the promotion applies automatically" do
before { promotion.apply_automatically = true }

it { is_expected.to be false }
end
end
end

0 comments on commit 7d86a96

Please sign in to comment.