rails.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. require "dotenv"
  2. Dotenv.instrumenter = ActiveSupport::Notifications
  3. # Watch all loaded env files with Spring
  4. begin
  5. require "spring/watcher"
  6. ActiveSupport::Notifications.subscribe(/^dotenv/) do |*args|
  7. event = ActiveSupport::Notifications::Event.new(*args)
  8. Spring.watch event.payload[:env].filename if Rails.application
  9. end
  10. rescue LoadError
  11. # Spring is not available
  12. end
  13. module Dotenv
  14. # Dotenv Railtie for using Dotenv to load environment from a file into
  15. # Rails applications
  16. class Railtie < Rails::Railtie
  17. config.before_configuration { load }
  18. # Public: Load dotenv
  19. #
  20. # This will get called during the `before_configuration` callback, but you
  21. # can manually call `Dotenv::Railtie.load` if you needed it sooner.
  22. def load
  23. Dotenv.load(
  24. root.join(".env.local"),
  25. root.join(".env.#{Rails.env}"),
  26. root.join(".env")
  27. )
  28. end
  29. # Internal: `Rails.root` is nil in Rails 4.1 before the application is
  30. # initialized, so this falls back to the `RAILS_ROOT` environment variable,
  31. # or the current working directory.
  32. def root
  33. Rails.root || Pathname.new(ENV["RAILS_ROOT"] || Dir.pwd)
  34. end
  35. # Rails uses `#method_missing` to delegate all class methods to the
  36. # instance, which means `Kernel#load` gets called here. We don't want that.
  37. def self.load
  38. instance.load
  39. end
  40. end
  41. end