Skip to content
Open
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
5 changes: 3 additions & 2 deletions core/app/models/spree/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class Promotion < Spree::Base

before_save :normalize_blank_values

scope :coupons, -> { where("#{table_name}.code IS NOT NULL") }
scope :coupons, -> { where(arel_table[:code].not_eq(nil)) }

scope :applied, lambda {
joins(<<-SQL).uniq
INNER JOIN spree_order_promotions
Expand All @@ -39,7 +40,7 @@ class Promotion < Spree::Base
self.whitelisted_ransackable_attributes = ['path', 'promotion_category_id', 'code']

def self.with_coupon_code(coupon_code)
where("lower(#{table_name}.code) = ?", coupon_code.strip.downcase).first
where(arel_table[:code].lower.eq(coupon_code.strip.downcase)).first
end

def self.active
Expand Down
20 changes: 10 additions & 10 deletions core/spec/models/spree/promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@
end
end

# Regression test for #4081
describe ".with_coupon_code" do
context "and code stored in uppercase" do
let!(:promotion) { create(:promotion, :code => "MY-COUPON-123") }
it "finds the code with lowercase" do
expect(Spree::Promotion.with_coupon_code("my-coupon-123")).to eql promotion
end
end
end

describe "#destroy" do
let(:promotion) { Spree::Promotion.create(:name => "delete me") }

Expand Down Expand Up @@ -515,16 +525,6 @@
end
end

# Regression test for #4081
describe "#with_coupon_code" do
context "and code stored in uppercase" do
let!(:promotion) { create(:promotion, :code => "MY-COUPON-123") }
it "finds the code with lowercase" do
expect(Spree::Promotion.with_coupon_code("my-coupon-123")).to eql promotion
end
end
end

describe '#used_by?' do
subject { promotion.used_by? user, [excluded_order] }

Expand Down