Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add contexts to seeding.

  • Loading branch information...
commit 853fb81bce163daf0d5dad7b36b22a875b339163 1 parent daf4088
Jason Harrelson authored September 19, 2010
56  lib/generators/prepare_genesis/templates/genesis.rake
@@ -2,9 +2,18 @@ namespace :db do
2 2
   desc "Loads seed data for the current environment."
3 3
   task :genesis => :environment do
4 4
     Genesis::Seeder.verify_or_create_version_table
5  
-    ignores = %w()
6  
-    seeds = Dir[File.join( RAILS_ROOT, 'db', 'seeds', '*.rb' )] +
7  
-            Dir[File.join( RAILS_ROOT, 'db', 'seeds', RAILS_ENV, '*.rb') ]
  5
+    ignores = %w(genesis_common.rb)
  6
+    seeds = Dir[File.join( Rails.root, 'db', 'seeds', '*.rb' )] +
  7
+            Dir[File.join( Rails.root, 'db', 'seeds', Rails.env, '*.rb') ]
  8
+
  9
+    contexts = ENV['CONTEXTS']
  10
+    unless contexts.nil? || contexts.empty?
  11
+      contexts = expand_contexts if contexts == 'all'
  12
+      contexts.split( ',' ).each do |context|
  13
+        seeds += Dir[File.join( Rails.root, 'db', 'seeds', 'contexts', context, '*.rb' )]
  14
+      end
  15
+    end
  16
+
8 17
     Genesis::Seeder.run( seeds, ENV['VERSION'] || nil, ignores )
9 18
   end
10 19
 
@@ -14,6 +23,35 @@ namespace :db do
14 23
     Rake::Task['db:genesis'].invoke
15 24
   end
16 25
 
  26
+  desc "An alias for the db:genesis task"
  27
+  task :seed => :environment do
  28
+    Rake::Task['db:genesis'].invoke
  29
+  end
  30
+
  31
+  desc "An alias for the db:regenesis task"
  32
+  task :reseed => :environment do
  33
+    Rake::Task['db:regenesis'].invoke
  34
+  end
  35
+
  36
+  desc "Removes all data, runs migrations and then seeds the database"
  37
+  task :regenesis => :environment do
  38
+    ActiveRecord::Base.connection.tables.select { |t| !['schema_migrations', 'schema_seeds', 'versions', 'sessions'].include?( t ) }.each do |table|
  39
+      puts "Emptying the #{table} table"
  40
+      klass = table.classify.to_s.constantize
  41
+      klass.delete_all
  42
+    end
  43
+
  44
+    puts ''
  45
+
  46
+    Genesis::SchemaSeed.delete_all
  47
+    ActiveRecord::Base.connection.execute( 'DELETE FROM `versions`' )
  48
+    ActiveRecord::Base.connection.execute( 'DELETE FROM `sessions`' )
  49
+
  50
+    Rake::Task['db:migrate'].invoke
  51
+    Rake::Task['db:test:prepare'].invoke
  52
+    Rake::Task['db:genesis'].invoke
  53
+  end
  54
+
17 55
   namespace :genesis do
18 56
     desc "Returns the current seed version from teh schema_seeds table"
19 57
     task :version => :environment do
@@ -21,3 +59,15 @@ namespace :db do
21 59
     end
22 60
   end
23 61
 end
  62
+
  63
+def seeds_root
  64
+  File.join( Rails.root, 'db', 'seeds' )
  65
+end
  66
+
  67
+def contexts_root
  68
+  File.join( seeds_root, 'contexts' )
  69
+end
  70
+
  71
+def expand_contexts
  72
+  Dir[File.join( contexts_root, '*'  )].map { |d| d.split( '/' ).last }.join ','
  73
+end
3  lib/genesis.rb
@@ -6,7 +6,8 @@
6 6
 require 'genesis/schema_seed'
7 7
 
8 8
 module Genesis
9  
-  VERSION = '1.1.0'
  9
+  VERSION = '1.2.0'
  10
+  SEEDS_ROOT = 'db/seeds'
10 11
 end
11 12
 
12 13
 ActiveRecord::Base.send :include, Genesis::ActiveRecordExtensions if defined? ActiveRecord::Base
2  lib/genesis/seeder.rb
@@ -107,7 +107,7 @@ def self.parse_to_version_and_name( seed_file_name )
107 107
     end
108 108
 
109 109
     def self.run_seeds
110  
-      callbacks = File.join( RAILS_ROOT, 'db', 'seeds', 'genesis_callbacks.rb' )
  110
+      callbacks = File.join( Rails.root, 'db', 'seeds', 'genesis_callbacks.rb' )
111 111
       if File.exists?( callbacks )
112 112
         load( callbacks )
113 113
         should_run_callbacks = true
56  rails_generators/prepare_seeding/templates/genesis.rake
@@ -2,9 +2,18 @@ namespace :db do
2 2
   desc "Loads seed data for the current environment."
3 3
   task :genesis => :environment do
4 4
     Genesis::Seeder.verify_or_create_version_table
5  
-    ignores = %w()
6  
-    seeds = Dir[File.join( RAILS_ROOT, 'db', 'seeds', '*.rb' )] +
7  
-            Dir[File.join( RAILS_ROOT, 'db', 'seeds', RAILS_ENV, '*.rb') ]
  5
+    ignores = %w(genesis_common.rb)
  6
+    seeds = Dir[File.join( Rails.root, 'db', 'seeds', '*.rb' )] +
  7
+            Dir[File.join( Rails.root, 'db', 'seeds', Rails.env, '*.rb') ]
  8
+
  9
+    contexts = ENV['CONTEXTS']
  10
+    unless contexts.nil? || contexts.empty?
  11
+      contexts = expand_contexts if contexts == 'all'
  12
+      contexts.split( ',' ).each do |context|
  13
+        seeds += Dir[File.join( Rails.root, 'db', 'seeds', 'contexts', context, '*.rb' )]
  14
+      end
  15
+    end
  16
+
8 17
     Genesis::Seeder.run( seeds, ENV['VERSION'] || nil, ignores )
9 18
   end
10 19
 
@@ -14,6 +23,35 @@ namespace :db do
14 23
     Rake::Task['db:genesis'].invoke
15 24
   end
16 25
 
  26
+  desc "An alias for the db:genesis task"
  27
+  task :seed => :environment do
  28
+    Rake::Task['db:genesis'].invoke
  29
+  end
  30
+
  31
+  desc "An alias for the db:regenesis task"
  32
+  task :reseed => :environment do
  33
+    Rake::Task['db:regenesis'].invoke
  34
+  end
  35
+
  36
+  desc "Removes all data, runs migrations and then seeds the database"
  37
+  task :regenesis => :environment do
  38
+    ActiveRecord::Base.connection.tables.select { |t| !['schema_migrations', 'schema_seeds', 'versions', 'sessions'].include?( t ) }.each do |table|
  39
+      puts "Emptying the #{table} table"
  40
+      klass = table.classify.to_s.constantize
  41
+      klass.delete_all
  42
+    end
  43
+
  44
+    puts ''
  45
+
  46
+    Genesis::SchemaSeed.delete_all
  47
+    ActiveRecord::Base.connection.execute( 'DELETE FROM `versions`' )
  48
+    ActiveRecord::Base.connection.execute( 'DELETE FROM `sessions`' )
  49
+
  50
+    Rake::Task['db:migrate'].invoke
  51
+    Rake::Task['db:test:prepare'].invoke
  52
+    Rake::Task['db:genesis'].invoke
  53
+  end
  54
+
17 55
   namespace :genesis do
18 56
     desc "Returns the current seed version from teh schema_seeds table"
19 57
     task :version => :environment do
@@ -21,3 +59,15 @@ namespace :db do
21 59
     end
22 60
   end
23 61
 end
  62
+
  63
+def seeds_root
  64
+  File.join( Rails.root, 'db', 'seeds' )
  65
+end
  66
+
  67
+def contexts_root
  68
+  File.join( seeds_root, 'contexts' )
  69
+end
  70
+
  71
+def expand_contexts
  72
+  Dir[File.join( contexts_root, '*'  )].map { |d| d.split( '/' ).last }.join ','
  73
+end

0 notes on commit 853fb81

Please sign in to comment.
Something went wrong with that request. Please try again.