Class: SagaForge::InstallGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- SagaForge::InstallGenerator
- Includes:
- ActiveRecord::Generators::Migration, Generators::MigrationActions
- Defined in:
- lib/generators/saga_forge/install/install_generator.rb
Overview
Creates the SagaForge initializer and installs its migration into a new application. Idempotent: migrations that already exist are skipped, so re-running is safe. Multi-database aware: with --database (or an already configured config.database), the migration lands in db/NAME_migrate.
Constant Summary
Constants included from Generators::MigrationActions
Generators::MigrationActions::MIGRATIONS
Instance Method Summary collapse
- #copy_initializer ⇒ Object
- #copy_migrations ⇒ Object
- #print_next_steps ⇒ Object
-
#set_database_config ⇒ Object
With --database, record config.database in the initializer so later generator runs (e.g. saga_forge:upgrade after a gem update) still target the right directory without the flag.
Methods included from Generators::MigrationActions
#copy_saga_forge_migrations, included, #saga_forge_database, #saga_forge_migration_exists?, #saga_forge_migrations_dir
Instance Method Details
#copy_initializer ⇒ Object
20 21 22 |
# File 'lib/generators/saga_forge/install/install_generator.rb', line 20 def copy_initializer template "initializer.rb", "config/initializers/saga_forge.rb" end |
#copy_migrations ⇒ Object
35 36 37 38 39 40 |
# File 'lib/generators/saga_forge/install/install_generator.rb', line 35 def copy_migrations copy_saga_forge_migrations rescue => err say "#{err.class}: #{err}\n#{err.backtrace.join("\n")}", :red exit 1 end |
#print_next_steps ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/generators/saga_forge/install/install_generator.rb', line 42 def print_next_steps if (db = saga_forge_database) say <<~MSG Add the '#{db}' database to config/database.yml (per environment), e.g.: #{db}: <<: *default database: myapp_#{db} migrations_paths: db/#{db}_migrate then run: bin/rails db:migrate:#{db} MSG else say "\nNext: run bin/rails db:migrate" end end |
#set_database_config ⇒ Object
With --database, record config.database in the initializer so later generator runs (e.g. saga_forge:upgrade after a gem update) still target the right directory without the flag. --database=primary means "stay on the default connection", so nothing is recorded.
28 29 30 31 32 33 |
# File 'lib/generators/saga_forge/install/install_generator.rb', line 28 def set_database_config return unless (db = saga_forge_database) gsub_file "config/initializers/saga_forge.rb", /^\s*#?\s*config\.database\s*=.*$/, %( config.database = :#{db}) end |