Paul Bădărău

Rails engines in production: when to extract a boundary

When to extract a Rails engine from a working application: define the boundary, keep host policy out, and plan migrations, testing, and versioned releases.

The questionnaire code had already survived an education platform before I extracted it into Responsa, a Rails engine. It handled educator check-ins, student feedback, public surveys, and assessments with a Typeform-style builder, one-question-at-a-time navigation, partial saves, branching, public tokens, and automatic or manual marking; meanwhile the surrounding app managed course deliveries, ordered steps, anonymous student identity, six-month signed links, progression rules, staff roles, and reporting.

I never start a Rails build by creating engines/ for components I imagine reusing later, because that produces a private framework with no real callers. An engine is a product surface that needs compatibility rules, migrations, installers, and test fixtures even inside one organisation.

The application drew the boundary first

The source app was a Rails 8.1 platform for a youth education non-profit where educators delivered modules and lessons, logged class context, requested volunteer sessions, and submitted check-ins. Students and public survey respondents needed a different privacy model from signed-in educators, which forced a clean split between form mechanics and education policy.

Forms have ordered questions, responses hold answers and completion states, branching picks the next question, and long answers route to an assessor; those rules form a coherent state machine on their own. Course progression, signed six-month student links, and attaching a check-in to a course delivery and step belong strictly to the host. My extraction test is simple: if you can't describe the candidate without naming the host's central records, it's still a feature. You also need callers whose policies disagree (educator check-ins, anonymous student feedback, and public surveys), because two callers with identical rules don't prove a real boundary.

A gem becomes an engine when Rails owns the contract

A plain Ruby gem parses files, talks to an API, or calculates scores, and shouldn't own routes, controllers, assets, or migrations. Responsa owns Responsa::Form, Responsa::Response, and Responsa::Answer, eight question types (short text, long text, boolean, scale, multiple choice, number, location, and ranking), the builder and marking UI, database migrations, Stimulus controllers for dynamic fields, and CSS prefixed .responsa-. Packaging that as a plain gem would force every host to wire up tables, routes, and JavaScript by hand.

Host policy stays outside. There's no required Lesson, Delivery, User, or tenant model, and responses store a generic user_uid string instead of coupling to a host User or authentication scheme. If a candidate touches ApplicationRecord, calls current_user in a model, or assumes one global route helper, a namespace won't untangle it.

Let the host own routes and controllers

Configurable engines with fifteen options usually end up with no working default. Responsa ships one supported path: install the gem, run bin/rails generate responsa:install, run migrations, and mount responsa_for in routes (defaulting to /responsa). The installer copies Stimulus controllers, pins them in the importmap, copies engine migrations, and runs the Action Text installer, requiring Rails 8, importmap, Turbo, Stimulus, and Propshaft or Sprockets.

The only escape hatches we support are responsa_for path: "admin", path: nil, and forms_as: when "survey" fits better. Engine controllers inherit from the host's ApplicationController. A host can subclass Responsa::ResponsesController, enforce authentication, and nest routes while Responsa resolves host helpers like form_response_path. The education app attaches delivery, step, and audience on completion and updates progression without Responsa knowing courses exist; hosts can also override view groups under app/views/responsa/ or drop the packaged CSS for Tailwind.

Version numbers turn internal code into a promise

Extracting an engine creates a second release cycle where migrations, table names, indexes, foreign keys, route helpers, and generated JavaScript become an upgrade contract. Responsa targets Ruby 3.1+ and Rails 8.0+ with a defined asset pipeline, because claiming to support "any Rails app" is impossible to test honestly.

Additive migrations are straightforward; removing a column or changing stored meaning requires a migration plan, seeds, and SemVer discipline (breaking changes take a major version, new behaviour a minor, fixes a patch). Keeping the education response controller in the host app means an engine upgrade never overwrites course progression, and we explicitly avoided an after_response_complete hook inside the engine. Separate release schedules for host and engine are stronger proof of a real boundary than code reuse alone.

Test the package without knowing the first app

Engine bugs usually hide at the integration joins (migrations, routes, asset pins, and helpers) more often than inside model validations. Responsa's spec/dummy app creates and migrates its own database, seeds sample forms and assessments, mounts at /forms, and exercises ordinary flows, branching, partial saves, exact-answer auto-marking, and needs_review assessments alongside unit and request specs for the installer and overridden route helpers.

No engine test may reference Course, deliveries, or host authentication, while the host suite verifies educator check-in attachments, progression updates, and six-month signed student links. That keeps question-flow regressions in the engine and course-completion regressions in the host; for more on structuring suites this way, see Tests as Contracts.

Leave product policy in the host

Responsa never decides who can create a form, mark an assessment, view a student response, or open the next lesson. The host owns safeguarding, privacy policy, notifications, and reporting. I'd turn down requests for a universal auth adapter, a reporting layer, or single-client question types until a second real host asks for them, keeping the seam small enough that another app can install the engine without inheriting the first app's worldview.