Class: Rsg::Generators::Orm::ActiveRecordGenerator

Inherits:
Base
  • Object
show all
Includes:
Rails::Generators::Database
Defined in:
lib/rsg/generators/orm/active_record_generator.rb

Constant Summary collapse

SUPPORTED_DBS =
%w(sqlite3 mysql postgres)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_pathsObject



12
13
14
15
16
17
# File 'lib/rsg/generators/orm/active_record_generator.rb', line 12

def self.source_paths
  @__source_paths ||= [
    Rails::Generators::AppGenerator.source_root,
    Pathname.new(__FILE__).dirname.join("templates").expand_path
  ]
end

.source_rootObject



8
9
10
# File 'lib/rsg/generators/orm/active_record_generator.rb', line 8

def self.source_root
  Pathname.new(__FILE__).dirname.join("templates").expand_path
end

Instance Method Details



19
20
21
# File 'lib/rsg/generators/orm/active_record_generator.rb', line 19

def banner
  say "Configuring active record"
end

#configure_driverObject



27
28
29
30
31
32
33
34
# File 'lib/rsg/generators/orm/active_record_generator.rb', line 27

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

#enable_active_recordObject



23
24
25
# File 'lib/rsg/generators/orm/active_record_generator.rb', line 23

def enable_active_record
  enable_railtie "active_record"
end

#migrateObject



42
43
44
45
46
47
48
49
# File 'lib/rsg/generators/orm/active_record_generator.rb', line 42

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

#write_db_sampleObject



36
37
38
39
40
# File 'lib/rsg/generators/orm/active_record_generator.rb', line 36

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