# Patches the database_configuration method to not evaluate ERB in production
# mode for security purposes.  Also, adds support to handle encrypted
# password fields as strings without ERB.
#
# The original implementation is as follows:
#
#    def database_configuration
#      require 'erb'
#      YAML::load(ERB.new(IO.read(paths["config/database"].first)).result)
#    end
module Rails
  class Application
    class Configuration
      def database_configuration
        data = IO.read(paths["config/database"].first)
        Vmdb::ConfigurationEncoder.load(data, false)
      end
    end
  end
end

# Patches the database_configuration method to not evaluate ERB in production
# mode for security purposes.  Also, adds support to handle encrypted
# password fields as strings without ERB.
#
# The original implementation is as follows:
#
#    def database_configuration
#      require 'erb'
#      YAML::load(ERB.new(IO.read(paths["config/database"].first)).result)
#    end
require 'rails/application/configuration'
Rails::Application::Configuration.module_eval do
  # sorry for the multi line {}
  # block needs to be associated with module not prepend
  prepend Module.new {
    def database_configuration
      path = paths["config/database"].existent.first
      yaml = Pathname.new(path) if path

      if yaml && yaml.exist?
        Vmdb::ConfigurationEncoder.load(IO.read(yaml), false)
      else
        super
      end
    end
  }
end

Fix rails asset precompile without database.yml
Fix rails asset precompile without database.yml

We currently override Rails `database_configuration`
This new version more closely mimics the rails [behavior], allowing
us to precompile assets without a `database.yml` file.

In our case, it respects ENV['DATABASE_URL'].

Fixes #3285

[behavior]: https://github.com/rails/rails/blob/master/railties/lib/rails/application/configuration.rb#L87-L101
