diff --git a/docs/modules/ROOT/pages/usage.adoc b/docs/modules/ROOT/pages/usage.adoc index 1b68c110d4..97cfe75cec 100644 --- a/docs/modules/ROOT/pages/usage.adoc +++ b/docs/modules/ROOT/pages/usage.adoc @@ -33,18 +33,17 @@ end == Rails configuration tip -If you are using Rails 6.1 or newer, add the following `config.generators.after_generate` setting to -your config/application.rb to apply RuboCop autocorrection to code generated by `bin/rails g`. +In Rails 6.1+, add the following `config.generators.after_generate` setting to +your `config/environments/development.rb` to apply RuboCop autocorrection to code generated by `bin/rails g`. [source,ruby] ---- -module YourCoolApp - class Application < Rails::Application - config.generators.after_generate do |files| - parsable_files = files.filter { |file| File.exist?(file) && file.end_with?('.rb') } - unless parsable_files.empty? - system("bundle exec rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true) - end +# config/environments/development.rb +Rails.application.configure do + config.generators.after_generate do |files| + parsable_files = files.filter { |file| file.end_with?('.rb') } + unless parsable_files.empty? + system("bundle exec rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true) end end end @@ -53,3 +52,18 @@ end It uses `rubocop -A` to apply `Style/FrozenStringLiteralComment` and other unsafe autocorrection cops. `rubocop -A` is unsafe autocorrection, but code generated by default is simple and less likely to be incompatible with `rubocop -A`. If you have problems you can replace it with `rubocop -a` instead. + +In Rails 7.2+, it is recommended to use `config.generators.apply_rubocop_autocorrect_after_generate!` instead of the above setting: + +[source,diff] +---- + # config/environments/development.rb + Rails.application.configure do + (snip) + # Apply autocorrection by RuboCop to files generated by `bin/rails generate`. +- # config.generators.apply_rubocop_autocorrect_after_generate! ++ config.generators.apply_rubocop_autocorrect_after_generate! + end +---- + +You only need to uncomment.