Merge pull request #7914 from aignatov-bio/ai-sci-11136-change-creator-after-duplicate-locations

Change create by on location duplicate [SCI-11136]
This commit is contained in:
aignatov-bio 2024-10-03 11:22:01 +02:00 committed by GitHub
commit ac57267140
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!
new_storage_location = @storage_location.duplicate!(current_user)
if new_storage_location
@storage_location = new_storage_location
log_activity('storage_location_created')

View file

@ -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