-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Controller generator improvements #45627
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,26 @@ | ||
Description: | ||
Generates a new controller and its views. Pass the controller name, either | ||
CamelCased or under_scored, and a list of views as arguments. | ||
CamelCased or under_scored, and a list of actions as arguments. | ||
|
||
To create a controller within a module, specify the controller name as a | ||
path like 'parent_module/controller_name'. | ||
|
||
This generates a controller class in app/controllers and invokes helper, | ||
template engine, assets, and test framework generators. | ||
|
||
Example: | ||
`bin/rails generate controller CreditCards open debit credit close` | ||
Examples: | ||
`bin/rails generate controller credit_cards open debit credit close` | ||
|
||
CreditCards controller with URLs like /credit_cards/debit. | ||
This generates a `CreditCardsController` with routes like /credit_cards/debit. | ||
Controller: app/controllers/credit_cards_controller.rb | ||
Test: test/controllers/credit_cards_controller_test.rb | ||
Views: app/views/credit_cards/debit.html.erb [...] | ||
Helper: app/helpers/credit_cards_helper.rb | ||
|
||
`bin/rails generate controller users index --skip-routes` | ||
|
||
This generates a `UsersController` with an index acion and no routes. | ||
|
||
`bin/rails generate controller admin/dashboard --parent=admin_controller` | ||
|
||
This generates a `Admin::DashboardController` with an `AdminController` parent class. |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,6 +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" | ||||||||||||||
|
||||||||||||||
check_class_collision suffix: "Controller" | ||||||||||||||
|
||||||||||||||
|
@@ -25,6 +26,18 @@ def add_routes | |||||||||||||
end | ||||||||||||||
|
||||||||||||||
private | ||||||||||||||
def parent | ||||||||||||||
options[:parent] | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
def parent_class_name | ||||||||||||||
if parent | ||||||||||||||
parent | ||||||||||||||
else | ||||||||||||||
"ApplicationController" | ||||||||||||||
end | ||||||||||||||
Comment on lines
+34
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it will be a good idea to enable the relevant rubocop's check - https://docs.rubocop.org/rubocop/1.31/cops_style.html#styleredundantcondition There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree this would be better addressed by enabling the cop. Looks like there's only 3 offenses in all of Rails and I've introduced 2 of them. 😅 Would you like to open a PR to introduce this @fatkodima? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea @fatkodima ! 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of adding this method, could a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but I think a more sweeping change could be made to change the other generators too so they look the same, more or less. I'll open another PR to do that. It is likely better to state the default in the class opt so it shows up in the help text. |
||||||||||||||
end | ||||||||||||||
|
||||||||||||||
def file_name | ||||||||||||||
@_file_name ||= remove_possible_suffix(super) | ||||||||||||||
end | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
acion -> action
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Do you want to open a PR?