123456789_123456789_123456789_123456789_123456789_

Class: Rails::Generators::AppGenerator

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, AppBase, Base, Thor::Group
Instance Chain:
self, AppBase, AppName, Base, Actions, Thor::Actions, Thor::Group
Inherits: Rails::Generators::AppBase
Defined in: railties/lib/rails/generators/rails/app/app_generator.rb,
railties/lib/rails/commands/application/application_command.rb

Constant Summary

AppName - Included

RESERVED_NAMES

AppBase - Inherited

BUN_VERSION, CSS_OPTIONS, JAVASCRIPT_OPTIONS, NODE_LTS_VERSION, OPTION_IMPLICATIONS

Class Attribute Summary

  • .exit_on_failure? ⇒ Boolean readonly Internal use only

    We want to exit on failure to be kind to other libraries This is only when accessing via CLI.

Base - Inherited

Class Method Summary

AppBase - Inherited

Base - Inherited

.base_root

Returns the base root for a common set of generators.

.default_source_root

Returns the default source root for a given generator.

.desc

Tries to get the description from a USAGE file one folder above the source root otherwise uses a default description.

.hide!

Convenience method to hide this generator from the available ones when running rails generator command.

.hook_for

Invoke a generator based on the value supplied by the user to the given option named “name”.

.namespace

Convenience method to get the namespace from the class name.

.remove_hook_for

Remove a previously added hook.

.source_root

Returns the source root for this generator using default_source_root as default.

.add_shebang_option!

Small macro to add ruby as an option to the generator with proper default value plus an instance helper method called shebang.

.banner

Use Rails default banner.

.base_name

Sets the base_name taking into account the current class namespace.

.default_aliases_for_option

Returns default aliases for the option name given doing a lookup in aliases.

.default_for_option

Returns default for the option name given doing a lookup in config.

.default_generator_root,
.default_value_for_option

Returns the default value for the option name given doing a lookup in options.

.generator_name

Removes the namespaces and get the generator name.

.usage_path,
.class_option

Make class option aware of options and aliases.

.inherited

Cache source root and add lib/generators/base/generator/templates to source paths.

.hooks

Keep hooks configuration that are used on prepare_for_invocation.

.prepare_for_invocation

Prepare class invocation to search on ::Rails namespace if a previous added hook is being used.

Instance Attribute Summary

Instance Method Summary

AppBase - Inherited

AppName - Included

Base - Inherited

#class_collisions

Check whether the given class names are already taken by user application or Ruby on ::Rails.

#extract_last_module

Takes in an array of nested modules and extracts the last module.

#indent,
#module_namespacing

Wrap block with namespace of current application if namespace exists and is not skipped.

#namespace, #namespace_dirs, #namespaced_path, #wrap_with_namespace

Actions - Included

#add_source

Add the given source to Gemfile

#application
#environment

Adds configuration code to a Rails runtime environment.

#gem

Adds a gem declaration to the Gemfile for the specified gem.

#gem_group

Wraps gem entries inside a group.

#generate

Runs another generator.

#git

Runs one or more git commands.

#github,
#initializer

Creates an initializer file in config/initializers/.

#lib

Creates a file in lib/.

#rails_command

Runs the specified Rails command.

#rake

Runs the specified Rake task.

#rakefile

Creates a Rake tasks file in lib/tasks/.

#readme

Reads the given file at the source root and prints it in the console.

#route

Make an entry in Rails routing file config/routes.rb.

#vendor

Creates a file in vendor/.

#append_file_with_newline

Append string to a file with a newline if necessary.

#execute_command

Runs the supplied command using either “rake …” or “rails …” based on the executor parameter provided.

#indentation

Indent the Gemfile to the depth of @indentation.

#log

Define log for backwards compatibility.

#match_file,
#optimize_indentation

Returns optimized string with indentation.

#quote

Always returns value in double quotes.

#rebase_indentation
#route_namespace_pattern,
#with_indentation

Manage Gemfile indentation for a DSL action block.

#initialize

Constructor Details

.new(*args) ⇒ AppGenerator

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 332

def initialize(*args)
  super

  imply_options(OPTION_IMPLICATIONS, meta_options: META_OPTIONS)

  @after_bundle_callbacks = []
end

Class Attribute Details

.exit_on_failure?Boolean (readonly)

This method is for internal use only.

We want to exit on failure to be kind to other libraries This is only when accessing via CLI

[ GitHub ]

  
# File 'railties/lib/rails/commands/application/application_command.rb', line 11

def self.exit_on_failure?
  true
end

Class Method Details

.apply_rails_template(template, destination)

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 324

def self.apply_rails_template(template, destination) # :nodoc:
  generator = new([destination], { template: template }, { destination_root: destination })
  generator.set_default_accessors!
  generator.apply_rails_template
  generator.run_bundle
  generator.run_after_bundle_callbacks
end

Instance Method Details

#after_bundle(&block) (private)

Registers a callback to be executed after bundle binstubs have run.

after_bundle do
  git add: '.'
end
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 595

def after_bundle(&block) # :doc:
  @after_bundle_callbacks << block
end

#create_active_record_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 418

def create_active_record_files
  return if options[:skip_active_record]
  build(:database_yml)
end

#create_app_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 361

def create_app_files
  build(:app)
end

#create_bin_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 365

def create_bin_files
  build(:bin)
end

#create_boot_file

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 414

def create_boot_file
  template "config/boot.rb"
end

#create_cifiles

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 391

def create_cifiles
  return if skip_ci?
  build(:cifiles)
end

#create_config_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 396

def create_config_files
  build(:config)
end

#create_credentials

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 409

def create_credentials
  build(:credentials)
  build(:credentials_diff_enroll)
end

#create_db_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 423

def create_db_files
  return if options[:skip_active_record]
  build(:db)
end

#create_devcontainer_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 465

def create_devcontainer_files
  return if skip_devcontainer? || options[:dummy_app]
  build(:devcontainer)
end

#create_dockerfiles

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 381

def create_dockerfiles
  return if options[:skip_docker] || options[:dummy_app]
  build(:dockerfiles)
end

#create_lib_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 428

def create_lib_files
  build(:lib)
end

#create_log_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 432

def create_log_files
  build(:log)
end

#create_master_key

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 405

def create_master_key
  build(:master_key)
end

#create_public_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 436

def create_public_files
  build(:public_directory)
end

#create_root_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 345

def create_root_files
  build(:readme)
  build(:rakefile)
  build(:node_version) if using_node?
  build(:ruby_version)
  build(:configru)

  unless options[:skip_git]
    build(:gitignore)
    build(:gitattributes)
  end

  build(:gemfile)
  build(:version_control)
end

#create_rubocop_file

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 386

def create_rubocop_file
  return if skip_rubocop?
  build(:rubocop)
end

#create_script_folder

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 440

def create_script_folder
  return if options[:dummy_app]
  build(:script)
end

#create_storage_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 461

def create_storage_files
  build(:storage) unless skip_storage?
end

#create_system_test_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 457

def create_system_test_files
  build(:system_test) if depends_on_system_test?
end

#create_test_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 453

def create_test_files
  build(:test) unless options[:skip_test]
end

#create_tmp_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 445

def create_tmp_files
  build(:tmp)
end

#create_vendor_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 449

def create_vendor_files
  build(:vendor)
end

#delete_action_cable_files_skipping_action_cable

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 535

def delete_action_cable_files_skipping_action_cable
  if options[:skip_action_cable]
    remove_dir "app/javascript/channels"
  end
end

#delete_action_mailer_files_skipping_action_mailer

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 526

def delete_action_mailer_files_skipping_action_mailer
  if options[:skip_action_mailer]
    remove_file "app/views/layouts/mailer.html.erb"
    remove_file "app/views/layouts/mailer.text.erb"
    remove_dir "app/mailers"
    remove_dir "test/mailers"
  end
end

#delete_active_job_folder_if_skipping_active_job

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 520

def delete_active_job_folder_if_skipping_active_job
  if options[:skip_active_job]
    remove_dir "app/jobs"
  end
end

#delete_api_initializers

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 547

def delete_api_initializers
  unless options[:api]
    remove_file "config/initializers/cors.rb"
  end
end

#delete_app_assets_if_api_option

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 470

def delete_app_assets_if_api_option
  if options[:api]
    remove_dir "app/assets"
  end
end

#delete_app_helpers_if_api_option

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 476

def delete_app_helpers_if_api_option
  if options[:api]
    remove_dir "app/helpers"
    remove_dir "test/helpers"
  end
end

#delete_app_views_if_api_option

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 483

def delete_app_views_if_api_option
  if options[:api]
    if options[:skip_action_mailer]
      remove_dir "app/views"
    else
      remove_file "app/views/layouts/application.html.erb"
      remove_dir  "app/views/pwa"
    end
  end
end

#delete_application_record_skipping_active_record

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 514

def delete_application_record_skipping_active_record
  if options[:skip_active_record]
    remove_file "app/models/application_record.rb"
  end
end

#delete_assets_initializer_skipping_asset_pipeline

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 506

def delete_assets_initializer_skipping_asset_pipeline
  if skip_asset_pipeline?
    remove_file "config/initializers/assets.rb"
    remove_file "app/assets/stylesheets/application.css"
    create_file "app/assets/stylesheets/application.css", "/* Application styles */\n" unless options[:api]
  end
end

#delete_new_framework_defaults

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 553

def delete_new_framework_defaults
  unless options[:update]
    remove_file "config/initializers/new_framework_defaults_#{Rails::VERSION::MAJOR}_#{Rails::VERSION::MINOR}.rb"
  end
end

#delete_non_api_initializers_if_api_option

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 541

def delete_non_api_initializers_if_api_option
  if options[:api]
    remove_file "config/initializers/content_security_policy.rb"
  end
end

#delete_public_files_if_api_option

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 494

def delete_public_files_if_api_option
  if options[:api]
    remove_file "public/400.html"
    remove_file "public/404.html"
    remove_file "public/406-unsupported-browser.html"
    remove_file "public/422.html"
    remove_file "public/500.html"
    remove_file "public/icon.png"
    remove_file "public/icon.svg"
  end
end

#file(*args, &block) (private)

Define file as an alias to create_file for backwards compatibility.

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 585

def file(*args, &block)
  create_file(*args, &block)
end

#finish_template

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 559

def finish_template
  build(:leftovers)
end

#get_builder_class (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 599

def get_builder_class
  defined?(::AppBuilder) ? ::AppBuilder : Rails::AppBuilder
end

#run_after_bundle_callbacks

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 573

def run_after_bundle_callbacks
  @after_bundle_callbacks.each(&:call)
end

#update_active_storage

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 374

def update_active_storage
  unless skip_active_storage?
    rails_command "active_storage:update", inline: true
  end
end

#update_bin_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 369

def update_bin_files
  build(:bin_when_updating)
end

#update_config_files

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 400

def update_config_files
  build(:config_when_updating)
end