mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-14 04:04:23 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user