Usage
You need to tell RuboCop to load the Rails extension. There are three ways to do this:
RuboCop configuration file
Put this into your .rubocop.yml
.
require: rubocop-rails
Now you can run rubocop
and it will automatically load the RuboCop Rails
cops together with the standard cops.
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 autocorretion to code generated by bin/rails g
.
module YourCoolApp
class Application < Rails::Application
config.generators.after_generate do |files|
parsable_files = files.filter { |file| file.end_with?('.rb') }
system("bundle exec rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true)
end
end
end
It uses rubocop -A
to apply Style/FrozenStringLiteralComment
and other unsafe autocorretion 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.