Class: StepperMotor::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
ActiveRecord::Generators::Migration
Defined in:
lib/generators/install_generator.rb

Overview

The generator is used to install StepperMotor. It adds an example Journey, a configing initializer and the migration that creates tables. Run it with bin/rails g stepper_motor:install in your console.

Constant Summary collapse

UUID_MESSAGE =
<<~MSG
  If set, uuid type will be used for hero_id of the Journeys, as well as for the Journey IDs.
  Use this if most of your models use UUD as primary key"
MSG

Instance Method Summary collapse

Instance Method Details

#create_initializerObject



33
34
35
36
37
# File 'lib/generators/install_generator.rb', line 33

def create_initializer
  # Take the initializer code from the test dummy app, it is the best place really.
  initializer_code_from_dummy_app = File.read(__dir__ + "/../../test/dummy/config/initializers/stepper_motor.rb")
  create_file "config/initializers/stepper_motor.rb", initializer_code_from_dummy_app
end

#create_migration_fileObject

Generates monolithic migration file that contains all database changes.



22
23
24
25
26
27
28
29
30
31
# File 'lib/generators/install_generator.rb', line 22

def create_migration_file
  # Migration files are named "...migration_001.rb" etc. This allows them to be emitted
  # as they get added, and the order of the migrations can be controlled using predictable sorting.
  # Adding a new migration to the gem is then just adding a file.
  migration_file_paths_in_order = Dir.glob(__dir__ + "/*_migration_*.rb.erb").sort
  migration_file_paths_in_order.each do |migration_template_path|
    untemplated_migration_filename = File.basename(migration_template_path).gsub(/\.erb$/, "")
    migration_template(migration_template_path, File.join(db_migrate_path, untemplated_migration_filename))
  end
end