|
33 | 33 |
|
34 | 34 | == Rails configuration tip |
35 | 35 |
|
36 | | -If you are using Rails 6.1 or newer, add the following `config.generators.after_generate` setting to |
37 | | -your config/application.rb to apply RuboCop autocorrection to code generated by `bin/rails g`. |
| 36 | +In Rails 6.1+, add the following `config.generators.after_generate` setting to |
| 37 | +your `config/environments/development.rb` to apply RuboCop autocorrection to code generated by `bin/rails g`. |
38 | 38 |
|
39 | 39 | [source,ruby] |
40 | 40 | ---- |
41 | | -module YourCoolApp |
42 | | - class Application < Rails::Application |
43 | | - config.generators.after_generate do |files| |
44 | | - parsable_files = files.filter { |file| File.exist?(file) && file.end_with?('.rb') } |
45 | | - unless parsable_files.empty? |
46 | | - system("bundle exec rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true) |
47 | | - end |
| 41 | +# config/environments/development.rb |
| 42 | +Rails.application.configure do |
| 43 | + config.generators.after_generate do |files| |
| 44 | + parsable_files = files.filter { |file| file.end_with?('.rb') } |
| 45 | + unless parsable_files.empty? |
| 46 | + system("bundle exec rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true) |
48 | 47 | end |
49 | 48 | end |
50 | 49 | end |
|
53 | 52 | It uses `rubocop -A` to apply `Style/FrozenStringLiteralComment` and other unsafe autocorrection cops. |
54 | 53 | `rubocop -A` is unsafe autocorrection, but code generated by default is simple and less likely to |
55 | 54 | be incompatible with `rubocop -A`. If you have problems you can replace it with `rubocop -a` instead. |
| 55 | + |
| 56 | +In Rails 7.2+, it is recommended to use `config.generators.apply_rubocop_autocorrect_after_generate!` instead of the above setting: |
| 57 | + |
| 58 | +[source,diff] |
| 59 | +---- |
| 60 | + # config/environments/development.rb |
| 61 | + Rails.application.configure do |
| 62 | + (snip) |
| 63 | + # Apply autocorrection by RuboCop to files generated by `bin/rails generate`. |
| 64 | +- # config.generators.apply_rubocop_autocorrect_after_generate! |
| 65 | ++ config.generators.apply_rubocop_autocorrect_after_generate! |
| 66 | + end |
| 67 | +---- |
| 68 | + |
| 69 | +You only need to uncomment. |
0 commit comments