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
Make Store Credits more flexible (#12074)
Damian Legawiecabb8b776e724
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex 73d54d4d7f..63012259b5 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -89,9 +89,9 @@ module Spreebelongs_to :user, optional: trueendif Spree.admin_user_class- belongs_to :created_by, class_name: Spree.admin_user_class.to_s, optional: true- belongs_to :approver, class_name: Spree.admin_user_class.to_s, optional: true- belongs_to :canceler, class_name: Spree.admin_user_class.to_s, optional: true+ belongs_to :created_by, class_name: "::#{Spree.admin_user_class}", optional: true+ belongs_to :approver, class_name: "::#{Spree.admin_user_class}", optional: true+ belongs_to :canceler, class_name: "::#{Spree.admin_user_class}", optional: trueelsebelongs_to :created_by, optional: truebelongs_to :approver, optional: truediff --git a/core/app/models/spree/refund.rb b/core/app/models/spree/refund.rbindex e8b40627f2..acaaafaf6b 100644--- a/core/app/models/spree/refund.rb+++ b/core/app/models/spree/refund.rb@@ -13,7 +13,7 @@ module Spreebelongs_to :reimbursement, optional: trueendbelongs_to :reason, class_name: 'Spree::RefundReason', foreign_key: :refund_reason_id- belongs_to :refunder, class_name: Spree.admin_user_class.to_s, optional: true+ belongs_to :refunder, class_name: "::#{Spree.admin_user_class}", optional: truehas_many :log_entries, as: :sourcediff --git a/core/app/models/spree/store_credit.rb b/core/app/models/spree/store_credit.rbindex a486416fcf..7cbec93ce3 100644--- a/core/app/models/spree/store_credit.rb+++ b/core/app/models/spree/store_credit.rb@@ -19,13 +19,13 @@ module SpreeDEFAULT_CREATED_BY_EMAIL = '[email protected]'.freezebelongs_to :user, class_name: "::#{Spree.user_class}", foreign_key: 'user_id'- belongs_to :category, class_name: 'Spree::StoreCreditCategory'- belongs_to :created_by, class_name: Spree.admin_user_class.to_s, foreign_key: 'created_by_id'- belongs_to :credit_type, class_name: 'Spree::StoreCreditType', foreign_key: 'type_id'+ belongs_to :category, class_name: 'Spree::StoreCreditCategory', optional: true+ belongs_to :created_by, class_name: "::#{Spree.admin_user_class}", foreign_key: 'created_by_id', optional: true+ belongs_to :credit_type, class_name: 'Spree::StoreCreditType', foreign_key: 'type_id', optional: truebelongs_to :store, class_name: 'Spree::Store'has_many :store_credit_events, class_name: 'Spree::StoreCreditEvent'- validates :user, :category, :credit_type, :created_by, :currency, :store, presence: true+ validates :currency, :store, presence: truevalidates :amount, numericality: { greater_than: 0 }validates :amount_used, numericality: { greater_than_or_equal_to: 0 }validate :amount_used_less_than_or_equal_to_amount@@ -36,7 +36,6 @@ module Spreescope :order_by_priority, -> { includes(:credit_type).order('spree_store_credit_types.priority ASC') }- before_validation :associate_credit_typeafter_save :store_eventbefore_destroy :validate_no_amount_used@@ -249,12 +248,5 @@ module Spreethrow(:abort)endend-- def associate_credit_type- unless type_id- credit_type_name = category.try(:non_expiring?) ? 'Non-expiring' : 'Expiring'- self.credit_type = Spree::StoreCreditType.find_or_create_by(name: credit_type_name)- end- endendenddiff --git a/core/spec/models/spree/store_credit_spec.rb b/core/spec/models/spree/store_credit_spec.rbindex c8116fa377..4694380ab4 100644--- a/core/spec/models/spree/store_credit_spec.rb+++ b/core/spec/models/spree/store_credit_spec.rb@@ -22,38 +22,6 @@ describe Spree::StoreCredit, type: :model doexpect(store_credit.errors[:amount_used]).to include(validation_message)endend-- context 'category is a non-expiring type' do- let!(:secondary_credit_type) { create(:secondary_credit_type) }- let(:store_credit) { build(:store_credit, credit_type: nil) }-- before { allow(store_credit.category).to receive(:non_expiring?).and_return(true) }-- it 'sets the credit type to non-expiring' do- subject- expect(store_credit.credit_type.name).to eq secondary_credit_type.name- end- end-- context 'category is an expiring type' do- before { allow(store_credit.category).to receive(:non_expiring?).and_return(false) }-- it 'sets the credit type to non-expiring' do- subject- expect(store_credit.credit_type.name).to eq 'Expiring'- end- end-- context 'the type is set' do- let!(:secondary_credit_type) { create(:secondary_credit_type) }- let(:store_credit) { build(:store_credit, credit_type: secondary_credit_type) }-- before { allow(store_credit.category).to receive(:non_expiring?).and_return(false) }-- it "doesn't overwrite the type" do- expect { subject }.not_to change(store_credit, :credit_type)- end- endenddescribe 'validations' do