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.

Drop additional Store Credit <> Gift Card association, just use originator

Damian Legawiec62e26d295f21
diff --git a/admin/app/views/spree/admin/payments/_payment.html.erb b/admin/app/views/spree/admin/payments/_payment.html.erb
index 1e89e541b1..4e3829c13e 100644
--- a/admin/app/views/spree/admin/payments/_payment.html.erb
+++ b/admin/app/views/spree/admin/payments/_payment.html.erb
@@ -24,8 +24,8 @@
<% if payment.try(:gateway_dashboard_payment_url).present? %>
<%= link_to_with_icon 'eye', Spree.t(:details), payment.gateway_dashboard_payment_url, class: 'dropdown-item', target: '_blank' %>
<% end %>
- <% if payment.source.is_a?(Spree::StoreCredit) && payment.source.respond_to?(:gift_card) && payment.source.gift_card.present? %>
- <%= link_to_with_icon 'eye', Spree.t(:details), spree.admin_gift_card_path(payment.source.gift_card), class: 'dropdown-item' %>
+ <% if payment.source.is_a?(Spree::StoreCredit) && payment.source.originator_type == 'Spree::GiftCard' %>
+ <%= link_to_with_icon 'eye', Spree.t(:details), spree.admin_gift_card_path(payment.source.originator), class: 'dropdown-item' %>
<% end %>
<% if can?(:update, payment) && (payment.checkout? || payment.pending?) %>
diff --git a/core/app/models/spree/gift_card.rb b/core/app/models/spree/gift_card.rb
index b01da1cc74..8819756b30 100644
--- a/core/app/models/spree/gift_card.rb
+++ b/core/app/models/spree/gift_card.rb
@@ -47,7 +47,7 @@ module Spree
belongs_to :user, class_name: Spree.user_class.to_s, optional: true
belongs_to :batch, class_name: 'Spree::GiftCardBatch', optional: true, foreign_key: :gift_card_batch_id
- has_many :store_credits, class_name: 'Spree::StoreCredit'
+ has_many :store_credits, class_name: 'Spree::StoreCredit', as: :originator
has_many :orders, inverse_of: :gift_card, class_name: 'Spree::Order'
has_many :users, through: :orders, class_name: Spree.user_class.to_s
diff --git a/core/app/models/spree/order/gift_card.rb b/core/app/models/spree/order/gift_card.rb
index 9999fba8e3..4f8b749632 100644
--- a/core/app/models/spree/order/gift_card.rb
+++ b/core/app/models/spree/order/gift_card.rb
@@ -14,7 +14,7 @@ module Spree
return 0.to_d unless gift_card.present?
store_credit_ids = payments.store_credits.valid.pluck(:source_id)
- store_credits = Spree::StoreCredit.where(id: store_credit_ids, gift_card: gift_card)
+ store_credits = Spree::StoreCredit.where(id: store_credit_ids, originator: gift_card)
store_credits.sum(:amount)
end
diff --git a/core/app/services/spree/gift_cards/apply.rb b/core/app/services/spree/gift_cards/apply.rb
index e0130e9b59..693895ea8a 100644
--- a/core/app/services/spree/gift_cards/apply.rb
+++ b/core/app/services/spree/gift_cards/apply.rb
@@ -24,7 +24,8 @@ module Spree
user: order.user,
amount: amount,
currency: order.currency,
- expires_at: gift_card.expires_at
+ originator: gift_card,
+ action_originator: gift_card
)
gift_card.amount_remaining -= amount
gift_card.save!
diff --git a/core/app/views/spree/shared/_payment.html.erb b/core/app/views/spree/shared/_payment.html.erb
index f3e244e628..9b2667bbfe 100644
--- a/core/app/views/spree/shared/_payment.html.erb
+++ b/core/app/views/spree/shared/_payment.html.erb
@@ -12,11 +12,11 @@
<p class="mb-0"><%= Spree.t(:expiration) %> <%= source.month %>/<%= source.year %></p>
</div>
<% elsif source.is_a?(Spree::StoreCredit) %>
- <% if source.respond_to?(:gift_card) && source.gift_card.present? %>
- <div class="d-flex align-items-center text-decoration-none w-100 h-100">
- <%= icon('gift', height: 30, class: 'mr-3 text-muted') %>
+ <% if source.originator_type == 'Spree::GiftCard' %>
+ <div class="d-flex align-items-center">
+ <%= icon('gift', height: 30, class: 'mr-3 text-muted opacity-50') %>
<div>
- <p class="mb-1"><%= Spree.t(:gift_card) %>: <strong><%= source.gift_card.code.upcase %></strong></p>
+ <p class="mb-1"><%= Spree.t(:gift_card) %>: <strong><%= source.originator.code.upcase %></strong></p>
<%= @order.display_gift_card_total %>
</div>
</div>
diff --git a/core/db/migrate/20250506073057_create_spree_gift_cards_and_spree_gift_card_batches.rb b/core/db/migrate/20250506073057_create_spree_gift_cards_and_spree_gift_card_batches.rb
index 7947f86a61..1a38b2ddaf 100644
--- a/core/db/migrate/20250506073057_create_spree_gift_cards_and_spree_gift_card_batches.rb
+++ b/core/db/migrate/20250506073057_create_spree_gift_cards_and_spree_gift_card_batches.rb
@@ -28,11 +28,6 @@ class CreateSpreeGiftCardsAndSpreeGiftCardBatches < ActiveRecord::Migration[7.2]
add_index :spree_gift_cards, :code, unique: { scope: :store_id }
- add_column :spree_store_credits, :gift_card_id, :bigint, if_not_exists: true
- add_column :spree_store_credits, :expires_at, :datetime, if_not_exists: true
- add_column :spree_store_credits, :state, :integer, default: 0, if_not_exists: true
- add_index :spree_store_credits, :gift_card_id, if_not_exists: true
-
add_column :spree_orders, :gift_card_id, :bigint, if_not_exists: true
add_index :spree_orders, :gift_card_id, if_not_exists: true
end
diff --git a/core/spec/models/spree/order_merger_spec.rb b/core/spec/models/spree/order_merger_spec.rb
index e472386e3b..36aae2922e 100644
--- a/core/spec/models/spree/order_merger_spec.rb
+++ b/core/spec/models/spree/order_merger_spec.rb
@@ -161,7 +161,7 @@ module Spree
let(:order_2) { create(:order, gift_card: gift_card, total: 20) }
let(:gift_card) { create(:gift_card) }
let(:store_credit_payment_method) { create(:store_credit_payment_method) }
- let(:store_credit) { create(:store_credit, gift_card: gift_card, amount: 20) }
+ let(:store_credit) { create(:store_credit, originator: gift_card, amount: 20) }
let!(:payment) { create(:payment, order: order_2, payment_method: store_credit_payment_method, amount: 20, source: store_credit) }
it 'merges the gift card' do
diff --git a/core/spec/services/spree/gift_cards/apply_spec.rb b/core/spec/services/spree/gift_cards/apply_spec.rb
index b449d41d49..577a618870 100644
--- a/core/spec/services/spree/gift_cards/apply_spec.rb
+++ b/core/spec/services/spree/gift_cards/apply_spec.rb
@@ -32,7 +32,7 @@ RSpec.describe Spree::GiftCards::Apply do
expect(store_credit.amount).to eq(30)
expect(store_credit.store).to eq(Spree::Store.default)
- expect(store_credit.gift_card).to eq(gift_card)
+ expect(store_credit.originator).to eq(gift_card)
end
context 'when the order has applied store credit' do
@@ -51,7 +51,7 @@ RSpec.describe Spree::GiftCards::Apply do
expect(order.reload.gift_card).to eq(nil)
expect(order.total_applied_store_credit).to eq(10)
- expect(order.payments.store_credits.last.source.gift_card).to eq(nil)
+ expect(order.payments.store_credits.last.source.originator).to eq(nil)
end
end