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

0

My API uses the gem shrine to effect many different types of uploads. Unfortunately during development the files are accumulating in the public/uploads folder every time I db:drop of the database and re-runs.

My Factory Girl injects these files through fixtures.

Can every time you run the db:drop command, delete all files in the public/uploads folder?

    
asked by anonymous 03.01.2017 / 14:57

1 answer

0

The solution is simple, create a task called cleanup with 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

Whenever calling the command db:drop will be invoked then cleanup:uploads .

I hope it helps other developers, this is very useful!

    
03.01.2017 / 14:59