Skip to content

Commit

Permalink
Merge pull request #4230 from jcsanti/backport-4228
Browse files Browse the repository at this point in the history
Backport #4228 to V2.11
  • Loading branch information
waiting-for-dev authored Jan 5, 2022
2 parents 3e94658 + 23e9647 commit c728183
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
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

0 comments on commit c728183

Please sign in to comment.