Overriding Rails 3 database configuration depending upon platform

Software Engineering 1717 views

This is uself if you develop on one platform, and have to deploy to a different plaform. For example, I want to develop on my Mac OS X laptop, but am restricted to deploying to a Windows Server 2008 server.

Place this file in something line lib/extensions/configuration_extensions.rb:

# lib/extenstions/configuration_extentions.rb

module Rails
  class Application
    class Configuration

      def database_configuration
        require 'erb'
        case RUBY_PLATFORM.downcase
          when /darwin/
            YAML::load(ERB.new(IO.read('config/database_mac.yml')).result)
          when /mswin/, /ming/
            YAML::load(ERB.new(IO.read('config/database_win.yml')).result)
          when /java/
            YAML::load(ERB.new(IO.read('config/database_java.yml')).result)
          else
            YAML::load(ERB.new(IO.read(paths["config/database"].first)).result)
        end
      end
      
    end
  end
end

And update your config/environment.rb file:

# config/environment.rb

require 'extensions/configuration_extensions'

# Initialize the rails application