diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rb index 65f3e7580d8..316a79fcbdc 100644 --- a/core/app/models/spree/promotion.rb +++ b/core/app/models/spree/promotion.rb @@ -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 @@ -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 diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rb index 62dfe75a338..1d2e53e3eaa 100644 --- a/core/spec/models/spree/promotion_spec.rb +++ b/core/spec/models/spree/promotion_spec.rb @@ -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") } @@ -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] }