From 229cfc342cbe822c6a9903fc721d911f6ee9df26 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Wed, 14 Feb 2024 10:42:09 -0500 Subject: [PATCH] Join files to Rails.root at load time --- lib/dotenv/rails.rb | 15 ++++++++++----- spec/dotenv/rails_spec.rb | 11 +++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/dotenv/rails.rb b/lib/dotenv/rails.rb index f5b004d..4a3ebec 100644 --- a/lib/dotenv/rails.rb +++ b/lib/dotenv/rails.rb @@ -18,7 +18,7 @@ module Dotenv # Rails integration for using Dotenv to load ENV variables from a file class Rails < ::Rails::Railtie - delegate :files, :files=, :overwrite, :overwrite=, :autorestore, :autorestore=, :logger, :logger=, to: "config.dotenv" + delegate :files=, :overwrite, :overwrite=, :autorestore, :autorestore=, :logger, :logger=, to: "config.dotenv" def initialize super() @@ -27,15 +27,20 @@ def initialize logger: Dotenv::ReplayLogger.new, overwrite: false, files: [ - root.join(".env.#{env}.local"), - (root.join(".env.local") unless env.test?), - root.join(".env.#{env}"), - root.join(".env") + ".env.#{env}.local", + (".env.local" unless env.test?), + ".env.#{env}", + ".env" ].compact, autorestore: env.test? ) end + # The list of files to load, joined with Rails.root + def files + config.dotenv.files.map { |file| root.join(file) } + end + # Public: Load dotenv # # This will get called during the `before_configuration` callback, but you diff --git a/spec/dotenv/rails_spec.rb b/spec/dotenv/rails_spec.rb index febec31..248ccf0 100644 --- a/spec/dotenv/rails_spec.rb +++ b/spec/dotenv/rails_spec.rb @@ -65,6 +65,17 @@ ] ) end + + it "returns the relatives paths to Rails.root" do + expect(Dotenv::Rails.files.last).to eql(fixture_path(".env")) + allow(Rails).to receive(:root).and_return(Pathname.new("/tmp")) + expect(Dotenv::Rails.files.last.to_s).to eql("/tmp/.env") + end + + it "returns absolute paths unchanged" do + Dotenv::Rails.files = ["/tmp/.env"] + expect(Dotenv::Rails.files).to eql([Pathname.new("/tmp/.env")]) + end end it "watches other loaded files with Spring" do