Rails Boilerplate

So far in this series, we have looked at the benefits of using a Boilerplate to streamline your workflow, and two articles on how to set up a React Boilerplate with Parcel instead of Webpack, and a traditional means to compile and bundle a traditional HTML/SCSS project using Parcel. So, what about Rails?

Config File

If you’ve used Unix/Linux for any length of time, you’re familiar with dotfiles. Those are the files beginning with a . and usually ending with rc: .bashrc, .vimrc, and .zshrc being familiar with examples. Modifying these files allows you to configure the behavior of the associated program, and it can be a never-ending quest for some to get the “perfect” setup.

You can streamline new Rails project creation with the use of a .railsrc file. There are a few rules:

  • The file must be named .railsrc (don't forget the preceding .)
  • The file must be placed in the user's path. So, it has to be in your $HOME directory.

Follow these simple rules, and the rails new command will inject commands into the command line. For instance, I never use the default Rails database SQLite; I always add the command-line switch to use Postgres. Do I want to type this every time? NO!!!!!

So my simple .railsrc file:

--database=postgresql

I can start a new project: rails new cool_app and silently the PostgreSQL flag is added to the installation process.

What are your defaults? What do you change or use each time? A more involved setup may look like this:

--database=postgresql
--webpack
--skip-action-cable
--skip-spring
--skip-turbolinks

There is more we can do.

Template Files

The last line of the .railsrc file is reserved for template files that will run during install. So, let's look at my simple railsrc file again:

--database=postgresql
--template=/path/to/file/rails-template.rb

I have stored my simple template file locally, and you can name it as you like. Here is my simple script:

#add guard-minitest and spring to dev

gem_group :development do
gem 'pry-rails'
gem 'awesome_print'
end

gem_group :test do
gem 'guard'
gem 'guard-minitest'
gem 'minitest'
gem 'minitest-reporters'
gem 'rails-controller-testing'
end

run "bundle install"

generate :controller, "static_pages home"
route "root 'static_pages#home'"

run "cp ~/development/my-docs/starter/renovate.json ."
run "cp ~/development/my-docs/starter/LICENSE ."
run "cp ~/development/my-docs/Guardfile ."

#create postgres DB for postgress.app
run "psql -c 'CREATE DATABASE #{app_path}_development;'"
run "psql -c 'CREATE DATABASE #{app_path}_test;'"

In this template, I am setting up my beginning project preferences:

  • Set up a better console printing with awesome_print
  • Set up my preferred testing environment
  • Bundle install
  • Set up a static controller for the Home page
  • Copy a few files
  • Create the Postgres databases.

So, all I have to do is change to the new project directory and start the Rails server. The DB is set up enough to get started. This works for me, but I want to do more.

More Resources

Matt Brictson has put together a more full-featured Rails template that is preloaded with the best practices for TDD, security, deployment, and developer productivity.

Jumpstart

Building on Matt's open-source template, Chris Oliver has put together a template called Jumpstart. This template creates a Boilerplate app that includes user accounts, an admin interface, and Bootstrap styling.

Jumpstart Pro

Chris also has a commercial product called Jumpstart Pro that bootstraps a full-featured e-commerce application setup.

Post install template

What about automating post-install tasks? Well, I highly recommend checking out the free service RailsBytes and trying some templates out.

For instance, do you want to install Rubocop:

rails app:template LOCATION='https://railsbytes.com/script/V33sBe'

Thats it. Check it out.

Rails Minimal

Finally, there is a new feature coming to the rails new command to create a minimal application. The original pull request has been merged, but there still appears to be a documentation pull request in the works. When it is finished and deployed, the command rails new cool_app --minimal will look like the following:

  • skip_action_cable
  • skip_action_mailbox
  • skip_action_mailer
  • skip_action_text
  • skip_active_job
  • skip_active_storage
  • skip_bootsnap
  • skip_jbuilder
  • skip_spring
  • skip_system_tests
  • skip_turbolinks
  • skip_webpack
  • Sprockets should have JavaScript folders by default
  • rails new --minimal webpack=react still works

When fully deployed, this will be a nice feature to have for streamlining the workflow.