Rails 7.Start Kit loves Rubocop

Ilya Zykin
3 min readJan 29, 2023

--

This weekend I’ve already published Rails 7. Start Kit Release 1.2, but not a time to stop. Release 1.3 is also here. In this release I’ve added gem rubocop-rails. All related changes are in this Pull Request

How I did it

Step 1

I’ve added rubocop-rails to the Gemfile. It is important do not require the gem to the rails app. Also we need rubocop only for development environment.

group :development, :test do
# Fake data for development and testing
gem "faker", "3.1.0"

# Code linting
gem "rubocop-rails", "2.17.4", require: false
end

and bundle install of course

Step 2

I’ve added .rubocop.yml with the only rule declaration

require: rubocop-rails

After that I have run the command

$ rubocop --require rubocop-rails

I’ve seen a lot of messages about potential issues. About 230+ items.

Some of them were reasonable, some were unusual to me, and some were about acceptable things to me.

Funny thing, that original code of Rails 7 does not follow best practices from Rubocop rules set.

Step 3

I had to review all my files and file of the Rails 7 to decide what things I want to pass through, what things I’m going to address.

I had to extend file to describe my expectations about original Rails 7 code and my own rules.

I’ve got the following list of rules

require: rubocop-rails
Style/FrozenStringLiteralComment:
Enabled: true
Style/Documentation:
Enabled: false
Style/StringLiterals:
Exclude:
- Gemfile
- Rakefile
- test/test_helper.rb
- config/boot.rb
- config/importmap.rb
- config/application.rb
- config/initializers/assets.rb
- config/environments/test.rb
- config/environments/development.rb
- config/environments/production.rb
- app/mailers/application_mailer.rb
Naming/FileName:
Exclude:
- config/_PUMA.rb
- config/_SCHEDULE.rb
- config/initializers/_CHEWY.rb
- config/initializers/_CONFIG.rb
- config/initializers/_REDIS.rb
- config/initializers/_SIDEKIQ.rb
Style/SymbolArray:
Exclude:
- config/initializers/filter_parameter_logging.rb
Style/GlobalStdStream:
Exclude:
- config/environments/production.rb
Style/ClassAndModuleChildren:
Exclude:
- test/test_helper.rb
Layout/SpaceInsidePercentLiteralDelimiters:
Exclude:
- Gemfile
Rails/Output:
Exclude:
- db/seeds.rb
- app/jobs/example_job.rb
- app/models/currency_rate.rb
- config/environments/development.rb
- config/initializers/_CHEWY.rb
- config/initializers/_CONFIG.rb
- config/initializers/_REDIS.rb
- config/initializers/_SIDEKIQ.rb
Metrics/MethodLength:
Exclude:
- bin/helpers/docker.rb
- app/controllers/demo_controller.rb
Style/OptionalBooleanParameter:
Exclude:
- bin/helpers/docker.rb
Style/OptionalBooleanParameter:
Exclude:
- bin/helpers/common.rb
Lint/UselessAssignment:
Exclude:
- bin/helpers/puma.rb
- bin/helpers/sidekiq.rb
Style/GlobalVars:
Exclude:
- bin/helpers/common.rb
- bin/helpers/helpers.rb
- config/initializers/_REDIS.rb
Metrics/BlockLength:
Exclude:
- config/environments/development.rb

Step 4

I addressed some Rubocop issues what left after my changes. And added some commands to improve my user experience in Rails 7. Start Kit. That’s it.

What I’ve learned

First, there is new syntax for passing params

content_raw: content_raw, # old syntax
content_raw: , # new syntax

Second, magic comment # frozen_string_literal: true at the beginning of a ruby file is still best practice

# frozen_string_literal: true

source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.2.0"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "7.0.4.2"

Conclusion

It is just a fantastic how with using Rails 7. Start Kit I can easyli learn new things about Rails.

I hope this project will help you too.

Subscribe to the project to know about most recent updates.

Happy coding!

--

--

Ilya Zykin
Ilya Zykin

Written by Ilya Zykin

IT coach. React and Rails enthusiast. Passionate programmer, caring husband and pancake baker on Sundays. School teacher of computer studies in the past.

No responses yet