Simplify exchange variant callback and prefer delete over destroy

Delete is faster than destroy but should only be used if the object has no callbacks or touches.
This commit is contained in:
Matt-Yorkley
2023-05-19 10:54:15 +01:00
parent 434c9ae110
commit a8559e621f
2 changed files with 7 additions and 12 deletions

View File

@@ -3,9 +3,13 @@
class ExchangeVariant < ApplicationRecord
belongs_to :exchange
belongs_to :variant, class_name: 'Spree::Variant'
after_destroy :destroy_related_outgoing_variants
def destroy_related_outgoing_variants
VariantDeleter.new.destroy_related_outgoing_variants(variant_id, exchange.order_cycle)
after_destroy :delete_related_outgoing_variants
def delete_related_outgoing_variants
ExchangeVariant.where(variant_id: variant_id).
joins(:exchange).
where(exchanges: { order_cycle: exchange.order_cycle, incoming: false }).
delete_all
end
end

View File

@@ -11,15 +11,6 @@ class VariantDeleter
variant.destroy
end
def destroy_related_outgoing_variants(variant_id, order_cycle)
internal_variants = ExchangeVariant.where(variant_id: variant_id).
joins(:exchange).
where(
exchanges: { order_cycle: order_cycle, incoming: false }
)
internal_variants.destroy_all
end
private
def only_variant_on_product?(variant)