Posts filed under ‘rake’
Rake task to prepare test database from migrations
To use your migrations for preparing test database create a file testdb.rake
in the directory lib/tasks.
Now add the following code in the lib/tasks/testdb.rake file…
module Rake
module TaskManager
def redefine_task(task_class, args, &block)
task_name, deps = resolve_args(args)
task_name = task_class.scope_name(@scope, task_name)
deps = [deps] unless deps.respond_to?(:to_ary)
deps = deps.collect {|d| d.to_s }
task = @tasks[task_name.to_s] = task_class.new(task_name, self)
task.application = self
task.add_comment(@last_comment)
@last_comment = nil
task.enhance(deps, &block)
task
end
end
class Task
class << self
def redefine_task(args, &block)
Rake.application.redefine_task(self, args, &block)
end
end
end
end
def redefine_task(args, &block)
Rake::Task.redefine_task(args, &block)
end
namespace :db do
namespace :test do
desc 'Prepare the test database and migrate schema'
redefine_task :prepare => :environment do
Rake::Task['db:test:migrate_schema'].invoke
end
desc 'Use the migrations to create the test database'
task :migrate_schema => 'db:test:purge' do
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
ActiveRecord::Migrator.migrate("db/migrate/")
end
end
end
To prepare the test database from migrations run this command
$ rake db:test:migrate_schema
Rake task to remove the image and audio files created by captcha
To add a rake task to the rails application which removes all the image and audio files created by captcha, create a file named remove_captcha_files.rake in the lib/tasks directory.
Add the following code to the lib/tasks/remove_captcha_files.rake file…
desc "Remove captcha images and audio files"
task :remove_captcha_files do
image_path = "#{RAILS_ROOT}/public/images/captcha/"
audio_path = "#{RAILS_ROOT}/public/captcha_audio/"
Dir.foreach(image_path){|file| File.delete(image_path+file) if (/^.*.jpg$/).match(file)} if File.exist?(image_path)
Dir.foreach(audio_path){|file| File.delete(audio_path+file) if (/^.*.wav$/).match(file)} if File.exist?(audio_path)
puts "Captcha files removed."
end
You can confirm that task is added to your app by running
rake –task
To remove all the files created by captcha, simply run the command
rake remove_captcha_files
from the command line from your application root.
To better view the code visit here




Rss Feeds