Override a rake task
Today I ran into a problem where we have a company gem that includes a bunch of helpful rake tasks, one of which imports a production database into your dev db.
I wanted to do some stuff after this rake import task that was specific to the application I’m working on.
I first tried to overwrite the task, like you would do with any Ruby method. No dice though, this had the effect of running my lib/import.rake task before the gem task that I was trying to override.
I saw a few posts about this but they all seem to be overkill for what I wanted. I didn’t want to monkey patch Rake unless I really had to.
Then I struck gold…
Rake::Task['some:task:name'].clear`
One line to stop the original task and then I could crack on and do whatever I wanted.
namespace :db do
task :import => :environment do
Rake::Task["db:import"].clear
...
Rake::Task["my_post_import_task"].invoke
end
end
Simple.