class Rsg::Generators::Orm::ActiveRecordGenerator < Rsg::Generators::Base include Rails::Generators::Database SUPPORTED_DBS = %w(sqlite3 mysql postgres) class_option :database, type: :string, default: nil, desc: "Configure for selected database (options: #{SUPPORTED_DBS.join("/")})" class_option :auto_migrate, type: :boolean, default: nil, desc: "Automatically run rake db:migrate" def self.source_root Pathname.new(__FILE__).dirname.join("templates").expand_path end def self.source_paths @__source_paths ||= [ Rails::Generators::AppGenerator.source_root, Pathname.new(__FILE__).dirname.join("templates").expand_path ] end def banner say "Configuring active record" end def enable_active_record enable_railtie "active_record" end def configure_driver database ||= options.fetch(:database) { ask("Which database you'd like to use?", limited_to: SUPPORTED_DBS, default: "sqlite3") } # From the core rails template template "config/databases/#{database}.yml", "config/database.yml" name, version = gem_for_database(database) append_gem name, version: version end def write_db_sample copy_file "db.rake", "lib/tasks/db.rake" copy_file "seeds.rb", "db/seeds.rb" copy_file "samples.rb", "db/samples.rb" end def migrate if options.key?(:auto_migrate) rake "db:create db:migrate" if options[:auto_migrate] return end rake "db:create db:migrate" unless no?("Would you like to create the database schema?") end end