diff --git a/lib/dotenv/rails.rb b/lib/dotenv/rails.rb index f5b004dd..4a3ebecf 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 febec319..248ccf00 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