Rails Gem files need some help

Fire up a fresh Rails project and you might think everything is clean, fresh, and ready to go.  Just don’t open the Gemfile as it’s a mess.

Here’s what a default Gemfile for a brand-spankin’ new Rails project looks like:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
 
ruby '2.5.1'
 
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby
 
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
 
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
 
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
 
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
 
group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
 
group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0'
end
 
group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15', '< 4.0'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'chromedriver-helper'
end
 
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Start grouping gems together by functionality (completely arbitrarily) and you might end up with something like this:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
 
ruby '2.5.1'
 
# framework
gem 'rails'
 
# rack/middleware
gem 'rack-cors'
gem 'rack-attack'
 
# Low-Level Data
gem 'pg'
gem 'pg_search'            # search support
# gem 'pghero'               # db-analysis
gem "hiredis"
gem "redis", "~> 4.0"
# gem "aws-sdk-s3", require: false # image storage
gem 'oj'                   # fast JSON parse
 
# High-level Data
gem 'aasm'                 # state-machine support
gem 'valid_email2'         # email validator
gem 'phone'                # phone number parsing & validation
gem 'ensurance'            # adds ability to User.ensure()
gem 'seedbank'             # better data seeds
gem 'activerecord-import', '>= 0.11.0' # bulk insertion of data
gem 'storext'              # store attributes in a jsonb column
 
# main application
gem 'ancestry'             # parent-child relationships for User
gem "aws-sdk-s3", require: false # Document storage
gem "rolify"               # role mgmt
 
# GraphQL
gem 'graphql'
 
# Services
gem 'interactor'           # encapsulate application's business logic
gem 'interactor-contracts' # even better services
 
# utils
gem 'jwt'                  # JSON Web Token
gem 'bcrypt', '~> 3.1.7'   # secure password hashing
gem 'figaro'               # ENV variable management
gem 'recursive-open-struct'
gem 'awesome_print'        # print anything in console
gem 'hirb'                 # Model formatter in the console
gem 'colorize'             # colorize strings
gem 'fast_blank'           # faster .blank? / .present? using C
 
gem 'bootsnap', require: false
gem 'parallel', require: false
 
# web server
gem 'puma'
 
# Logging
# gem 'timber', '~> 2.6'
 
group :development, :test do
  gem 'annotate', github: 'ctran/annotate_models', branch: :develop
  gem 'faker'
  # gem 'letter_opener'
end
 
group :development do
  gem 'pry-rails'
end
 
group :test do
  gem 'factory_bot_rails'
  gem 'rspec-rails'
  gem 'rspec_junit_formatter' # for circleci
end

taken from an actual project

Why can’t the default Rails’ Gemfile be more like that?

Leave a Reply

Your email address will not be published. Required fields are marked *