Skip to content

Commit

Permalink
[Doc] Sync the Rails configuration tip with the README
Browse files Browse the repository at this point in the history
  • Loading branch information
koic committed Sep 13, 2024
1 parent 5dc42d5 commit 38cec18
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions docs/modules/ROOT/pages/usage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

0 comments on commit 38cec18

Please sign in to comment.