Created
June 4, 2009 19:34
-
-
Save e1senh0rn/123792 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'highline/import' | |
namespace :db do | |
desc 'Populates DB in interactive mode with sample data' | |
task :populate => :environment do | |
puts | |
if agree("Do you want me to create admin user? ") | |
Factory :admin_user, | |
:login => 'admin', | |
:name => 'Administrator', | |
:password => 'adminus', | |
:password_confirmation => 'adminus' | |
say "Administrative user crated! Login - <%= color('admin', BOLD) %>, password - <%= color('adminus', BOLD) %>" | |
end | |
puts | |
count = ActiveSupport::OrderedHash.new([[:user,50], [:artist,60], [:song,200], [:comment,100]]) | |
count.each do |model,default| | |
count[model] = ask("How many <%= color('#{model.to_s.pluralize}', BOLD) %> do you want?\t", Integer) {|q| q.default = default } | |
end | |
puts | |
count.each do |model,quantity| | |
print 'Creating ' + model.to_s.pluralize | |
quantity.times do | |
print '.' | |
case model | |
# simple factory | |
when :user, :artist | |
Factory model | |
# not so simple factory | |
when :song | |
Factory :song, | |
:artist => Artist.find(:first, :offset => rand(Artist.count :all)), | |
:user => (rand(2)) ? User.find(:first, :offset => rand(User.count :all)) : nil # I love ruby ninja style!!! | |
# ouch, that's one complicated! | |
when :comment | |
if rand(2) | |
# user's comment | |
Factory :comment, | |
:song => Song.find(:first, :offset => rand(Song.count :all)), | |
:user => User.find(:first, :offset => rand(User.count :all)) | |
else | |
# anonymous comment | |
Factory :anonymous_comment, | |
:song => Song.find(:first, :offset => rand(Song.count :all)) | |
end | |
end # case | |
end # count[model].times | |
puts 'Done!' | |
end # count.each | |
end # End db:populate task | |
desc "Migrates to version 0 and than migrates back up to latest migration" | |
task :reset_schema => :environment do | |
ActiveRecord::Migrator.migrate("db/migrate/", 0) | |
ActiveRecord::Migrator.migrate("db/migrate/") | |
end | |
desc "Drops all table and recreates it using migration. Then fills it with sample data." | |
task :bootstrap => :environment do | |
Rake::Task["db:reset_schema"].invoke | |
Rake::Task["db:populate"].invoke | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment