Skip to content

Commit

Permalink
normalize promotion code before validation
Browse files Browse the repository at this point in the history
  • Loading branch information
storm2513 committed Oct 3, 2020
1 parent f58e5fc commit f4a064e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/app/models/spree/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Promotion < Spree::Base

before_save :normalize_blank_values

before_validation :normalize_code

scope :coupons, -> { where.not(code: nil) }
scope :advertised, -> { where(advertise: true) }
scope :applied, lambda {
Expand Down Expand Up @@ -227,6 +229,10 @@ def normalize_blank_values
end
end

def normalize_code
self.code = code.strip if code.present?
end

def match_all?
match_policy == 'all'
end
Expand Down
32 changes: 32 additions & 0 deletions core/spec/models/spree/promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,38 @@
expect(@valid_promotion).not_to be_valid
end

describe 'code should be unique' do
let(:code) { 'code' }

context 'code is unique' do
before do
@valid_promotion.code = code
end

it { expect(@valid_promotion).to be_valid }
end

context 'code is not unique' do
let!(:promotion_with_code) { Spree::Promotion.create! name: 'test1', code: code }

context 'code is identical' do
before do
@valid_promotion.code = code
end

it { expect(@valid_promotion).not_to be_valid }
end

context 'code is identical with whitespace' do
before do
@valid_promotion.code = code + ' '
end

it { expect(@valid_promotion).not_to be_valid }
end
end
end

describe 'expires_at_must_be_later_than_starts_at' do
before do
@valid_promotion.starts_at = Date.today
Expand Down

0 comments on commit f4a064e

Please sign in to comment.