Skip to content

Commit

Permalink
Added rake task documentation and cleaned up rake task. Introduced SE…
Browse files Browse the repository at this point in the history
…ED environment variable to allow users to filter the list of seeds to load.

Signed-off-by: Michael Bleigh <michael@intridea.com>
  • Loading branch information
zdennis authored and Michael Bleigh committed Mar 3, 2009
1 parent dafdd2e commit 8e6f92d
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions tasks/seed_fu_tasks.rake
@@ -1,16 +1,44 @@
namespace :db do
desc "Loads seed data from db/fixtures for the current environment."
desc <<-EOS
Loads seed data for the current environment. It will look for
ruby seed files in <RAILS_ROOT>/db/fixtures/ and
<RAILS_ROOT>/db/fixtures/<RAILS_ENV>/.
By default it will load any ruby files found. You can filter the files
loaded by passing in the SEED environment variable with a comma-delimited
list of patterns to include. Any files not matching the pattern will
not be loaded.
You can also change the directory where seed files are looked for
with the FIXTURE_PATH environment variable.
Examples:
# default, to load all seed files for the current environment
rake db:seed
# to load seed files matching orders or customers
rake db:seed SEED=orders,customers
# to load files from RAILS_ROOT/features/fixtures
rake db:seed FIXTURE_PATH=features/fixtures
EOS
task :seed => :environment do
fixture_path = ENV["FIXTURE_PATH"] ? ENV["FIXTURE_PATH"] : "db/fixtures"
Dir[File.join(RAILS_ROOT, fixture_path, '*.rb')].sort.each { |fixture|
puts "\n== Seeding from #{File.split(fixture).last} " + ("=" * (60 - (17 + File.split(fixture).last.length)))
load fixture
puts "=" * 60 + "\n"
}
Dir[File.join(RAILS_ROOT, fixture_path, RAILS_ENV, '*.rb')].sort.each { |fixture|
puts "\n== [#{RAILS_ENV}] Seeding from #{File.split(fixture).last} " + ("=" * (60 - (20 + File.split(fixture).last.length + RAILS_ENV.length)))
load fixture
puts "=" * 60 + "\n"
}

global_seed_files = Dir[File.join(RAILS_ROOT, fixture_path, '*.rb')].sort
env_specific_seed_files = Dir[File.join(RAILS_ROOT, fixture_path, RAILS_ENV, '*.rb')]
potential_seed_files = (global_seed_files + env_specific_seed_files).uniq

if ENV["SEED"]
filter = ENV["SEED"].gsub(/,/, "|")
potential_seed_files.reject!{ |file| true unless file =~ /#{filter}/ }
puts "\n == Filtering seed files against regexp: #{filter}"
end

potential_seed_files.each do |file|
pretty_name = file.sub("#{RAILS_ROOT}/", "")
puts "\n== Seed from #{pretty_name} " + ("=" * (60 - (17 + File.split(file).last.length)))
load file
end
end
end

0 comments on commit 8e6f92d

Please sign in to comment.