Skip to content

Commit

Permalink
Join files to Rails.root at load time
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Feb 14, 2024
1 parent dd40e1c commit 229cfc3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/dotenv/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions spec/dotenv/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 229cfc3

Please sign in to comment.