Permalink
Browse files

Make Genesis a Railtie.

- Load rake tasks using Railtie.
- Load ActiveRecordExtensions using Railtie.
  • Loading branch information...
1 parent fd7bb09 commit 68fb69726eef3d3f6f963f7196655d281ff56fbd @midas committed Mar 24, 2013
@@ -15,7 +15,6 @@ def self.source_root
def install_seeding
envs.each { |env| empty_directory "db/seeds/#{env}" }
- copy_file 'genesis.rake', 'lib/tasks/genesis.rake'
copy_file 'genesis_callbacks.rb', 'db/seeds/genesis_callbacks.rb'
end
View
@@ -1,12 +1,13 @@
-$:.unshift(File.dirname(__FILE__)) unless
- $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
+#$:.unshift(File.dirname(__FILE__)) unless
+ #$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
require 'genesis/active_record_extensions'
-require 'genesis/seeder'
+require 'genesis/railtie'
require 'genesis/schema_seed'
+require 'genesis/seeder'
module Genesis
SEEDS_ROOT = 'db/seeds'
end
-ActiveRecord::Base.send :include, Genesis::ActiveRecordExtensions if defined? ActiveRecord::Base
+#ActiveRecord::Base.send :include, Genesis::ActiveRecordExtensions if defined? ActiveRecord::Base
View
@@ -0,0 +1,16 @@
+require 'genesis'
+require 'rails'
+
+class Railtie < Rails::Railtie
+
+ initializer 'genesis.insert_into_active_record' do |app|
+ ActiveSupport.on_load :active_record do
+ ActiveRecord::Base.send :include, Genesis::ActiveRecordExtensions
+ end
+ end
+
+ rake_tasks do
+ Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
+ end
+
+end
@@ -1,4 +1,5 @@
namespace :db do
+
desc "Loads seed data for the current environment."
task :genesis => :environment do
Genesis::Seeder.verify_or_create_version_table
@@ -22,12 +23,31 @@ namespace :db do
puts message( contexts, :using_contexts => using_contexts ), "", ""
end
- desc "Drops and recreates all tables along with seeding the database"
+ desc "Recreates the databse by migrating down to VERSION=0 and then db:migrate and db:seed"
task :mulligan => :environment do
- Rake::Task['db:migrate:reset'].invoke
+ raise 'Cannot seed production' if ENV['RAILS_ENV'] == 'production' || Rails.env.production?
+
+ ENV['VERSION']= '0'
+ Rake::Task['db:migrate'].invoke
+ Rake::Task['db:migrate'].reenable
+ ENV.delete 'VERSION'
+ Rake::Task["db:migrate"].invoke
+ Genesis::SchemaSeed.delete_all
Rake::Task['db:genesis'].invoke
end
+ namespace :mulligan do
+
+ desc 'Recreates database using db:migrate:reset and db:seed (helpful when an irreversible migration is blocking db:mulligan)'
+ task :reset => :environment do
+ raise 'Cannot seed production' if ENV['RAILS_ENV'] == 'production' || Rails.env.production?
+
+ Rake::Task['db:migrate:reset'].invoke
+ Rake::Task['db:genesis'].invoke
+ end
+
+ end
+
desc "An alias for the db:genesis task"
task :seed => :environment do
Rake::Task['db:genesis'].invoke
@@ -38,8 +58,10 @@ namespace :db do
Rake::Task['db:regenesis'].invoke
end
- desc "Removes all data, runs migrations and then seeds the database"
+ desc "Removes all data and then seeds the database"
task :regenesis => :environment do
+ raise 'Cannot seed production' if ENV['RAILS_ENV'] == 'production' || Rails.env.production?
+
ActiveRecord::Base.connection.tables.select { |t| !['schema_migrations', 'schema_seeds', 'versions', 'sessions'].include?( t ) }.each do |table|
puts "Emptying the #{table} table"
klass = table.classify.to_s.constantize
@@ -49,11 +71,7 @@ namespace :db do
puts ''
Genesis::SchemaSeed.delete_all
- ActiveRecord::Base.connection.execute( 'DELETE FROM `versions`' )
- ActiveRecord::Base.connection.execute( 'DELETE FROM `sessions`' )
- Rake::Task['db:migrate'].invoke
- Rake::Task['db:test:prepare'].invoke
Rake::Task['db:genesis'].invoke
end
View
@@ -1,88 +0,0 @@
-namespace :db do
- desc "Loads seed data for the current environment."
- task :genesis => :environment do
- Genesis::Seeder.verify_or_create_version_table
- ignores = %w(genesis_common.rb)
- seeds = Dir[File.join( Rails.root, 'db', 'seeds', '*.rb' )] +
- Dir[File.join( Rails.root, 'db', 'seeds', Rails.env, '*.rb') ]
-
- contexts = ENV['CONTEXTS']
- unless contexts.nil? || contexts.empty?
- using_contexts = true
- contexts = expand_contexts if contexts == 'all'
- contexts.split( ',' ).each do |context|
- seeds += Dir[File.join( Rails.root, 'db', 'seeds', 'contexts', context, '*.rb' )]
- end
- end
-
-
-
- puts "", message( contexts, :using_contexts => using_contexts, :start => true ), ""
- Genesis::Seeder.run( seeds, ENV['VERSION'] || nil, ignores )
- puts message( contexts, :using_contexts => using_contexts ), "", ""
- end
-
- desc "Drops and recreates all tables along with seeding the database"
- task :mulligan => :environment do
- Rake::Task['db:migrate:reset'].invoke
- Rake::Task['db:genesis'].invoke
- end
-
- desc "An alias for the db:genesis task"
- task :seed => :environment do
- Rake::Task['db:genesis'].invoke
- end
-
- desc "An alias for the db:regenesis task"
- task :reseed => :environment do
- Rake::Task['db:regenesis'].invoke
- end
-
- desc "Removes all data, runs migrations and then seeds the database"
- task :regenesis => :environment do
- ActiveRecord::Base.connection.tables.select { |t| !['schema_migrations', 'schema_seeds', 'versions', 'sessions'].include?( t ) }.each do |table|
- puts "Emptying the #{table} table"
- klass = table.classify.to_s.constantize
- klass.delete_all
- end
-
- puts ''
-
- Genesis::SchemaSeed.delete_all
- ActiveRecord::Base.connection.execute( 'DELETE FROM `versions`' )
- ActiveRecord::Base.connection.execute( 'DELETE FROM `sessions`' )
-
- Rake::Task['db:migrate'].invoke
- Rake::Task['db:test:prepare'].invoke
- Rake::Task['db:genesis'].invoke
- end
-
- namespace :genesis do
- desc "Returns the current seed version from teh schema_seeds table"
- task :version => :environment do
- puts "[Genesis Seed Version] #{Genesis::Seeder.get_current_version}"
- end
- end
-end
-
-def seeds_root
- File.join( Rails.root, 'db', 'seeds' )
-end
-
-def contexts_root
- File.join( seeds_root, 'contexts' )
-end
-
-def expand_contexts
- Dir[File.join( contexts_root, '*' )].map { |d| d.split( '/' ).last }.join ','
-end
-
-def message( contexts, options={} )
- msg = options[:using_contexts] ?
- "*** #{start_or_end_word( options )} seeding (contexts: #{contexts.split(',').join(', ')})" :
- "*** #{start_or_end_word( options )} seeding"
-end
-
-def start_or_end_word( options )
- return options[:start] ? 'Start' : 'End'
-end

0 comments on commit 68fb697

Please sign in to comment.