Define Dotenv::Rails class-level options explicitly - #551
Open
mokevnin wants to merge 1 commit into
Open
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The options the README documents —
Dotenv::Rails.files,.overwrite,.logger,.autorestore(and their writers) — are set on the class, butdelegate ... to: "config.dotenv"only defines them on the railtie instance. They currently work becauseRails::Railtieprivately forwards unknown class methods toinstancethroughmethod_missing.So the documented public API has no definition anywhere:
Dotenv::Rails.methods, so doc generators and editor completion can't find itDotenv::Rails.method(:files).source_locationisnil— nothing to jump toThis delegates the same names from the singleton class to
instance, so they become real methods.No behavior change:
method_missingis untouched and still handles everything else, and delegating toinstance(notconfig.dotenv) keeps the customlogger=replay logic on the path. Static analysis is one more thing that benefits, but the introspection/docs side is the reason.loadis left alone since it already has an explicitdef 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.