mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Merge pull request #10832 from Matt-Yorkley/cloning
Clone all ExchangeVariant objects in bulk
This commit is contained in:
@@ -86,9 +86,9 @@ class Exchange < ApplicationRecord
|
||||
exchange = dup
|
||||
exchange.order_cycle = new_order_cycle
|
||||
exchange.enterprise_fee_ids = enterprise_fee_ids
|
||||
exchange.variant_ids = variant_ids
|
||||
exchange.tag_ids = tag_ids
|
||||
exchange.save!
|
||||
clone_all_exchange_variants(exchange.id)
|
||||
exchange
|
||||
end
|
||||
|
||||
@@ -105,4 +105,17 @@ class Exchange < ApplicationRecord
|
||||
|
||||
receiver.touch_later
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# An Order Cycle can have thousands of ExchangeVariants.
|
||||
# It's a simple association without any callbacks on creation. So we can
|
||||
# insert in bulk and improve the performance tenfold for large order cycles.
|
||||
def clone_all_exchange_variants(exchange_id)
|
||||
return unless variant_ids.any?
|
||||
|
||||
ExchangeVariant.insert_all( # rubocop:disable Rails/SkipsModelValidations
|
||||
variant_ids.map{ |variant_id| { variant_id: variant_id, exchange_id: exchange_id } }
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user