Merge pull request #10860 from Matt-Yorkley/order-cycle-deleting

Delete exchange variants in bulk when deleting an order cycle
This commit is contained in:
Filipe
2023-06-01 13:49:32 +01:00
committed by GitHub
3 changed files with 21 additions and 13 deletions

View File

@@ -16,7 +16,7 @@ class Exchange < ApplicationRecord
belongs_to :sender, class_name: 'Enterprise'
belongs_to :receiver, class_name: 'Enterprise'
has_many :exchange_variants, dependent: :destroy
has_many :exchange_variants, dependent: :delete_all
has_many :variants, through: :exchange_variants
has_many :exchange_fees, dependent: :destroy
@@ -25,6 +25,8 @@ class Exchange < ApplicationRecord
validates :order_cycle, :sender, :receiver, presence: true
validates :sender_id, uniqueness: { scope: [:order_cycle_id, :receiver_id, :incoming] }
before_destroy :delete_related_exchange_variants, prepend: true
after_save :touch_receiver
accepts_nested_attributes_for :variants
@@ -118,4 +120,13 @@ class Exchange < ApplicationRecord
variant_ids.map{ |variant_id| { variant_id: variant_id, exchange_id: exchange_id } }
)
end
def delete_related_exchange_variants
return unless incoming?
ExchangeVariant.where(variant_id: variant_ids).
joins(:exchange).
where(exchanges: { order_cycle: order_cycle, incoming: false }).
delete_all
end
end

View File

@@ -3,9 +3,15 @@
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
return unless exchange.incoming?
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)