Skip to content

Define Dotenv::Rails class-level options explicitly - #551

Open
mokevnin wants to merge 1 commit into
bkeepers:mainfrom
mokevnin:explicit-class-level-options
Open

Define Dotenv::Rails class-level options explicitly#551
mokevnin wants to merge 1 commit into
bkeepers:mainfrom
mokevnin:explicit-class-level-options

Conversation

@mokevnin

@mokevnin mokevnin commented Sep 2, 2026

Copy link
Copy Markdown

The options the README documents — Dotenv::Rails.files, .overwrite, .logger, .autorestore (and their writers) — are set on the class, but delegate ... to: "config.dotenv" only defines them on the railtie instance. They currently work because Rails::Railtie privately forwards unknown class methods to instance through method_missing.

So the documented public API has no definition anywhere:

  • it doesn't appear in Dotenv::Rails.methods, so doc generators and editor completion can't find it
  • Dotenv::Rails.method(:files).source_location is nil — nothing to jump to
  • it leans on private Rails behavior; if that forwarding is ever narrowed, the documented API breaks silently

This delegates the same names from the singleton class to instance, so they become real methods.

No behavior change: method_missing is untouched and still handles everything else, and delegating to instance (not config.dotenv) keeps the custom logger= replay logic on the path. Static analysis is one more thing that benefits, but the introspection/docs side is the reason.

load is left alone since it already has an explicit def self.load.

Tested: bundle exec rake (rspec + minitest + standard) green on Ruby 4.0 / Rails 7.1. Added one spec asserting the methods are defined on the singleton class and that a class-level write reaches the instance; it fails without the change.

The options documented in the README (`Dotenv::Rails.files`, `.overwrite`,
`.logger`, `.autorestore` and their writers) are configured on the class, but
`delegate` only defines them on the railtie instance. They work today solely
because `Rails::Railtie` privately forwards unknown class methods to
`instance` via `method_missing`.

That indirection means the gem's documented public API has no definition
anywhere: it is absent from `Dotenv::Rails.methods`, `method(:files)` has no
source location, so doc generators and editor completion can't see it, and
the API depends on private Rails behavior that could be narrowed without
notice.

Delegating the same names from the singleton class to `instance` makes them
real methods. `method_missing` remains in place as the fallback for
everything else, and delegating to `instance` (rather than `config.dotenv`)
keeps the custom `logger=` replay logic on the path, so behavior is
unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant