Show default parent classes for generators in help texts

Print default parent class for controller, job, and model generators.

Before:

[--parent=PARENT]               # The parent class for the generated job

After:

[--parent=PARENT]               # The parent class for the generated job
                                # Default: ApplicationJob
This commit is contained in:
Gannon McGibbon 2022-07-25 13:59:06 -05:00
parent 97f13c2b82
commit e1635ff4a7
3 changed files with 13 additions and 25 deletions

View File

@ -7,7 +7,7 @@ module Rails # :nodoc:
class JobGenerator < Rails::Generators::NamedBase # :nodoc:
class_option :queue, type: :string, default: "default", desc: "The queue name for the generated job"
class_option :parent, type: :string, desc: "The parent class for the generated job"
class_option :parent, type: :string, default: "ApplicationJob", desc: "The parent class for the generated job"
check_class_collision suffix: "Job"
@ -28,16 +28,8 @@ module Rails # :nodoc:
end
private
def parent
options[:parent]
end
def parent_class_name
if parent
parent
else
"ApplicationJob"
end
options[:parent]
end
def file_name

View File

@ -11,7 +11,7 @@ module ActiveRecord
class_option :migration, type: :boolean
class_option :timestamps, type: :boolean
class_option :parent, type: :string, desc: "The parent class for the generated model"
class_option :parent, type: :string, default: "ApplicationRecord", desc: "The parent class for the generated model"
class_option :indexes, type: :boolean, default: true, desc: "Add indexes for references and belongs_to columns"
class_option :primary_key_type, type: :string, desc: "The type for primary key"
class_option :database, type: :string, aliases: %i(--db), desc: "The database for your model's migration. By default, the current environment's primary database is used."
@ -24,7 +24,7 @@ module ActiveRecord
end
def create_model_file
generate_abstract_class if database && !parent
generate_abstract_class if database && !custom_parent?
template "model.rb", File.join("app/models", class_path, "#{file_name}.rb")
end
@ -40,7 +40,7 @@ module ActiveRecord
# - options parent is present and database option is not present
# - migrations option is nil or false
def skip_migration_creation?
parent && !database || !migration
custom_parent? && !database || !migration
end
def attributes_with_index
@ -49,12 +49,12 @@ module ActiveRecord
# Used by the migration template to determine the parent name of the model
def parent_class_name
if parent
if custom_parent?
parent
elsif database
abstract_class_name
else
"ApplicationRecord"
parent
end
end
@ -77,6 +77,10 @@ module ActiveRecord
options[:parent]
end
def custom_parent?
parent != self.class.class_options[:parent].default
end
def migration
options[:migration]
end

View File

@ -6,7 +6,7 @@ module Rails
argument :actions, type: :array, default: [], banner: "action action"
class_option :skip_routes, type: :boolean, desc: "Don't add routes to config/routes.rb."
class_option :helper, type: :boolean
class_option :parent, type: :string, desc: "The parent class for the generated controller"
class_option :parent, type: :string, default: "ApplicationController", desc: "The parent class for the generated controller"
check_class_collision suffix: "Controller"
@ -26,16 +26,8 @@ module Rails
end
private
def parent
options[:parent]
end
def parent_class_name
if parent
parent
else
"ApplicationController"
end
options[:parent]
end
def file_name