From a6025d3a123b2bf687f55764def285c5b7900502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 26 Jul 2021 23:39:08 +0200 Subject: [PATCH] Simplify copying files in Active Storage migration We can use the `ActiveStorage::Blob` class to find where the file is supposed to be stored instead of manually setting the path. This is also more robust because it works with Active Storage configurations which don't store files in the default folder. --- lib/tasks/active_storage.rake | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/tasks/active_storage.rake b/lib/tasks/active_storage.rake index f6ce1a5ea..762e2d33e 100644 --- a/lib/tasks/active_storage.rake +++ b/lib/tasks/active_storage.rake @@ -61,16 +61,11 @@ namespace :active_storage do end ActiveStorage::Attachment.find_each do |attachment| + dest = ActiveStorage::Blob.service.path_for(attachment.blob.key) name = attachment.name source = attachment.record.send(name).path - dest_dir = File.join( - "storage", - attachment.blob.key.first(2), - attachment.blob.key.first(4).last(2)) - dest = File.join(dest_dir, attachment.blob.key) - - FileUtils.mkdir_p(dest_dir) + FileUtils.mkdir_p(File.dirname(dest)) logger.info "Copying #{source} to #{dest}" FileUtils.cp(source, dest) end