Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parent option defaults #45659

Merged
merged 2 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 2 additions & 10 deletions activejob/lib/rails/generators/job/job_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Generators # :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"

Expand All @@ -28,16 +28,8 @@ def create_job_file
end

private
def parent
options[:parent]
end

def parent_class_name
if parent
parent
else
"ApplicationJob"
end
options[:parent]
end

def file_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ModelGenerator < Base # :nodoc:

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."
Expand All @@ -24,7 +24,7 @@ def create_migration_file
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

Expand All @@ -40,7 +40,7 @@ def create_module_file
# - 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
Expand All @@ -49,12 +49,12 @@ def attributes_with_index

# 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

Expand All @@ -77,6 +77,10 @@ def parent
options[:parent]
end

def custom_parent?
parent != self.class.class_options[:parent].default
end

def migration
options[:migration]
end
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/generators/rails/controller/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Examples:

`bin/rails generate controller users index --skip-routes`

This generates a `UsersController` with an index acion and no routes.
This generates a `UsersController` with an index action and no routes.

`bin/rails generate controller admin/dashboard --parent=admin_controller`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ControllerGenerator < NamedBase # :nodoc:
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"

Expand All @@ -26,16 +26,8 @@ def add_routes
end

private
def parent
options[:parent]
end

def parent_class_name
if parent
parent
else
"ApplicationController"
end
options[:parent]
end

def file_name
Expand Down