 Install and use Rubocop - TLDR
       Install and use Rubocop - TLDR
    
  
  
    
    
  Rubocop is one of the most essential gems that I always add to all my apps.
Rails 8 will have Rubocop included by default.
1. Installation #
# Gemfile
group :development, :test do
  gem 'rubocop-rails', require: false
end
Create a config file:
# console
bundle
echo > .rubocop.yml
My basic setup:
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 #
# console - run check:
bundle exec rubocop
# console - run check on specific file/folder:
rubocop app/models/user.rb
3. Disable cops #
- Disable in a file, around a code block:
app/models/user.rb
  # rubocop: disable Metrics/AbcSize, Metrics/MethodLength
  def full_name
    ...
  end
  # rubocop: enable Metrics/AbcSize, Metrics/MethodLength
- Disable on a whole file:
# .rubocop.yml
Metrics/ClassLength:
  Exclude:
    - 'app/models/user.rb'
    - 'app/controllers/users_controller.rb'
4. AutoCorrect #
# 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 #
# 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!
Did you like this article? Did it save you some time?