How to install Rubocop in your Rails app

April 05, 2020

Rubocop is a static code analyzer and formatter for Ruby. Rubocop is a static code analyzer and formatter for Ruby. It can point out things like "the code doesn't match the style guide". Rubocop-rails is a plugin for RuboCop that focuses on applying Rails best practices and coding conventions. Install it together.

Install Rubocop

Gemfile
group :development do
  gem 'rubocop', require: false
  gem 'rubocop-rails', require: false
end

Since we won't be using rubocop in our Rails app, we've set require to false. This will make your Rails app start a little faster and reduce unexpected behavior. For things that are used by bundle exec, we add require: false.

$ bundle install

Create a configuration file for Robocop.

$ touch .rubocop.yml
$ touch .rubocop_todo.yml

This is a sample file. Create your own .rubocop.yml by refactoring it. The details of the configuration values can be found in the official documentation of RuboCop.

.rubocop.yml
inherit_from: .rubocop_todo.yml

require:
  - rubocop-rails
  - rubocop-rspec
  - rubocop-performance

Rails:
  Enabled: true

AllCops:
  TargetRubyVersion: 2.6
  Exclude:
    - app/channels/**/*
    - node_modules/**/*
    - bin/*
    - test/**/*
    - spec/**/*
    - lib/*
    - db/migrate/*
    - db/schema.rb
    - lib/tasks/*
    - public/*
    - tmp/*
    - vendor/**/*
    - config/spring.rb

Style/Documentation:
  Description: 'Document classes and non-namespace modules.'
  Enabled: false

Layout/LineLength:
  Description: 'This cop checks the length of lines in the source code.'
  Max: 120

Style/ClassAndModuleChildren:
  Description: 'This cop checks the style of children definitions at classes and modules.'
  EnforcedStyle: compact

Style/FrozenStringLiteralComment:
  Description: 'This cop  will add the comment # frozen_string_literal: true to the top of files to enable frozen string literals. '
  Enabled: false

Style/HashEachMethods:
  Description:  'This cop checks for uses of each_key and each_value Hash methods.'
  Enabled: true

Style/HashTransformKeys:
  Description:  'This cop looks for uses of _.each_with_object({}) {...}, _.map {...}.to_h, and Hash[_.map {...}] that are actually just transforming the keys of a hash, and tries to use a simpler & faster call to transform_keys instead.'
  Enabled: true

Style/HashTransformValues:
  Description:  'This cop looks for uses of _.each_with_object({}) {...}, _.map {...}.to_h, and Hash[_.map {...}] that are actually just transforming the values of a hash, and tries to use a simpler & faster call to transform_values instead.'
  Enabled: true

How to use

Standard output of analysis results

$ bundle exec rubocop

Run by specifying a file

$ bundle exec rubocop Gemfile
$ bundle exec rubocop app/controllers

Save warnings to .rubycop_todo.yml.

$ bundle exec rubocop --auto-gen-config

Automatic correction

$ bundle exec rubocop --auto-correct

Refactoring flow using Rubocop

# Run auto-correction
$ bundle exec rubocop --auto-correct

# Save warnings to `.rubycop_todo.yml`.
$ bundle exec rubocop --auto-gen-config

# Modify the code while checking the .rubycop_todo.yml. When the modification is completed, delete the target rule in .rubycop_todo.yml

Repeat the above until there are no more warnings.

Extended plugins

rubocop-performance

There are also RuboCop plugins that focus on code performance checks. Official page

rubocop-rspec

There are also plugins that will check the code style of your RSpec file. Official page

Tips

There are also reek and rails_best_practices.

References