In a `Rails::Railtie`, how does a gem expose `config.audit_trail` settings to the host, and why read them in an initializer rather than the class body?
answer
- config shared by every railtie
- ActiveSupport::OrderedOptions with defaults
- class body runs at require time
- host sets it in config/application.rb
- config/initializers run after gem initializers
basics
~20 sThe railtie assigns config.audit_trail = ActiveSupport::OrderedOptions.new, with defaults, and the host overrides keys in config/application.rb or an environment file. The class body runs when the gem is required, before host config exists, so values are read in an initializer.
solid answer
~40 sRailtie configuration is one store shared by all railties and the application, so `config.audit_trail = ActiveSupport::OrderedOptions.new` in the railtie creates a namespace the host can reach as `config.audit_trail.enabled = false` in `config/application.rb` or `config/environments/production.rb`. Defaults go in the class body right after the assignment. The class body itself runs when `Bundler.require` loads the gem, before the `Application` class body sets anything, so code there only ever sees defaults. An `initializer "audit_trail.configure" do |app| ... end` runs after the application and environment config are evaluated and can read `app.config.audit_trail`. Values a host sets in `config/initializers/*.rb` arrive later still, because by default gem initializers run before the app's; read those at use time or in `config.after_initialize`.
code
ruby · 15 lines# lib/audit_trail/railtie.rb (gem)
module AuditTrail
class Railtie < ::Rails::Railtie
config.audit_trail = ActiveSupport::OrderedOptions.new
config.audit_trail.enabled = true
config.audit_trail.actor_method = :current_user
initializer "audit_trail.configure" do |app|
AuditTrail.enabled = app.config.audit_trail.enabled
end
end
end
# config/environments/test.rb (host)
config.audit_trail.enabled = falsego deeper
Remember the pattern: the gem assigns an OrderedOptions namespace with defaults, and the host overrides keys in config/application.rb.
Explain the shared config store, the boot order from Bundler.require to config/initializers, and why reads belong in an initializer.
Diagnose settings that do nothing by checking when the gem reads them; design gems to read lazily or document where hosts must configure.
Treat a gem's config namespace as a public contract: one unique name, documented read timing, and defaults that are safe for every host.
## One configuration store Every railtie, engine and the application expose a `config` object. For railties it is a `Rails::Railtie::Configuration`, whose unknown keys live in a single class-level hash shared by all of them. Two consequences follow: - a key a gem assigns, `config.audit_trail = ...`, is visible from the host as `Rails.application.config.audit_trail`; - names are global, so a gem should use its own name as the key and never a generic one such as `config.audit`. Rails' own frameworks use the same pattern: `ActionMailer::Railtie` assigns `config.action_mailer = ActiveSupport::OrderedOptions.new`, and that is why `config.action_mailer.delivery_method` works in an environment file. `ActiveSupport::OrderedOptions` is a hash that also answers method calls, so `config.audit_trail.enabled = true` and `config.audit_trail.enabled` work without declaring each key. Assigning to a name that is a real configuration method raises `NoMethodError` ("Cannot assign to ..., it is a configuration method"), which protects names such as `to_prepare`. `config.x` is a different tool: it is the application's own space for custom settings (`config.x.payment_processing.schedule`), not a namespace a gem should claim. ## When each piece of code runs The order of a normal boot decides what a railtie can see: 1. `config/application.rb` requires the frameworks, then `Bundler.require` loads gems. The gem's **railtie class body runs here**: it can assign the namespace and defaults, but the host has not configured anything yet. 2. The `Application` class body runs: `config.load_defaults`, then the host's `config.audit_trail.enabled = false`. 3. During initialization, each environment file in `config/environments` is evaluated early, so production overrides are in place too. 4. Railtie and engine initializers run, the gems' before the application's own. A gem initializer here sees everything from steps 2 and 3. 5. The application's `config/initializers/*.rb` run, by default after the gem initializers. 6. `config.after_initialize` blocks run at the end. | Where the host sets it | Visible to the railtie class body | Visible to a gem initializer | Visible at use time or in `after_initialize` | |---|---|---|---| | `config/application.rb` | no | yes | yes | | `config/environments/*.rb` | no | yes | yes | | `config/initializers/audit_trail.rb` | no | not by default | yes | ## Reading the values safely - **In an initializer**, through the application object: `initializer "audit_trail.configure" do |app| AuditTrail.enabled = app.config.audit_trail.enabled end`. - **At use time**, by reading `Rails.application.config.audit_trail` when a record is saved, which sees any value set before the first save. - **In `config.after_initialize`**, when the gem must act once all host initializers have run; the configuring guide recommends this when code depends on other initializers having finished. What not to do: copy a value into a constant in the class body, or branch on it there. That freezes the default before the host has spoken, and the bug appears only for hosts that change the setting. ## Telling hosts where to configure A gem's README should state where settings belong. Many gems ask for `config/application.rb` or an environment file for anything read during boot, and accept `config/initializers` only for values read lazily. An install generator that writes `config/initializers/audit_trail.rb` is fine when every setting in it is read at use time. ## A complete pattern, step by step 1. In the railtie class body, assign the namespace: `config.audit_trail = ActiveSupport::OrderedOptions.new`. 2. Set every default right after it, so hosts can read and print the full set. 3. Declare one initializer that reads `app.config.audit_trail` and passes the values to the gem's own module. 4. For settings that may be changed from `config/initializers`, read them at use time instead of copying them. 5. Document each key, its default and the latest point in boot where a host may set it. ## Debugging a setting that does nothing 1. In `bin/rails console`, print `Rails.application.config.audit_trail` to confirm the value arrived. 2. Run `bin/rails initializers` and find the gem's initializer relative to the app's `load_config_initializers` step. 3. Search the gem for reads at class-body time or in a constant. 4. Move the host setting earlier or the gem's read later.
- Why does a host setting in config/initializers/audit_trail.rb sometimes do nothing?Gem initializers run, by default, before the application's `load_config_initializers` step. A gem that copies the value during its own initializer has already read the default. Either the gem reads the value later, at use time or in `config.after_initialize`, or the host sets it in `config/application.rb`.
- What is the difference between config.audit_trail and config.x.audit_trail?`config.audit_trail` is a namespace the gem's railtie creates and documents. `config.x` is the application's own bucket for custom settings; a gem that read from `config.x` would depend on a host convention rather than defining its own contract.
Like a form a contractor leaves for the homeowner: the contractor prints default answers on it when dropping it off, but should read it only on the day work starts, not in the van on the way over.
- The railtie class body runs after config/application.rb is evaluated
- Each railtie has a private config that the host cannot reach
- Host values in config/initializers always reach gem initializers
- A gem should store its settings under config.x
- OrderedOptions requires every key to be declared before use