Augusto Queirantes
Posted on September 17, 2022
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
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
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'
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
If everything is ok you should look something like this:
That's a lot of errors, but it's ok, to fix most of them you can run the following command:
rubocop -A
It'll fixes all correctable issues, now just fix the rest and everything is fine
You can see all the code changed here
Posted on September 17, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.