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

Controller generator improvements #45627

Merged
merged 2 commits into from Jul 22, 2022

Conversation

gmcgibbon
Copy link
Member

Summary

Update controller generator description:

Before:

Usage:
  rails generate controller NAME [action action] [options]

Options:
      [--skip-namespace], [--no-skip-namespace]              # Skip namespace (affects only isolated engines)
      [--skip-collision-check], [--no-skip-collision-check]  # Skip collision check
      [--skip-routes], [--no-skip-routes]                    # Don't add routes to config/routes.rb.
      [--helper], [--no-helper]                              # Indicates when to generate helper
                                                             # Default: true
  -e, [--template-engine=NAME]                               # Template engine to be invoked
                                                             # Default: erb
  -t, [--test-framework=NAME]                                # Test framework to be invoked
                                                             # Default: test_unit

Runtime options:
  -f, [--force]                    # Overwrite files that already exist
  -p, [--pretend], [--no-pretend]  # Run but do not make any changes
  -q, [--quiet], [--no-quiet]      # Suppress status output
  -s, [--skip], [--no-skip]        # Skip files that already exist

Description:
    Generates a new controller and its views. Pass the controller name, either
    CamelCased or under_scored, and a list of views 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`

    CreditCards controller with URLs 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

After:

Usage:
  rails generate controller NAME [action action] [options]

Options:
      [--skip-namespace], [--no-skip-namespace]              # Skip namespace (affects only isolated engines)
      [--skip-collision-check], [--no-skip-collision-check]  # Skip collision check
      [--skip-routes], [--no-skip-routes]                    # Don't add routes to config/routes.rb.
      [--helper], [--no-helper]                              # Indicates when to generate helper
                                                             # Default: true
      [--parent=PARENT]                                      # The parent class for the generated controller
  -e, [--template-engine=NAME]                               # Template engine to be invoked
                                                             # Default: erb
  -t, [--test-framework=NAME]                                # Test framework to be invoked
                                                             # Default: test_unit

Runtime options:
  -f, [--force]                    # Overwrite files that already exist
  -p, [--pretend], [--no-pretend]  # Run but do not make any changes
  -q, [--quiet], [--no-quiet]      # Suppress status output
  -s, [--skip], [--no-skip]        # Skip files that already exist

Description:
    Generates a new controller and its views. Pass the controller name, either
    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.

Examples:
    `bin/rails generate controller credit_cards open debit credit close`

    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 a `AdminController` parent class.

And add a --parent option to the controller generator to specify a superclass. similar to the exist ones on the job and model generators.

- Arguments are actions, not views
- URLs are routes
- Adds example for --skip-routes option
@rails-bot rails-bot bot added the railties label Jul 19, 2022
Comment on lines +34 to +38
if parent
parent
else
"ApplicationController"
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if parent
parent
else
"ApplicationController"
end
parent || "ApplicationController"

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea @fatkodima ! 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of adding this method, could a default: "ApplicationController" be specified on the class_option?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

…f controller.

Example:

`bin/rails g controller admin/users --parent=admin_controller` generates:

```ruby
class Admin::UsersController < AdminController
  # ...
end
```
@gmcgibbon gmcgibbon force-pushed the controller_generator_improvements branch from 0992ea1 to aaeffb2 Compare July 21, 2022 00:20
@gmcgibbon gmcgibbon merged commit 97f13c2 into rails:main Jul 22, 2022
@gmcgibbon gmcgibbon deleted the controller_generator_improvements branch July 22, 2022 19:23

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

acion -> action

Copy link
Member Author

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?

gmcgibbon added a commit to gmcgibbon/rails that referenced this pull request Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants