|
| 1 | +require 'rake' |
| 2 | + |
| 3 | +require ::File.expand_path('../config/environment', __FILE__) |
| 4 | + |
| 5 | +# Include all of ActiveSupport's core class extensions, e.g., String#camelize |
| 6 | +require 'active_support/core_ext' |
| 7 | + |
| 8 | +namespace :generate do |
| 9 | + desc "Create an empty model in app/models, e.g., rake generate:model NAME=User" |
| 10 | + task :model do |
| 11 | + unless ENV.has_key?('NAME') |
| 12 | + raise "Must specificy model name, e.g., rake generate:model NAME=User" |
| 13 | + end |
| 14 | + |
| 15 | + model_name = ENV['NAME'].camelize |
| 16 | + model_filename = ENV['NAME'].underscore + '.rb' |
| 17 | + model_path = APP_ROOT.join('app', 'models', model_filename) |
| 18 | + |
| 19 | + if File.exist?(model_path) |
| 20 | + raise "ERROR: Model file '#{model_path}' already exists" |
| 21 | + end |
| 22 | + |
| 23 | + puts "Creating #{model_path}" |
| 24 | + File.open(model_path, 'w+') do |f| |
| 25 | + f.write(<<-EOF.strip_heredoc) |
| 26 | + class #{model_name} < ActiveRecord::Base |
| 27 | + # Remember to create a migration! |
| 28 | + end |
| 29 | + EOF |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + desc "Create an empty migration in db/migrate, e.g., rake generate:migration NAME=create_tasks" |
| 34 | + task :migration do |
| 35 | + unless ENV.has_key?('NAME') |
| 36 | + raise "Must specificy migration name, e.g., rake generate:migration NAME=create_tasks" |
| 37 | + end |
| 38 | + |
| 39 | + name = ENV['NAME'].camelize |
| 40 | + filename = "%s_%s.rb" % [Time.now.strftime('%Y%m%d%H%M%S'), ENV['NAME'].underscore] |
| 41 | + path = APP_ROOT.join('db', 'migrate', filename) |
| 42 | + |
| 43 | + if File.exist?(path) |
| 44 | + raise "ERROR: File '#{path}' already exists" |
| 45 | + end |
| 46 | + |
| 47 | + puts "Creating #{path}" |
| 48 | + File.open(path, 'w+') do |f| |
| 49 | + f.write(<<-EOF.strip_heredoc) |
| 50 | + class #{name} < ActiveRecord::Migration |
| 51 | + def change |
| 52 | + end |
| 53 | + end |
| 54 | + EOF |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + desc "Create an empty model spec in spec, e.g., rake generate:spec NAME=user" |
| 59 | + task :spec do |
| 60 | + unless ENV.has_key?('NAME') |
| 61 | + raise "Must specificy migration name, e.g., rake generate:spec NAME=user" |
| 62 | + end |
| 63 | + |
| 64 | + name = ENV['NAME'].camelize |
| 65 | + filename = "%s_spec.rb" % ENV['NAME'].underscore |
| 66 | + path = APP_ROOT.join('spec', filename) |
| 67 | + |
| 68 | + if File.exist?(path) |
| 69 | + raise "ERROR: File '#{path}' already exists" |
| 70 | + end |
| 71 | + |
| 72 | + puts "Creating #{path}" |
| 73 | + File.open(path, 'w+') do |f| |
| 74 | + f.write(<<-EOF.strip_heredoc) |
| 75 | + require 'spec_helper' |
| 76 | + describe #{name} do |
| 77 | + pending "add some examples to (or delete) #{__FILE__}" |
| 78 | + end |
| 79 | + EOF |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | +end |
| 84 | + |
| 85 | +namespace :db do |
| 86 | + desc "Drop, create, and migrate the database" |
| 87 | + task :reset => [:drop, :create, :migrate] |
| 88 | + |
| 89 | + desc "Create the databases at #{DB_NAME}" |
| 90 | + task :create do |
| 91 | + puts "Creating development and test databases if they don't exist..." |
| 92 | + system("createdb #{APP_NAME}_development && createdb #{APP_NAME}_test") |
| 93 | + end |
| 94 | + |
| 95 | + desc "Drop the database at #{DB_NAME}" |
| 96 | + task :drop do |
| 97 | + puts "Dropping development and test databases..." |
| 98 | + system("dropdb #{APP_NAME}_development && dropdb #{APP_NAME}_test") |
| 99 | + end |
| 100 | + |
| 101 | + desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)." |
| 102 | + task :migrate do |
| 103 | + ActiveRecord::Migrator.migrations_paths << File.dirname(__FILE__) + 'db/migrate' |
| 104 | + ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true |
| 105 | + ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil) do |migration| |
| 106 | + ENV["SCOPE"].blank? || (ENV["SCOPE"] == migration.scope) |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + desc "rollback your migration--use STEP=number to step back multiple times" |
| 111 | + task :rollback do |
| 112 | + step = (ENV['STEP'] || 1).to_i |
| 113 | + ActiveRecord::Migrator.rollback('db/migrate', step) |
| 114 | + Rake::Task['db:version'].invoke if Rake::Task['db:version'] |
| 115 | + end |
| 116 | + |
| 117 | + desc "Populate the database with dummy data by running db/seeds.rb" |
| 118 | + task :seed do |
| 119 | + require APP_ROOT.join('db', 'seeds.rb') |
| 120 | + end |
| 121 | + |
| 122 | + desc "Returns the current schema version number" |
| 123 | + task :version do |
| 124 | + puts "Current version: #{ActiveRecord::Migrator.current_version}" |
| 125 | + end |
| 126 | + |
| 127 | + namespace :test do |
| 128 | + desc "Migrate test database" |
| 129 | + task :prepare do |
| 130 | + system "rake db:migrate RACK_ENV=test" |
| 131 | + end |
| 132 | + end |
| 133 | +end |
| 134 | + |
| 135 | +desc 'Start IRB with application environment loaded' |
| 136 | +task "console" do |
| 137 | + exec "irb -r./config/environment" |
| 138 | +end |
| 139 | + |
| 140 | +task :default => :spec |
0 commit comments