DEV Community

Cover image for Rails guide - Adding linter - Part 3
Augusto Queirantes
Augusto Queirantes

Posted on • Updated on

Rails guide - Adding linter - Part 3

Hey guys! How are you!

That's the third article of a series that teaches how to create a production ready Rails application. In this article we'll configure linter.

Linter is a tool that assures that all code follows the best practices defined in the project. The tool that we gonna use is called rubocop.

Configurin rubocop

First thing to do is add this gem to the application gemfile, to do so just add the following line in your Gemfile:

gem "rubocop", "~> 1.36.0", require: false
gem "rubocop-rails", "~> 2.16.1", require: false
gem "rubocop-rspec", "~> 2.13.1", require: false
Enter fullscreen mode Exit fullscreen mode

This lines will install rubocop that's the main gem we'll use and rubocop-rails and rubocop-rspec that are dependencies of the main gem. Once you save the file, run the following command to install them:

bundle install
Enter fullscreen mode Exit fullscreen mode

Now we need to create rubocop's configuration file, it's called .rubocop.yml and need to be at the root path of the application, here it's content:

require:
  - rubocop-rspec
  - rubocop-rails

AllCops:
  NewCops: enable
  Exclude:
    - 'app/assets/**/*'
    - 'app/views/**/*'
    - 'db/**/*.rb'
    - 'lib/**/*.rake'
    - 'config/**/*.rb'
    - 'bin/bundle'
Enter fullscreen mode Exit fullscreen mode

You can use this file to ignore some checks that you don't wanna run in your application, you can see how to do it by looking in the pull request in the end of this article.

Testing it out

Now that the tool is properly configured, let's test it. To run rubocop just type the following command in your terminal:

rubocop
Enter fullscreen mode Exit fullscreen mode

If everything is ok you should look something like this:

Image description

That's a lot of errors, but it's ok, to fix most of them you can run the following command:

rubocop -A
Enter fullscreen mode Exit fullscreen mode

It'll fixes all correctable issues, now just fix the rest and everything is fine

You can see all the code changed here

Top comments (0)