diff --git a/app/controllers/storage_locations_controller.rb b/app/controllers/storage_locations_controller.rb index d9f81cba5..122f54225 100644 --- a/app/controllers/storage_locations_controller.rb +++ b/app/controllers/storage_locations_controller.rb @@ -86,7 +86,7 @@ class StorageLocationsController < ApplicationController def duplicate ActiveRecord::Base.transaction do - new_storage_location = @storage_location.duplicate! + new_storage_location = @storage_location.duplicate!(current_user) if new_storage_location @storage_location = new_storage_location log_activity('storage_location_created') diff --git a/app/models/storage_location.rb b/app/models/storage_location.rb index 03fb0a0b3..d444e185c 100644 --- a/app/models/storage_location.rb +++ b/app/models/storage_location.rb @@ -60,13 +60,14 @@ class StorageLocation < ApplicationRecord storage_location_repository_rows.count.zero? end - def duplicate! + def duplicate!(user) ActiveRecord::Base.transaction do new_storage_location = dup new_storage_location.name = next_clone_name + new_storage_location.created_by = user new_storage_location.save! copy_image(self, new_storage_location) - recursive_duplicate(id, new_storage_location.id) + recursive_duplicate(id, new_storage_location.id, user) new_storage_location rescue ActiveRecord::RecordInvalid false @@ -142,13 +143,14 @@ class StorageLocation < ApplicationRecord private - def recursive_duplicate(old_parent_id = nil, new_parent_id = nil) + def recursive_duplicate(old_parent_id = nil, new_parent_id = nil, user = nil) StorageLocation.where(parent_id: old_parent_id).find_each do |child| new_child = child.dup new_child.parent_id = new_parent_id + new_child.created_by = user new_child.save! copy_image(child, new_child) - recursive_duplicate(child.id, new_child.id) + recursive_duplicate(child.id, new_child.id, user) end end