How many tools are needed for types & docs for Ruby on Rails?

September 12, 2023

I know it's OK to code without types and docs, but if you want them (like Java, TypeScript, Python, etc.), below is the setup you may have to go through. I'm thinking about having a new project-create-toolkit to include all of them, otherwise it's painful and waste of time to manually set up each of them for every new Ruby on Rails project.

Install and setup the gems:

Linter:

gem 'rubocop', require: false

Static Type Checker:

gem 'sorbet', :group => :development
gem 'sorbet-runtime'
gem 'tapioca', require: false, :group => :development
gem 'webrick'

Type Annotation:

gem "rbs"

Annotate controllers with route info:

gem "chusaku", require: false

Annotate models and routes:

gem 'annotate'

In lib/tasks, create routes.rake:

task routes: :environment do
  Rails.application.eager_load!
  models = ApplicationRecord.descendants.collect(&:name).join('|').downcase
  controllers = ApplicationController.descendants.collect(&:name)
  controllers = (controllers.map { |controller| controller[0..-11].downcase }).join('|')
  if models
    puts `bundle exec rails routes -g "#{models}|#{controllers}"`
  else
    puts `bundle exec rails routes -g "#{controllers}"`
  end
end

Initialize Sorbet: https://sorbet.org/docs/adopting#step-2-initialize-sorbet-in-our-project

Install and setup VS Code Extensions:

Most importantly, Shopify's Ruby extension set, but also add all those for ERBs, etc.