Module: Railsless::ActiveRecord::Rake
- Defined in:
- lib/railsless/active_record/rake.rb
Constant Summary collapse
- TEMPLATES_PATH =
File.('../../../templates', File.dirname(__FILE__))
Class Method Summary collapse
- .load_database_tasks!(config) ⇒ Object
-
.load_generator_tasks!(config) ⇒ Object
Creates database.yml files and migrations.
- .load_tasks!(config = nil) ⇒ Object
Class Method Details
.load_database_tasks!(config) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/railsless/active_record/rake.rb', line 20 def load_database_tasks!(config) extend ::Rake::DSL # ActiveRecord looks to db:load_config for its DatabaseTasks setup. # Load it up with our own configuration first. namespace :db do task :load_config do ::ActiveRecord::Tasks::DatabaseTasks.root = config.root ::ActiveRecord::Tasks::DatabaseTasks.env = config.env ::ActiveRecord::Tasks::DatabaseTasks.db_dir = config.db_path ::ActiveRecord::Tasks::DatabaseTasks.database_configuration = config.db_config ::ActiveRecord::Tasks::DatabaseTasks.migrations_paths = config.migrations_path ::ActiveRecord::Tasks::DatabaseTasks.seed_loader = SeedLoader.new(config.seeds_path) ::ActiveRecord::Tasks::DatabaseTasks.fixtures_path = nil # TODO end end # Stub out the :environment task that DatabaseTasks depends on. # Ideally, the app using this library loads the app from within an # :environment task (as per the README), but provide this as a fallback. unless ::Rake::Task.task_defined? :environment task :environment end load 'active_record/railties/databases.rake' end |
.load_generator_tasks!(config) ⇒ Object
Creates database.yml files and migrations.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/railsless/active_record/rake.rb', line 48 def load_generator_tasks!(config) extend ::Rake::DSL namespace :db do namespace :generate do desc "Generate and write a config/database.yml" task :config do db_config_path = config.db_config_path if File.exists?(db_config_path) puts "Database config already exists at #{db_config_path}; skipping..." else FileUtils.mkdir_p(config.config_path) FileUtils.cp(File.join(TEMPLATES_PATH, 'database.yml'), db_config_path) end end desc "Generate a database migration, eg: rake db:generate:migration NAME=CreatePosts" task :migration do migrations_path = config.migrations_path = Time.now.strftime("%Y%m%d%H%M%S") name = ENV.fetch('NAME') do fail "Usage: rake db:generate:migration NAME=CreatePosts" end # Normalise the name to a MigrationClass and a migration_filename migration_class = ActiveSupport::Inflector.camelize(name) migration_filename = "#{}_#{ActiveSupport::Inflector.underscore(name)}.rb" migration_path = File.join(migrations_path, migration_filename) template = File.read(File.join(TEMPLATES_PATH, 'migration.erb')) FileUtils.mkdir_p(migrations_path) File.write(migration_path, ERB.new(template).result(binding)) puts "Created migration: #{migration_path}" end end end end |
.load_tasks!(config = nil) ⇒ Object
14 15 16 17 18 |
# File 'lib/railsless/active_record/rake.rb', line 14 def load_tasks!(config=nil) config ||= Railsless::ActiveRecord::Config.new load_database_tasks!(config) load_generator_tasks!(config) end |