Thirteen engines already running in this app

Free preview 3 min to read

A search worth running

$ grep -rl "class Engine < .*Rails::Engine" $(bundle show --paths)
railties, action_text-trix, actioncable, activestorage, actionmailbox,
actiontext, importmap-rails, lexxy, solid_cable, solid_cache, solid_queue,
stimulus-rails, tailwindcss-rails, turbo-rails

The search matches fourteen gems in this project's dependency tree. One of them, railties, only matches because it defines Rails::Engine itself, in its documentation and in the template for new plugins. The other thirteen each define a class shaped class Engine < Rails::Engine. Turbo, Stimulus, Tailwind, Solid Queue, Solid Cache, Solid Cable, Active Storage, Action Mailbox, Action Text: each one is, mechanically, a Rails engine. You've already used most of them. Earlier courses in this catalog covered Turbo Drive, Stimulus controllers, Solid Queue's job tables and Active Storage attachments without naming the mechanism that ties them together.

What an engine is, in one sentence

A Rails engine is a Rails application that can live inside another one. It has its own namespace and, depending on how it's built, its own routes, controllers, models and assets, packaged so a host app can depend on it instead of writing the same thing from scratch.

Why this deserves its own course now

The gem courses so far, "Bundler & Gemfile" and "Bundler: semantic versioning", treated gems as black boxes: add a line, run bundle install, trust the behavior. This course opens one of those boxes. It doesn't teach you to build an engine from scratch. "Architecture: private engines", a senior course in this catalog, does that, extracting a private, reusable engine from a growing app. This course's job is smaller and comes first. It's recognizing the pattern in gems you already depend on, and understanding what "isolated" buys before you try building something isolated yourself.

What this course covers

Lesson 2 covers isolate_namespace, the call underneath most of these engines, read from their source files, and what it does and doesn't isolate. Lesson 3 shows a split this app's dependency tree already contains: engines that add HTTP routes and engines that add none. Lesson 4 covers the mount keyword and the full-versus-mountable distinction from Rails' engines guide. Lesson 5 reads this app's own isolated table naming, taught in an earlier course, through the engine lens. Lesson 6 explains why this catalog's domain modules, Catalog::, Enrollment:: and Identity::, are deliberately not engines, and what would have to change for that to be worth revisiting. Lesson 7 traces one request end to end through an engine's stack.

Where this leaves you

You won't be able to build an engine yet; "Architecture: private engines" covers that later. You will be able to open any Rails app's Gemfile.lock, spot which dependencies are engines, and read what isolation bought each gem's author. A gem stops being a sealed box that happens to work.

Lesson 1 of 7 · free

Keep going with lesson 2

isolate_namespace: what it isolates

Reads isolate_namespace in three gems and explains that it isolates names, while process and database stay shared.

A free account also gives you the quizzes, your progress and notes, and the discussion.

Type to start searching...