Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Make Store Credits more flexible (#12074)

Damian Legawiecabb8b776e724
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 73d54d4d7f..63012259b5 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -89,9 +89,9 @@ module Spree
belongs_to :user, optional: true
end
if 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: true
else
belongs_to :created_by, optional: true
belongs_to :approver, optional: true
diff --git a/core/app/models/spree/refund.rb b/core/app/models/spree/refund.rb
index e8b40627f2..acaaafaf6b 100644
--- a/core/app/models/spree/refund.rb
+++ b/core/app/models/spree/refund.rb
@@ -13,7 +13,7 @@ module Spree
belongs_to :reimbursement, optional: true
end
belongs_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: true
has_many :log_entries, as: :source
diff --git a/core/app/models/spree/store_credit.rb b/core/app/models/spree/store_credit.rb
index a486416fcf..7cbec93ce3 100644
--- a/core/app/models/spree/store_credit.rb
+++ b/core/app/models/spree/store_credit.rb
@@ -19,13 +19,13 @@ module Spree
DEFAULT_CREATED_BY_EMAIL = '[email protected]'.freeze
belongs_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: true
belongs_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: true
validates :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 Spree
scope :order_by_priority, -> { includes(:credit_type).order('spree_store_credit_types.priority ASC') }
- before_validation :associate_credit_type
after_save :store_event
before_destroy :validate_no_amount_used
@@ -249,12 +248,5 @@ module Spree
throw(:abort)
end
end
-
- 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
- end
end
end
diff --git a/core/spec/models/spree/store_credit_spec.rb b/core/spec/models/spree/store_credit_spec.rb
index 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 do
expect(store_credit.errors[:amount_used]).to include(validation_message)
end
end
-
- 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
- end
end
describe 'validations' do