Update default scopes with acts_as_paranoid

Fixes an issue where the LineItem :sorted_by_name_and_unit_value scope was not working with removal of the default scopes on line item and variant, which meant that the join in the scope was excluding soft-deleted items that should not have been excluded.
This commit is contained in:
Matt-Yorkley
2021-03-26 18:22:05 +00:00
parent 5b3fd25a78
commit 1b19d4bdee
3 changed files with 14 additions and 23 deletions

View File

@@ -10,7 +10,7 @@ module Spree
include LineItemBasedAdjustmentHandling
belongs_to :order, class_name: "Spree::Order", inverse_of: :line_items
belongs_to :variant, class_name: "Spree::Variant"
belongs_to :variant, -> { with_deleted }, class_name: "Spree::Variant"
belongs_to :tax_category, class_name: "Spree::TaxCategory"
has_one :product, through: :variant
@@ -154,18 +154,6 @@ module Spree
@preferred_shipment = shipment
end
# Remove product default_scope `deleted_at: nil`
def product
variant.product
end
# This ensures that LineItems always have access to soft-deleted variants.
# In some situations, unscoped super will be nil. In these cases,
# we fetch the variant using variant_id. See issue #4946 for more details.
def variant
Spree::Variant.unscoped { super } || Spree::Variant.unscoped.find(variant_id)
end
def cap_quantity_at_stock!
scoper.scope(variant)
return if variant.on_demand

View File

@@ -13,7 +13,8 @@ module Spree
acts_as_paranoid
belongs_to :product, touch: true, class_name: 'Spree::Product'
belongs_to :product, -> { with_deleted }, touch: true, class_name: 'Spree::Product'
delegate_belongs_to :product, :name, :description, :permalink, :available_on,
:tax_category_id, :shipping_category_id, :meta_description,
:meta_keywords, :tax_category, :shipping_category
@@ -188,13 +189,6 @@ module Spree
price_in(currency).try(:amount)
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
# can_supply? is implemented in VariantStock
def in_stock?(quantity = 1)
can_supply?(quantity)

View File

@@ -190,8 +190,17 @@ module Spree
end
end
it "finds line items sorted by name and unit_value" do
expect(o.line_items.sorted_by_name_and_unit_value).to eq([li6, li5, li4, li3])
describe "#sorted_by_name_and_unit_value" do
it "finds line items sorted by name and unit_value" do
expect(o.line_items.sorted_by_name_and_unit_value).to eq([li6, li5, li4, li3])
end
it "includes soft-deleted products/variants" do
li3.variant.product.destroy
expect(o.line_items.reload.sorted_by_name_and_unit_value).to eq([li6, li5, li4, li3])
expect(o.line_items.sorted_by_name_and_unit_value.to_sql).to_not match "deleted_at"
end
end
it "finds line items from a given order cycle" do