mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
20 lines
495 B
Ruby
20 lines
495 B
Ruby
# frozen_string_literal: true
|
|
|
|
class ExchangeVariant < ApplicationRecord
|
|
self.belongs_to_required_by_default = false
|
|
|
|
belongs_to :exchange
|
|
belongs_to :variant, class_name: 'Spree::Variant'
|
|
|
|
after_destroy :delete_related_outgoing_variants
|
|
|
|
def delete_related_outgoing_variants
|
|
return unless exchange.incoming?
|
|
|
|
ExchangeVariant.where(variant_id:).
|
|
joins(:exchange).
|
|
where(exchanges: { order_cycle: exchange.order_cycle, incoming: false }).
|
|
delete_all
|
|
end
|
|
end
|