← notes

Rails application templates, and the ordering problem everyone hits

Aug 13, 2020

Make my Rails defaults one flag instead of a checklist.

rails new myapp -d postgresql -m template.rb and the app comes out with the gems added, generators run, files created and the first commit made. Rails 6, Ruby 2.6.

Application templates are underused, and the reason is that most people meet them once, hit the ordering problem, and go back to a starter repo. The template runs against a freshly generated app in a specific sequence, and anything needing the gems installed has to sit inside after_bundle rather than at the top level. Put a generator call in the wrong place and it fails with an error that never mentions ordering.

The trade against forking a starter repo runs both ways. A template runs once and produces an app entirely yours, with no upstream and nothing to merge. A starter repo is a fork, so you can pull later improvements and you carry its history and opinions forever.

Templates rot faster, though, because a template encodes assumptions about what rails new generates and every Rails version changes some of them. This one was written against Rails 6 and would need work now.

Built for faster screencast setup.