From 6da536e5c8870cb30594a7397e074803080e2708 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Mon, 15 Jul 2019 17:29:39 +0100 Subject: [PATCH] Add a rake task to move user avatars to Active Storage --- lib/tasks/upgrade_avatars.rake | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 lib/tasks/upgrade_avatars.rake diff --git a/lib/tasks/upgrade_avatars.rake b/lib/tasks/upgrade_avatars.rake new file mode 100644 index 000000000..03939dad8 --- /dev/null +++ b/lib/tasks/upgrade_avatars.rake @@ -0,0 +1,15 @@ +namespace "storage" do + task :upgrade_avatars => :environment do + User.active.where.not(:image_file_name => nil).in_batches.each_record do |user| + io = File.open(user.image.path) + filename = user.image.original_filename + content_type = if user.image.content_type.nil? + MimeMagic.by_magic(io)&.type + else + user.image.content_type + end + + user.avatar.attach(:io => io, :filename => filename, :content_type => content_type) + end + end +end -- 2.39.5