From 2cb8567baf14a820da50ee2f15630279042a88ad Mon Sep 17 00:00:00 2001 From: Ladislav Smola Date: Tue, 1 Aug 2017 13:23:58 +0200 Subject: [PATCH] Allow batch disconnect for the batch strategy Allow batch disconnect for the batch strategy --- .../saver/concurrent_safe_batch.rb | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/app/models/manager_refresh/save_collection/saver/concurrent_safe_batch.rb b/app/models/manager_refresh/save_collection/saver/concurrent_safe_batch.rb index ad1d9efa612..6d89a467f1c 100644 --- a/app/models/manager_refresh/save_collection/saver/concurrent_safe_batch.rb +++ b/app/models/manager_refresh/save_collection/saver/concurrent_safe_batch.rb @@ -99,11 +99,21 @@ def save!(inventory_collection, association) end def destroy_records(records) - # TODO(lsmola) we need at least batch disconnect. Batch destroy won't be probably possible because of the - # :dependent => :destroy. - ActiveRecord::Base.transaction do - records.each do |record| - delete_record!(inventory_collection, record) + return false unless inventory_collection.delete_allowed? + return if records.blank? + + # Is the delete_method rails standard deleting method? + rails_delete = %i(destroy delete).include?(inventory_collection.delete_method) + if !rails_delete && inventory_collection.model_class.respond_to?(inventory_collection.delete_method) + # We have custom delete method defined on a class, that means it supports batch destroy + inventory_collection.model_class.public_send(inventory_collection.delete_method, records.map(&:id)) + else + # We have either standard :destroy and :delete rails method, or custom instance level delete method + # Note: The standard :destroy and :delete rails method can't be batched because of the hooks and cascade destroy + ActiveRecord::Base.transaction do + records.each do |record| + delete_record!(inventory_collection, record) + end end end end