Getting Started with Engines
See also: Guides index · Configuring Trails Applications · trailties package
An engine is a small application that runs inside another one. It has its own models, controllers, views and routes, and a host app mounts it at a path. This guide builds a blogging engine called Blorgh and covers:
- What an engine is and how it differs from an application.
- Generating an engine and the files it contains.
- Adding features to it.
- Mounting it in a host application and reaching the host from it.
- Testing it.
What are Engines?
An application is itself an engine: Application extends Engine, which extends Trailtie. An engine has everything an app has except the boot process, which belongs to whichever app loads it.
A mountable engine is isolated: its classes live in a namespace of their own, so its ArticlesController cannot clash with the host's. Its routes, helpers and table names are scoped to that namespace too.
Generating an Engine
trails plugin new generates an engine package:
pnpm trails plugin new blorgh --mountableThe package is a pnpm project, published to npm rather than as a gem.
Inside an Engine
The engine class lives in lib/blorgh/engine.ts:
import { Trails } from "@blazetrails/trailties";
export const Blorgh = { name: "Blorgh" } as { name: string; Engine: typeof Engine };
export class Engine extends Trails.Engine {
static {
this.isolateNamespace(Blorgh);
}
}
Blorgh.Engine = Engine;isolateNamespace is what makes the engine mountable. It sets the engine's name to blorgh, prefixes its tables with blorgh_, and keeps the host's helpers out of its views.
The rest of the layout mirrors an application:
app/holdscontrollers/blorgh/,models/blorgh/,views/blorgh/and the other application directories, each namespaced by the engine's name.config/routes.tsdraws the engine's own routes.test/dummy/is a minimal host app, used to run the engine's tests.
Providing Engine Functionality
Inside the engine, generators work as they do in an app and put their output under the namespace:
pnpm trails generate scaffold article title:string text:textThis writes Blorgh.Article to app/models/blorgh/article.ts, a migration creating blorgh_articles, a Blorgh.ArticlesController, its views under app/views/blorgh/articles/, and a route.
The engine's routes are drawn on the engine, not the application:
// config/routes.ts
import { Blorgh } from "../lib/blorgh/engine.js";
Blorgh.Engine.instance()
.routes()
.draw(function () {
this.resources("articles");
this.root("articles#index");
});Because the engine is isolated, articles#index resolves to Blorgh.ArticlesController, and the route helpers (articlesPath) are available only inside the engine.
Associations and Controllers
An engine model associates with another engine model by its short name, as inside an app:
import { Base } from "@blazetrails/activerecord";
export class Comment extends Base {
static {
this.belongsTo("article");
}
}Controllers inherit from the engine's own ApplicationController, which the generator writes as Blorgh.ApplicationController.
Hooking Into an Application
Mounting the Engine
The host adds the engine as a dependency in its package.json, then mounts it in config/routes.ts:
import { Trails } from "@blazetrails/trailties";
import { Blorgh } from "blorgh";
Trails.application.routes().draw(function () {
this.mount(Blorgh.Engine, { at: "/blog" });
});Every engine route is now served under /blog. From the host, the engine's routes are reached through a proxy named after the engine (blorgh.articlesPath()), and from the engine the host's are reached through the main_app mounted helper.
Engine Setup
Copy the engine's migrations into the host, then run them:
pnpm trails blorgh:install:migrations
pnpm trails db:migrateEvery engine with a db/migrate/ directory defines <engine>:install:migrations, and railties:install:migrations copies those of every engine at once. A copied file gets a new timestamp and the engine's name as a suffix, and one already copied is skipped.
Using a Class Provided by the Application
An engine often needs a host model, such as the author of an article. Rather than naming a host class, the engine reads the class name from a setting on its top-level namespace, exported from lib/blorgh.ts:
// lib/blorgh.ts
export const Blorgh = { name: "Blorgh", authorClass: "User" };The model imports the namespace and reads the setting:
// app/models/blorgh/article.ts
import { Base } from "@blazetrails/activerecord";
import { Blorgh } from "../../../lib/blorgh.js";
export class Article extends Base {
static {
this.belongsTo("author", { className: Blorgh.authorClass });
}
}The setting is read when Article is defined, so the host sets it in an initializer, which runs before the autoloader loads the engine's models:
// config/initializers/blorgh.ts
import { Blorgh } from "blorgh";
Blorgh.authorClass = "Person";Configuring an Engine
An engine has its own config, read and written like the application's. An initializer declared on the engine runs during the host's boot:
import { Trails } from "@blazetrails/trailties";
export class Engine extends Trails.Engine {
static {
this.initializer("blorgh.setup", () => {
console.log("Blorgh is loading");
});
}
}Testing an Engine
test/dummy/ is a full host app with the engine mounted, and the engine's tests boot it. Run them from the engine's root:
pnpm trails testTests use Vitest; see the trailties package page. In a functional test, a request goes through the dummy app, so a route path names the engine's mount point.
Improving Engine Functionality
A host changes an engine's behavior without editing it:
- Overriding views. A view at the same path in the host, such as
app/views/blorgh/articles/index.html.tse, is found before the engine's. - Overriding models and controllers. The host reopens an engine class in an initializer, or includes a concern into it, through the module mixins trails uses for Ruby's
include. - Routes. The host can mount the engine more than once, at different paths, with
as:naming each mount's route proxy. - Assets. An engine's browser assets are served through the host's Vite build, not an asset pipeline; see CLAUDE.md.
- Load hooks. An engine that changes a framework class does so in an
onLoadhook (see Configuring), so it does not force the class to load early.