Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Promotion#with_coupon_code returns the newest created promotion with actions
Damian Legawiec6f2b0c390c63
diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rbindex 8d967c4b26..745df37a51 100644--- a/core/app/models/spree/promotion.rb+++ b/core/app/models/spree/promotion.rb@@ -45,7 +45,7 @@ module Spreedef self.with_coupon_code(coupon_code)where("lower(#{table_name}.code) = ?", coupon_code.strip.downcase).includes(:promotion_actions).where.not(spree_promotion_actions: { id: nil }).- first+ lastenddef self.activediff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rbindex e1eb796c39..89ba8e78bb 100644--- a/core/spec/models/spree/promotion_spec.rb+++ b/core/spec/models/spree/promotion_spec.rb@@ -5,59 +5,63 @@ describe Spree::Promotion, type: :model dolet(:promotion) { create(:promotion) }describe 'validations' do- before do- @valid_promotion = build(:promotion, name: 'A promotion', stores: [create(:store)])- end+ let!(:valid_promotion) { build(:promotion, name: 'A promotion', stores: [create(:store)]) }it 'valid_promotion is valid' do- expect(@valid_promotion).to be_valid+ expect(valid_promotion).to be_validendit 'validates usage limit' do- @valid_promotion.usage_limit = -1- expect(@valid_promotion).not_to be_valid+ valid_promotion.usage_limit = -1+ expect(valid_promotion).not_to be_valid- @valid_promotion.usage_limit = 100- expect(@valid_promotion).to be_valid+ valid_promotion.usage_limit = 100+ expect(valid_promotion).to be_validendit 'validates name' do- @valid_promotion.name = nil- expect(@valid_promotion).not_to be_valid+ valid_promotion.name = nil+ expect(valid_promotion).not_to be_valid+ end++ it 'can create multiple promos with the same code' do+ create(:promotion, code: 'ABC')+ valid_promotion.code = 'ABC'+ expect(valid_promotion).to be_validenddescribe 'expires_at_must_be_later_than_starts_at' dobefore do- @valid_promotion.starts_at = Date.today+ valid_promotion.starts_at = Date.todayendcontext 'starts_at is a date earlier than expires_at' do- before { @valid_promotion.expires_at = 5.days.from_now }+ before { valid_promotion.expires_at = 5.days.from_now }it 'is valid' do- expect(@valid_promotion).to be_valid+ expect(valid_promotion).to be_validendendcontext 'starts_at is a date earlier than expires_at' do- before { @valid_promotion.expires_at = 5.days.ago }+ before { valid_promotion.expires_at = 5.days.ago }context 'is not valid' do- before { @valid_promotion.valid? }+ before { valid_promotion.valid? }- it { expect(@valid_promotion).not_to be_valid }- it { expect(@valid_promotion.errors[:expires_at]).to include(I18n.t(:invalid_date_range, scope: 'activerecord.errors.models.spree/promotion.attributes.expires_at')) }+ it { expect(valid_promotion).not_to be_valid }+ it { expect(valid_promotion.errors[:expires_at]).to include(I18n.t(:invalid_date_range, scope: 'activerecord.errors.models.spree/promotion.attributes.expires_at')) }endendcontext 'starts_at and expires_at are nil' dobefore do- @valid_promotion.expires_at = nil- @valid_promotion.starts_at = nil+ valid_promotion.expires_at = nil+ valid_promotion.starts_at = nilendit 'is valid' do- expect(@valid_promotion).to be_valid+ expect(valid_promotion).to be_validendendend@@ -604,10 +608,16 @@ describe Spree::Promotion, type: :model doendcontext 'when promotion has no actions' do- let!(:promotion_without_actions) { create(:promotion, code: 'MY-COUPON-123').actions.clear }+ let!(:promotion_without_actions) { create(:promotion, code: 'MY-COUPON-123') }+ let!(:promotion_with_actions) { create(:promotion_with_order_adjustment, code: 'MY-COUPON-123') }it 'then returns the one with an action' do- expect(described_class.with_coupon_code('MY-COUPON-123')).to be_nil+ expect(described_class.with_coupon_code('MY-COUPON-123')).to eq(promotion_with_actions)+ end++ it 'return the last one created' do+ promotion_with_actions_2 = create(:promotion_with_order_adjustment, code: 'MY-COUPON-123')+ expect(described_class.with_coupon_code('MY-COUPON-123')).to eq(promotion_with_actions_2)endendend