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.

Migrate logic for Spree::Variant

Matthew Kennedy169ed3c9139d
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index ca20fef52d..2ceb2f6b3c 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -43,6 +43,7 @@ module Spree
inverse_of: :variant
has_many :wished_items, dependent: :destroy
+ has_many :digitals
before_validation :set_cost_currency
@@ -59,6 +60,7 @@ module Spree
after_create :create_stock_items
after_create :set_master_out_of_stock, unless: :is_master?
+ after_save :destroy_digital, if: :deleted?
after_touch :clear_in_stock_cache
@@ -302,8 +304,22 @@ module Spree
@backordered ||= !in_stock? && stock_items.exists?(backorderable: true)
end
+ # Is this variant to be downloaded by the customer?
+ def digital?
+ digitals.present?
+ end
+
private
+ # :dependent => :destroy needs to be handled manually
+ # spree does not delete variants, just marks them as deleted?
+ # optionally keep digitals around for customers who require continued access to their purchases
+ def destroy_digital
+ return if Spree::DigitalConfiguration[:keep_digitals]
+
+ digitals.map(&:destroy)
+ end
+
def ensure_no_line_items
if line_items.any?
errors.add(:base, :cannot_destroy_if_attached_to_line_items)