---
title: "Install and use Rubocop - TLDR"
author: "Yaroslav Shmarov"
date: "2021-08-03"
canonical_url: "https://blog.superails.com/install-and-use-rubocop"
markdown_url: "https://blog.superails.com/install-and-use-rubocop.md"
description: "Rubocop is one of the most essential gems that I always add to all my apps. Rails 8 will have Rubocop included by default."
tags: ["ruby","rails","rubyonrails","rubocop","code-quality"]
license: "GNU AGPL v3. Credit Yaroslav Shmarov and link the canonical URL."
---

# Install and use Rubocop - TLDR

[Rubocop](https://github.com/rubocop/rubocop-rails) is one of the most essential gems that I always add to all my apps.

Rails 8 [will have Rubocop](https://github.com/rails/rubocop-rails-omakase/blob/main/rubocop.yml) included by default.

[Official docs](https://docs.rubocop.org/rubocop-rails/index.html)

### 1. Installation

```ruby
# Gemfile
group :development, :test do
  gem 'rubocop-rails', require: false
end
```

Create a config file:

```shell
# console
bundle
echo > .rubocop.yml
```

My basic setup:

```yml
plugins:
  - rubocop-rails

AllCops:
  NewCops: enable
  TargetRubyVersion: 3.4
  Exclude:
    - '**/db/schema.rb'
    - '**/db/**/*'
    - 'config/**/*'
    - 'bin/*'
    - 'vendor/bundle/**/*'
    - 'node_modules/**/*'
    - app/models/concerns/obfuscates_id.rb

Style/FrozenStringLiteralComment:
  Enabled: false

Style/ClassAndModuleChildren:
  Enabled: false

Metrics/BlockLength:
  Enabled: false

Layout/LineLength:
  Enabled: false

Metrics/AbcSize:
  Enabled: false

Metrics/MethodLength:
  Enabled: false

Style/Documentation:
  Enabled: false

Bundler/OrderedGems:
  Enabled: false

Lint/MissingSuper:
  Enabled: false

Metrics/ModuleLength:
  Enabled: false

Metrics/CyclomaticComplexity:
  Enabled: false

Rails/RenderInline:
  Enabled: false

Metrics/PerceivedComplexity:
  Enabled: false

Metrics/ParameterLists:
  Enabled: false

Naming/PredicateMethod:
  Enabled: false

Style/StringLiterals:
  EnforcedStyle: double_quotes

# important for erb lint not to remove opening <% tags in html.erb files

Layout/LeadingEmptyLines:
  Enabled: false

# would like to enable these later

Style/OpenStructUse:
  Enabled: false

Rails/OutputSafety:
  Enabled: false
```

### 2. Run the cops

```shell
# console - run check:
bundle exec rubocop
# console - run check on specific file/folder:
rubocop app/models/user.rb
```

### 3. Disable cops

1. Disable in a file, around a code block:

app/models/user.rb

```ruby
  # rubocop: disable Metrics/AbcSize, Metrics/MethodLength
  def full_name
    ...
  end
  # rubocop: enable Metrics/AbcSize, Metrics/MethodLength
```  

2. Disable on a whole file:

```yml
# .rubocop.yml
Metrics/ClassLength:
  Exclude:
    - 'app/models/user.rb'
    - 'app/controllers/users_controller.rb'
```

### 4. AutoCorrect

```shell
# console - safe auto correct
rubocop -a

# console - dangerous auto correct
rubocop - A

# console - autocorrect a single specific cop
bundle exec rubocop -a --only Style/FrozenStringLiteralComment
bundle exec rubocop -A --only Layout/EmptyLineAfterMagicComment

# generate comments for uncorrected problems and stop flagging them as TODO:
rubocop --auto-correct --disable-uncorrectable
```

### 5. Github workflows

```yml
# mkdir .github
# mkdir .github/workflows
# echo > .github/workflows/.lint.yml
name: Code style

on: [pull_request]

jobs:
  lint:
    name: all linters
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
    steps:
      - uses: actions/checkout@v3
      - uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true
      - name: rubocop
        run: bundle exec rubocop --parallel
      # - name: erb-lint
      #   run: bundle exec erblint --lint-all
```

That's it!
