From 38cec18985618f9879d7308b37af0f8100c35f1a Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 14 Sep 2024 05:09:07 +0900 Subject: [PATCH] [Doc] Sync the Rails configuration tip with the README --- docs/modules/ROOT/pages/usage.adoc | 32 +++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) 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.