Skip to content
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
4 changes: 2 additions & 2 deletions core/app/models/spree/promotion_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ class Spree::PromotionCode < Spree::Base
belongs_to :promotion_code_batch, class_name: "Spree::PromotionCodeBatch", optional: true
has_many :adjustments

before_validation :normalize_code

validates :value, presence: true, uniqueness: { allow_blank: true, case_sensitive: true }
validates :promotion, presence: true
validate :promotion_not_apply_automatically, on: :create

before_save :normalize_code

self.whitelisted_ransackable_attributes = ['value']

# Whether the promotion code has exceeded its usage restrictions
Expand Down
50 changes: 41 additions & 9 deletions core/spec/models/spree/promotion_code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,54 @@

describe '#normalize_code' do
let(:promotion) { create(:promotion, code: code) }
let(:promotion_code) { promotion.codes.first }

before { subject }

context 'with mixed case' do
let(:code) { 'NewCoDe' }
context 'when no other code with the same value exists' do
let(:promotion_code) { promotion.codes.first }

it 'downcases the value before saving' do
expect(promotion_code.value).to eq('newcode')
context 'with mixed case' do
let(:code) { 'NewCoDe' }

it 'downcases the value before saving' do
expect(promotion_code.value).to eq('newcode')
end
end

context 'with extra spacing' do
let(:code) { ' new code ' }

it 'removes surrounding whitespace' do
expect(promotion_code.value).to eq 'new code'
end
end
end

context 'with extra spacing' do
let(:code) { ' new code ' }
it 'removes surrounding whitespace' do
expect(promotion_code.value).to eq 'new code'
context 'when another code with the same value exists' do
let(:promotion_code) { promotion.codes.build(value: code) }

context 'with mixed case' do
let(:code) { 'NewCoDe' }

it 'does not save the record and marks it as invalid' do
expect(promotion_code.valid?).to eq false

expect(promotion_code.errors.messages[:value]).to contain_exactly(
'has already been taken'
)
end
end

context 'with extra spacing' do
let(:code) { ' new code ' }

it 'does not save the record and marks it as invalid' do
expect(promotion_code.valid?).to eq false

expect(promotion_code.errors.messages[:value]).to contain_exactly(
'has already been taken'
)
end
end
end
end
Expand Down