Merge pull request #7946 from aignatov-bio/ai-sci-11161-update-duplicate-for-shared-location

Fix shared location duplication [SCI-11161]
This commit is contained in:
aignatov-bio 2024-10-08 16:10:57 +02:00 committed by GitHub
commit a9553814ef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 5 deletions

View file

@ -86,7 +86,7 @@ class StorageLocationsController < ApplicationController
def duplicate
ActiveRecord::Base.transaction do
new_storage_location = @storage_location.duplicate!(current_user)
new_storage_location = @storage_location.duplicate!(current_user, current_team)
if new_storage_location
@storage_location = new_storage_location
log_activity('storage_location_created')

View file

@ -61,14 +61,15 @@ class StorageLocation < ApplicationRecord
storage_location_repository_rows.count.zero?
end
def duplicate!(user)
def duplicate!(user, team)
ActiveRecord::Base.transaction do
new_storage_location = dup
new_storage_location.name = next_clone_name
new_storage_location.team = team unless parent_id
new_storage_location.created_by = user
new_storage_location.save!
copy_image(self, new_storage_location)
recursive_duplicate(id, new_storage_location.id, user)
recursive_duplicate(id, new_storage_location.id, user, new_storage_location.team)
new_storage_location
rescue ActiveRecord::RecordInvalid
false
@ -144,14 +145,15 @@ class StorageLocation < ApplicationRecord
private
def recursive_duplicate(old_parent_id = nil, new_parent_id = nil, user = nil)
def recursive_duplicate(old_parent_id = nil, new_parent_id = nil, user = nil, team = 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.team = team
new_child.created_by = user
new_child.save!
copy_image(child, new_child)
recursive_duplicate(child.id, new_child.id, user)
recursive_duplicate(child.id, new_child.id, user, team)
end
end