In a Rails engine, what does `isolate_namespace Billing` change for its tables, helpers, routes and model naming?
answer
- engine_name becomes billing
- table_name_prefix billing_
- routes default to module: billing
- only the engine's helpers
- param_key drops the namespace
basics
~20 sisolate_namespace Billing prefixes the engine's tables with billing_, routes its routes to Billing:: controllers, gives its controllers only Billing's helpers and route helpers, strips the namespace from param and route keys, and names the engine billing.
solid answer
~30 s`isolate_namespace Billing` sets the engine name to `billing` (the default `as:` for `mount` and the prefix of `billing:install:migrations`). It defines `Billing.table_name_prefix` as `billing_`, so `Billing::Invoice` reads `billing_invoices`. It sets the engine's default route scope to `module: "billing"`, so `resources :invoices` reaches `Billing::InvoicesController`. Controllers under `Billing` get only the engine's `app/helpers` and the engine's URL helpers, and the engine's helpers are not added to the host's helper path. It also turns on relative model naming: forms post `invoice[amount]` and `polymorphic_path` uses `invoices_path`, not `billing_invoices_path`. It does not hide constants: the host can still call `Billing::Invoice` directly.
code
ruby · 13 lines# engines/billing/config/routes.rb
Billing::Engine.routes.draw do
resources :invoices # -> Billing::InvoicesController
end
# engines/billing/app/models/billing/invoice.rb
module Billing
class Invoice < ApplicationRecord # table: billing_invoices
end
end
Billing::Engine.engine_name # => "billing"
Billing::Invoice.model_name.param_key # => "invoice"go deeper
Remember the visible effects: billing_ table prefix, Billing:: controllers and the billing route proxy name.
Walk through the five changes: engine_name, table_name_prefix, default route scope, helper and URL-helper isolation, relative model naming.
Point out what isolation does not give you: no constant privacy, no separate database, and host helpers missing from engine layouts. Plan table names before isolating an existing engine.
Treat isolation as a naming contract, then decide whether you also need a separately enforced dependency rule between host and engine code.
## The call and where it lives `isolate_namespace` is a class method of `Rails::Engine`. A mountable engine's `lib/billing/engine.rb` calls it with the module that holds all of the engine's code: ```ruby module Billing class Engine < ::Rails::Engine isolate_namespace Billing end end ``` The method does a small number of concrete things, each aimed at keeping the engine's names from colliding with the host application's. ## The five changes 1. **Engine name.** `engine_name` becomes the underscored module name, `billing`. Without isolation the default is derived from the class, `billing_engine`. The engine name is the default `:as` of `mount`, so the host's route proxy is called `billing`, and it prefixes the migration task `billing:install:migrations`. 2. **Table prefix.** Unless the module already defines one, `Billing.table_name_prefix` returns `"billing_"` (after any global `ActiveRecord::Base.table_name_prefix`). `Billing::Invoice` therefore maps to `billing_invoices`, and generators inside the engine create migrations for prefixed tables. 3. **Routes.** The engine's config gets `default_scope = { module: "billing" }`, so the engine's `config/routes.rb` can say `resources :invoices` and still dispatch to `Billing::InvoicesController`. Controllers nested in `Billing` include the engine's route set's URL helpers instead of the host's, which is why `invoices_path` inside the engine means the engine's route. 4. **Helpers.** A controller whose module parents include `Billing` uses the engine's `app/helpers` as its helper path. The engine also skips adding its helpers to the host's `helpers_paths`, so the host's `helper :all` (on by default through `include_all_helpers`) does not pull them in. 5. **Model naming.** `Billing.use_relative_model_naming?` returns `true`. Active Model then drops the namespace from `param_key` and `route_key`: `form_with(model: Billing::Invoice.new)` names fields `invoice[amount]`, and `polymorphic_path(invoice)` calls `invoice_path`, matching the engine's own unprefixed routes. It also marks the engine `isolated?` and records the engine on the module (`railtie_namespace`), which is how the helpers and URL-helper lookups find it. ## Without it: a full engine | Concern | Without `isolate_namespace` | With `isolate_namespace Billing` | |---|---|---| | Default `engine_name` | `billing_engine` | `billing` | | `Billing::Invoice` table | `invoices` | `billing_invoices` | | Engine helpers in host controllers | included via `helper :all` | not included | | Host helpers in engine controllers | included | not included | | Form param key | `billing_invoice` | `invoice` | | Route target of `resources :invoices` | `InvoicesController` unless you add `module:` | `Billing::InvoicesController` | The engines guide calls the isolation line highly recommended because without it the engine's helpers and classes can leak into the host or be overridden by same-named host code. ## Checking the effects in a console Every change above can be observed from `bin/rails console` in the host, which is the quickest way to settle an argument in review: - `Billing::Engine.isolated?` returns `true`; - `Billing::Engine.engine_name` returns `"billing"`; - `Billing.table_name_prefix` returns `"billing_"` and `Billing::Invoice.table_name` returns `"billing_invoices"`; - `Billing::Invoice.model_name.param_key` returns `"invoice"` and `route_key` returns `"invoices"`; - `Billing::InvoicesController.helpers_path` lists only the engine's `app/helpers` directory. If a value differs, look for a module that already defines `table_name_prefix` or `use_relative_model_naming?`; `isolate_namespace` leaves such definitions alone. ## What isolation does not do - **It is not access control.** Ruby constants are global; host code can still call `Billing::Invoice.where(...)` and engine code can still reference `::Employee`. Isolation scopes names, it does not hide them. - **It does not separate the database.** The engine's models use the host's connection and transactions; the prefix only changes default table names. - **It does not require a mount.** Active Storage is an isolated engine (`isolate_namespace ActiveStorage`) whose routes are drawn straight into the host route table. - **It does not namespace views by magic.** Views live where the controller path says, `app/views/billing/invoices`, because the controller is `Billing::InvoicesController`. ## Practical consequences - Pick the module name carefully: it becomes the table prefix, the proxy helper and the rake namespace. - A layout or partial in the engine that calls a host helper (`current_user`, `root_path`) fails because neither the helper nor the route is in the engine's set; use `main_app.root_path` for routes and expose shared helpers deliberately, for example `helper Billing::Engine.helpers` on the host side or an explicit helper module on the engine side. - If existing tables lack the prefix, set `self.table_name` or define `Billing.table_name_prefix` yourself before isolating.
- How do you share one engine helper with the host without dropping isolation?Add it on the host side: `helper Billing::SharedHelper` in a host controller, or `helper Billing::Engine.helpers` to include every module from the engine's `app/helpers`. Helpers defined with `helper_method` in engine controllers are not part of that module.
- An isolated engine must use existing unprefixed tables. What are the options?Set `self.table_name = "invoices"` per model, or define `Billing.table_name_prefix` on the module yourself; `isolate_namespace` only defines the `billing_` prefix when the module does not already respond to `table_name_prefix`.
- isolate_namespace makes the engine's classes private to the engine
- Isolated engines get their own database connection
- An isolated Billing::Invoice still reads the invoices table
- Host helpers stay available inside isolated engine controllers
- isolate_namespace is what allows an engine to be mounted