diff options
author | Matthew Draper <matthew@trebex.net> | 2014-04-08 14:49:03 +0930 |
---|---|---|
committer | Matthew Draper <matthew@trebex.net> | 2014-04-08 15:15:05 +0930 |
commit | de9f2f63b8548e2a8950c1727f0a1a6893713505 (patch) | |
tree | d6c14eba49b60db8c1bfe349f1f0c96990917110 /activerecord/lib | |
parent | 0ec047533a1d1ed14e001cfe7fdee818c0c18124 (diff) | |
download | rails-de9f2f63b8548e2a8950c1727f0a1a6893713505.tar.gz rails-de9f2f63b8548e2a8950c1727f0a1a6893713505.tar.bz2 rails-de9f2f63b8548e2a8950c1727f0a1a6893713505.zip |
Rearrange the config merger some more
This seems to simplify the operative part. Most importantly, by
pre-loading all the configs supplied in ENV, we ensure the list is
complete: if the developer specifies an unknown config, the exception
includes a list of valid ones.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/connection_handling.rb | 35 |
1 files changed, 16 insertions, 19 deletions
diff --git a/activerecord/lib/active_record/connection_handling.rb b/activerecord/lib/active_record/connection_handling.rb index 1c8fbcb625..f83829bb26 100644 --- a/activerecord/lib/active_record/connection_handling.rb +++ b/activerecord/lib/active_record/connection_handling.rb @@ -60,6 +60,7 @@ module ActiveRecord class MergeAndResolveDefaultUrlConfig # :nodoc: def initialize(raw_configurations) @raw_config = raw_configurations.dup + @env = DEFAULT_ENV.call.to_s end # Returns fully resolved connection hashes. @@ -70,30 +71,26 @@ module ActiveRecord private def config - env = DEFAULT_ENV.call.to_s - - cfg = Hash.new do |hash, key| - entry = @raw_config[key] - env_url = nil - - if key.to_s == env - env_url = ENV["DATABASE_URL"] + @raw_config.dup.tap do |cfg| + urls_in_environment.each do |key, url| + cfg[key] ||= {} + cfg[key]["url"] ||= url end + end + end - env_url ||= ENV["DATABASE_URL_#{key.upcase}"] - - if env_url - entry ||= {} - entry.merge!("url" => env_url) { |h, v1, v2| v1 || v2 } + def urls_in_environment + {}.tap do |mapping| + ENV.each do |k, v| + if k =~ /\ADATABASE_URL_(.*)/ + mapping[$1.downcase] = v + end end - hash[key] = entry if entry + # Check for this last, because it is prioritised over the + # longer "DATABASE_URL_#{@env}" spelling + mapping[@env] = ENV['DATABASE_URL'] if ENV['DATABASE_URL'] end - - @raw_config.keys.each {|k| cfg[k] } - cfg[env] - - cfg end end |