Skip to content

Latest commit

 

History

History
107 lines (73 loc) · 1.93 KB

rake.md

File metadata and controls

107 lines (73 loc) · 1.93 KB

enhancing existing rake task

... or how to call native Rails rake task plus my own task

Rake::Task['db:create'].enhance do
  Rake::Task['db:my_task'].invoke
end

rake prompt

... or console input

task :say_hi do
  puts "What's your name?"
  name = $stdin.gets.chomp
  puts "Hi #{name}!"
end

from https://gist.github.com/pixelmatrix/486467

run execute other task from task

Rake::Task['db:schema:load'].invoke

keywonds: invoke

Show all rake tasks

rake -T
  • however this will print only tasks that have descrition(desc)

Rake run rspec by default

#Rakefile
require "bundler/gem_tasks"
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec

if you need to override default Rails task

desc "this will be now a default task"
task info: :environment do
    puts 'Run rake test to test'
end

task(:default).clear.enhance ['info']

rake defaults

namespace :foo do
  namespace :bar do
    task :car do
    end
  end
  task bar: "bar:car"
end

task default: "foo:bar"

Use rails url/path helpers in rake task

# lib/tasks/generate_page.rake
include Rails.application.routes.url_helpers
default_url_options[:host] = "myroutes.check"

namespace :page do
  task :generate => :environment do
    puts root_url
  end
end

for rails 2.x you have to include include ActionController::UrlWriter

alternativly you can use:

p Rails.application.routes.url_helpers.posts_path
p Rails.application.routes.url_helpers.posts_url(:host => "example.com")