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.

Replace methods for fetching deleted resources with scope

Damian Legawiec6bf1159960b6
diff --git a/core/app/models/concerns/spree/default_price.rb b/core/app/models/concerns/spree/default_price.rb
index 1a36e34a10..0409cc9479 100644
--- a/core/app/models/concerns/spree/default_price.rb
+++ b/core/app/models/concerns/spree/default_price.rb
@@ -4,7 +4,7 @@ module Spree
included do
has_one :default_price,
- -> { where currency: Spree::Config[:currency] },
+ -> { with_deleted.where(currency: Spree::Config[:currency]) },
class_name: 'Spree::Price',
dependent: :destroy
@@ -14,10 +14,6 @@ module Spree
after_save :save_default_price
- def default_price
- Spree::Price.unscoped { super }
- end
-
def has_default_price?
!default_price.nil?
end
diff --git a/core/app/models/spree/inventory_unit.rb b/core/app/models/spree/inventory_unit.rb
index ec292e8c2f..760062fffe 100644
--- a/core/app/models/spree/inventory_unit.rb
+++ b/core/app/models/spree/inventory_unit.rb
@@ -1,7 +1,7 @@
module Spree
class InventoryUnit < Spree::Base
with_options inverse_of: :inventory_units do
- belongs_to :variant, class_name: 'Spree::Variant'
+ belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant'
belongs_to :order, class_name: 'Spree::Order'
belongs_to :shipment, class_name: 'Spree::Shipment', touch: true, optional: true
has_many :return_items, inverse_of: :inventory_unit
@@ -84,11 +84,6 @@ module Spree
split_inventory!(1)
end
- # Remove variant default_scope `deleted_at: nil`
- def variant
- Spree::Variant.unscoped { super }
- end
-
def current_or_new_return_item
Spree::ReturnItem.from_inventory_unit(self)
end
diff --git a/core/app/models/spree/line_item.rb b/core/app/models/spree/line_item.rb
index 56a0ade667..5bc9de7682 100644
--- a/core/app/models/spree/line_item.rb
+++ b/core/app/models/spree/line_item.rb
@@ -4,7 +4,7 @@ module Spree
with_options inverse_of: :line_items do
belongs_to :order, class_name: 'Spree::Order', touch: true
- belongs_to :variant, class_name: 'Spree::Variant'
+ belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant'
end
belongs_to :tax_category, class_name: 'Spree::TaxCategory'
@@ -120,11 +120,6 @@ module Spree
assign_attributes opts
end
- # Remove variant default_scope `deleted_at: nil`
- def variant
- Spree::Variant.unscoped { super }
- end
-
private
def ensure_valid_quantity
diff --git a/core/app/models/spree/price.rb b/core/app/models/spree/price.rb
index f489dabbfb..5c8189f69e 100644
--- a/core/app/models/spree/price.rb
+++ b/core/app/models/spree/price.rb
@@ -6,7 +6,7 @@ module Spree
MAXIMUM_AMOUNT = BigDecimal('99_999_999.99')
- belongs_to :variant, class_name: 'Spree::Variant', inverse_of: :prices, touch: true
+ belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant', inverse_of: :prices, touch: true
before_validation :ensure_currency
@@ -63,11 +63,6 @@ module Spree
Spree::Money.new(compare_at_price_including_vat_for(price_options), currency: currency)
end
- # Remove variant default_scope `deleted_at: nil`
- def variant
- Spree::Variant.unscoped { super }
- end
-
private
def ensure_currency
diff --git a/core/app/models/spree/shipping_method.rb b/core/app/models/spree/shipping_method.rb
index 6abe2f065b..c0d0dbde73 100644
--- a/core/app/models/spree/shipping_method.rb
+++ b/core/app/models/spree/shipping_method.rb
@@ -19,7 +19,7 @@ module Spree
foreign_key: 'shipping_method_id'
has_many :zones, through: :shipping_method_zones, class_name: 'Spree::Zone'
- belongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true
+ belongs_to :tax_category, -> { with_deleted }, class_name: 'Spree::TaxCategory', optional: true
validates :name, :display_on, presence: true
@@ -44,10 +44,6 @@ module Spree
select { |c| c.to_s.constantize < Spree::ShippingCalculator }
end
- def tax_category
- Spree::TaxCategory.unscoped { super }
- end
-
def available_to_display?(display_filter)
(frontend? && display_filter == DISPLAY_ON_FRONT_END) ||
(backend? && display_filter == DISPLAY_ON_BACK_END)
diff --git a/core/app/models/spree/shipping_rate.rb b/core/app/models/spree/shipping_rate.rb
index db3e9d7a06..536e948ff6 100644
--- a/core/app/models/spree/shipping_rate.rb
+++ b/core/app/models/spree/shipping_rate.rb
@@ -1,9 +1,8 @@
module Spree
class ShippingRate < Spree::Base
belongs_to :shipment, class_name: 'Spree::Shipment'
- belongs_to :tax_rate, class_name: 'Spree::TaxRate'
- belongs_to :shipping_method, class_name: 'Spree::ShippingMethod', inverse_of: :shipping_rates
-
+ belongs_to :tax_rate, -> { with_deleted }, class_name: 'Spree::TaxRate'
+ belongs_to :shipping_method, -> { with_deleted }, class_name: 'Spree::ShippingMethod', inverse_of: :shipping_rates
extend Spree::DisplayMoney
money_methods :base_price, :final_price, :tax_amount
@@ -32,14 +31,6 @@ module Spree
@tax_amount ||= tax_rate&.calculator&.compute_shipping_rate(self) || BigDecimal(0)
end
- def shipping_method
- Spree::ShippingMethod.unscoped { super }
- end
-
- def tax_rate
- Spree::TaxRate.unscoped { super }
- end
-
# returns base price - any available discounts for this Shipment
# useful when you want to present a list of available shipping rates
def final_price
diff --git a/core/app/models/spree/stock_item.rb b/core/app/models/spree/stock_item.rb
index 1261d9de88..9649271b86 100644
--- a/core/app/models/spree/stock_item.rb
+++ b/core/app/models/spree/stock_item.rb
@@ -4,7 +4,7 @@ module Spree
with_options inverse_of: :stock_items do
belongs_to :stock_location, class_name: 'Spree::StockLocation'
- belongs_to :variant, class_name: 'Spree::Variant'
+ belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant'
end
has_many :stock_movements, inverse_of: :stock_item
@@ -56,10 +56,6 @@ module Spree
in_stock? || backorderable?
end
- def variant
- Spree::Variant.unscoped { super }
- end
-
def reduce_count_on_hand_to_zero
set_count_on_hand(0) if count_on_hand > 0
end
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 3ebcf3efde..2c8848a0bf 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -3,7 +3,7 @@ module Spree
acts_as_paranoid
acts_as_list scope: :product
- belongs_to :product, touch: true, class_name: 'Spree::Product', inverse_of: :variants
+ belongs_to :product, -> { with_deleted }, touch: true, class_name: 'Spree::Product', inverse_of: :variants
belongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true
delegate :name, :name=, :description, :slug, :available_on, :shipping_category_id,
@@ -150,13 +150,6 @@ module Spree
!!deleted_at
end
- # Product may be created with deleted_at already set,
- # which would make AR's default finder return nil.
- # This is a stopgap for that little problem.
- def product
- Spree::Product.unscoped { super }
- end
-
def options=(options = {})
options.each do |option|
set_option_value(option[:name], option[:value])