Permalink
Browse files

Refactored seeds.rake.

Signed-off-by: James McCarthy <james2mccarthy@gmail.com>
  • Loading branch information...
james2m committed Jul 21, 2012
1 parent ae5a451 commit 0acc66fd37245fa608cd5642f10b741609cd9b20
Showing with 39 additions and 14 deletions.
  1. +4 −0 lib/seedbank/dsl.rb
  2. +6 −14 lib/tasks/seed.rake
  3. +29 −0 test/seedbank/dsl_test.rb
View
@@ -21,6 +21,10 @@ def seed_task_from_file(seed_file)
define_seed_task(seed_file, args)
end
+
+ def glob_seed_files_matching(*args, &block)
+ Dir.glob(File.join(seeds_root, *args), &block)
+ end
def define_seed_task(seed_file, *args)
task = Rake::Task.define_task(*args) do |seed_task|
View
@@ -4,32 +4,24 @@ namespace :db do
base_dependencies = ['db:seed:original']
override_dependency = []
- common_dependencies = []
namespace :seed do
# Create seed tasks for all the seeds in seeds_path and add them to the dependency
# list along with the original db/seeds.rb.
- Dir.glob(File.join(seeds_root, '*.seeds.rb')).each do |seed_file|
- common_dependencies << seed_task_from_file(seed_file)
- end
+ common_dependencies = glob_seed_files_matching('*.seeds.rb').map { |seed_file| seed_task_from_file(seed_file) }
desc "Load the seed data from db/seeds.rb and db/seeds/*.seeds.rb."
task 'common' => base_dependencies + common_dependencies
# Glob through the directories under seeds_path assuming they are all environments
# and create a task for each and add it to the dependency list. Then create a task
# for the environment
- Dir.glob(File.join(seeds_root, '/*/')).each do |e|
- environment = File.basename(e)
+ glob_seed_files_matching('/*/').each do |directory|
+ environment = File.basename(directory)
- environment_dependencies = []
- Dir.glob(File.join(seeds_root, environment, '*.seeds.rb')).sort.each do |seed_file|
- environment_dependencies << seed_task_from_file(seed_file)
- end
+ environment_dependencies = glob_seed_files_matching(environment, '*.seeds.rb').sort.map { |seed_file| seed_task_from_file(seed_file) }
- desc <<-EOT
- Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/#{environment}/*.seeds.rb.
- EOT
+ desc "Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/#{environment}/*.seeds.rb."
task environment => ['db:seed:common'] + environment_dependencies
override_dependency << "db:seed:#{environment}" if Rails.env == environment
@@ -41,6 +33,6 @@ namespace :db do
Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/ENVIRONMENT/*.seeds.rb.
ENVIRONMENT is the current environment in Rails.env.
EOT
- override_task :seed => ['db:seed:common'] + override_dependency
+ override_task :seed => ['db:seed:common'] + override_dependency
end
View
@@ -65,6 +65,35 @@
end
+ describe "glob_seed_files_matching" do
+
+ describe "with no namespace" do
+
+ let(:pattern) { '*.seeds.rb' }
+
+ it "returns all the files matching the pattern in seeds_root" do
+ expected_files = Dir.glob(File.join(Seedbank.seeds_root, pattern))
+
+ Seedbank::DSL.glob_seed_files_matching(pattern).must_equal expected_files
+ end
+
+ end
+
+ describe "with a namespace" do
+
+ let(:pattern) { '*.seeds.rb' }
+ let(:namespace) { 'development' }
+
+ it "returns all the files matching the pattern in seeds_root" do
+ expected_files = Dir.glob(File.join(Seedbank.seeds_root, namespace, pattern))
+
+ Seedbank::DSL.glob_seed_files_matching(namespace, pattern).must_equal expected_files
+ end
+
+ end
+
+ end
+
describe "define_seed_task" do
let(:name) { 'scoped:my_seed' }

0 comments on commit 0acc66f

Please sign in to comment.