How to remove upload files after the db:drop command in Rails?

Asked

Viewed 92 times

0

My API uses the Gem shrine to effect several different types of uploads. Unfortunately during development the files are accumulating in the folder public/uploads every time I do db:drop of the database and road again.

My Factories (Factory Girl) inject these files through fixtures.

It is possible that every time you rotate the command db:drop delete all files in the folder public/uploads?

1 answer

0


The solution is simple, create a task called cleanup with the method uploads:

rails g task cleanup uploads

Now add the code below to this task:

namespace :cleanup do
  desc 'Removes all Shrine uploads'
  task uploads: :environment do
    FileUtils.rm_rf Dir.glob("#{Rails.root}/public/uploads/*")
  end
end

Rake::Task['db:drop'].enhance do
  Rake::Task['cleanup:uploads'].invoke
end

Every time I call command db:drop shall then be invoked cleanup:uploads.

I hope you help other developers, this is very useful!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.