class Dotenv::Rails
Rails integration for using Dotenv to load ENV variables from a file
def self.load
Rails uses `#method_missing` to delegate all class methods to the
def self.load instance.load end
def deprecator # :nodoc:
def deprecator # :nodoc: @deprecator ||= ActiveSupport::Deprecation.new end
def env
check which rake tasks are being run to determine the environment.
When running `rake`, the Rails application is initialized in development, so we have to
The current environment that the app is running in.
def env @env ||= if defined?(Rake.application) && Rake.application.top_level_tasks.grep(TEST_RAKE_TASKS).any? env = Rake.application.options.show_tasks ? "development" : "test" ActiveSupport::EnvironmentInquirer.new(env) else ::Rails.env end end
def initialize
def initialize super config.dotenv = ActiveSupport::OrderedOptions.new.update( # Rails.logger is not available yet, so we'll save log messages and replay them when it is logger: Dotenv::ReplayLogger.new, overwrite: false, files: [ ".env.#{env}.local", (".env.local" unless env.test?), ".env.#{env}", ".env" ].compact, autorestore: env.test? && !defined?(ClimateControl) && !defined?(IceAge) ) end
def load
This will get called during the `before_configuration` callback, but you
Public: Load dotenv
def load Dotenv.load(*files.map { |file| root.join(file).to_s }, overwrite: overwrite) end
def logger=(new_logger)
def logger=(new_logger) logger.replay new_logger if logger.is_a?(ReplayLogger) config.dotenv.logger = new_logger end
def overload
def overload deprecator.warn("Dotenv::Rails.overload is deprecated. Set `Dotenv::Rails.overwrite = true` and call Dotenv::Rails.load instead.") Dotenv.load(*files.map { |file| root.join(file).to_s }, overwrite: true) end
def root
initialized, so this falls back to the `RAILS_ROOT` environment variable,
Internal: `Rails.root` is nil in Rails 4.1 before the application is
def root ::Rails.root || Pathname.new(ENV["RAILS_ROOT"] || Dir.pwd) end