Skip to content

Commit

Permalink
Make Genesis a Railtie.
Browse files Browse the repository at this point in the history
- Load rake tasks using Railtie.
- Load ActiveRecordExtensions using Railtie.
  • Loading branch information
Jason Harrelson committed Mar 24, 2013
1 parent fd7bb09 commit 68fb697
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 100 deletions.
1 change: 0 additions & 1 deletion lib/generators/genesis/install/install_generator.rb
Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions lib/genesis.rb
@@ -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
16 changes: 16 additions & 0 deletions lib/genesis/railtie.rb
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
88 changes: 0 additions & 88 deletions lib/tasks/genesis.rake

This file was deleted.

0 comments on commit 68fb697

Please sign in to comment.