• Stars
    star
    1,076
  • Rank 41,336 (Top 0.9 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 9 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

App template for Rails 7 projects; best practices for TDD, security, deployment, and developer productivity. Now with optional Vite integration! ⚡️

📣 I am no longer maintaining this project. Please check out mattbrictson/nextgen instead. Nextgen includes all the recommendations you see here, but in a new interactive format that lets you pick and choose the enhancements appropriate to your app. Try it and send me your feedback!


mattbrictson/rails-template

GitHub Workflow Status GitHub Workflow Status

About

This is the application template that I recommend for Rails 7 projects. I've assembled this template over the years to include best-practices, tweaks, documentation, and personal preferences, while still generally adhering to the "Rails way".

I've written several blog posts on techniques, helpers, and boilerplate that are included in this template, if you'd like to take a deeper dive:

Requirements

This template currently requires:

  • Rails 7.1
  • Ruby 3.1 or newer
  • PostgreSQL
  • Node 16.14+ or 18+, and Yarn 1.x

For older versions of Rails, you can use one of these branches going back all the way to Rails 4.2.

Installation

Optional.

To make this the default Rails application template on your system, create a ~/.railsrc file with these contents:

-d postgresql
-m https://raw.githubusercontent.com/mattbrictson/rails-template/main/template.rb

Usage

This template assumes you will store your project in a remote git repository (e.g. GitHub) and that you will deploy to a production environment. It will prompt you for this information in order to pre-configure your app, so be ready to provide:

  1. The git URL of your (freshly created and empty) GitHub repository
  2. The hostname of your production server

To generate a Rails application using this template, pass the -m option to rails new, like this:

rails new blog \
  -d postgresql \
  -m https://raw.githubusercontent.com/mattbrictson/rails-template/main/template.rb

Remember that options must go after the name of the application. The only database supported by this template is postgresql.

If you’ve installed this template as your default (using ~/.railsrc as described above), then all you have to do is run:

rails new blog

What does it do?

The template will perform the following steps:

  1. Generate your application files and directories
  2. Create the development and test databases
  3. Commit everything to git
  4. Push the project to the remote git repository you specified

What is included?

Optional support for vite_rails⚡️

Add the --javascript vite option to the rails new command to get started with Vite! Vite is an easy to use alternative to Webpack(er), and much more powerful than the standard import map and css/jsbundling-rails options that are built into Rails.

  • Frontend code (JS, CSS, images) will be placed in app/frontend/
  • Run yarn start to start the development server with hot reloading
  • Plain CSS with modern-normalize will be used for styles (the --css option will be ignored)

If you don't specify --javascript vite, then this template will use the standard Rails 7 behavior.

These gems are added to the standard Rails stack

  • Core
    • sidekiq – Redis-based job queue implementation for Active Job
  • Configuration
    • dotenv – for local configuration
  • Utilities
    • annotate – auto-generates schema documentation
    • good_migrations - prevents app models from being improperly referenced in migrations
  • Linting
    • rubocop – enforces Ruby code style
    • erblint – applies rubocop rules within html.erb files
    • stylelint – checks (S)CSS files
    • eslint – checks JS/TS files
  • Security
  • Testing

Postmark

I like to use Postmark for transactional email, and so I've included the postmark-rails gem and configured it in environments/production.rb. Make sure to sign up for a Postmark account to get an API key, or switch to your own preferred email provider before deploying your app.

Other tweaks that patch over some Rails shortcomings

  • A much-improved bin/setup script

How does it work?

This project works by hooking into the standard Rails application templates system, with some caveats. The entry point is the template.rb file in the root of this repository.

Normally, Rails only allows a single file to be specified as an application template (i.e. using the -m <URL> option). To work around this limitation, the first step this template performs is a git clone of the mattbrictson/rails-template repository to a local temporary directory.

This temporary directory is then added to the source_paths of the Rails generator system, allowing all of its ERb templates and files to be referenced when the application template script is evaluated.

Rails generators are very lightly documented; what you’ll find is that most of the heavy lifting is done by Thor. The most common methods used by this template are Thor’s copy_file, template, and gsub_file. You can dig into the well-organized and well-documented Thor source code to learn more.

More Repositories

1

airbrussh

Airbrussh pretties up your SSHKit and Capistrano output
Ruby
513
star
2

tomo

A friendly CLI for deploying Rails apps ✨
Ruby
370
star
3

bundleup

A friendlier CLI for Bundler’s `update` and `outdated` commands.
Ruby
187
star
4

chandler

[unmaintained] chandler syncs your CHANGELOG entries to GitHub's release notes
Ruby
154
star
5

capistrano-mb

[unmaintained] Capistrano tasks for deploying Rails from scratch to Ubuntu 16.04 and 18.04
Ruby
118
star
6

rails-starter

I've stopped maintaining this project after reaching Rails 4.1.6. For new versions of Rails, and for my latest Rails configuration suggestions, please use mattbrictson/rails-template.
Ruby
99
star
7

dotfiles

bash, git, rails, Sublime Text 3, homebrew, more...
Ruby
83
star
8

nextgen

Generate your next Rails app interactively! This template includes production-ready recommendations for testing, security, developer productivity, and modern frontends. Plus optional Vite support! ⚡️
Ruby
58
star
9

gem

A GitHub template for creating Ruby gems
Ruby
31
star
10

pgcli-rails

Replaces Rails PostgreSQL `dbconsole` with the much nicer `pgcli`
Ruby
28
star
11

minitest-snapshots

Minitest plugin for asserting that strings are the same as a saved snapshot, jest style
Ruby
25
star
12

jess

Lightweight, unofficial client for the JAMF Software Server (JSS) API
Ruby
5
star
13

tomo-plugin-sidekiq

sidekiq tasks for tomo
Ruby
4
star
14

tomo-plugin

A GitHub template for creating tomo plugins
Ruby
2
star
15

tomo-plugin-nvm

tomo plugin to manage node and yarn via nvm
Ruby
2
star
16

tomo-plugin-rollbar

rollbar tasks for tomo
Ruby
2
star
17

rails-new

Ruby
1
star