diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-11-28 11:06:59 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-11-28 15:23:24 -0800 |
commit | 2a9a8ad4dfb2609a2275c1a3540ad2768562a026 (patch) | |
tree | 6f7336876924864a3d478405992960f249797de8 /activerecord/lib | |
parent | d1afd987464717f8af1ab0e9a78af6f37b9ce425 (diff) | |
download | rails-2a9a8ad4dfb2609a2275c1a3540ad2768562a026.tar.gz rails-2a9a8ad4dfb2609a2275c1a3540ad2768562a026.tar.bz2 rails-2a9a8ad4dfb2609a2275c1a3540ad2768562a026.zip |
break establish_connection to smaller methods
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb | 70 |
1 files changed, 40 insertions, 30 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb index 3d0f146fed..e335ebd9c2 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb @@ -56,37 +56,47 @@ module ActiveRecord # may be returned on an error. def self.establish_connection(spec = ENV["DATABASE_URL"]) case spec - when nil - raise AdapterNotSpecified unless defined?(Rails.env) - establish_connection(Rails.env) - when ConnectionSpecification - self.connection_handler.establish_connection(name, spec) - when Symbol, String - if configuration = configurations[spec.to_s] - establish_connection(configuration) - elsif spec.is_a?(String) && hash = connection_url_to_hash(spec) - establish_connection(hash) - else - raise AdapterNotSpecified, "#{spec} database is not configured" - end - else - spec = spec.symbolize_keys - unless spec.key?(:adapter) then raise AdapterNotSpecified, "database configuration does not specify adapter" end - - begin - require "active_record/connection_adapters/#{spec[:adapter]}_adapter" - rescue LoadError => e - raise "Please install the #{spec[:adapter]} adapter: `gem install activerecord-#{spec[:adapter]}-adapter` (#{e})" - end - - adapter_method = "#{spec[:adapter]}_connection" - unless respond_to?(adapter_method) - raise AdapterNotFound, "database configuration specifies nonexistent #{spec[:adapter]} adapter" - end - - remove_connection - establish_connection(ConnectionSpecification.new(spec, adapter_method)) + when nil + raise AdapterNotSpecified unless defined?(Rails.env) + spec = resolve_string_connection Rails.env + when Symbol, String + spec = resolve_string_connection spec.to_s + when Hash + spec = resolve_hash_connection spec end + + if ConnectionSpecification === spec + return self.connection_handler.establish_connection(name, spec) + end + end + + def self.resolve_string_connection(spec) # :nodoc: + if configuration = configurations[spec] + spec = resolve_hash_connection(configuration) + elsif hash = connection_url_to_hash(spec) + spec = resolve_hash_connection(hash) + else + raise AdapterNotSpecified, "#{spec} database is not configured" + end + end + + def self.resolve_hash_connection(spec) # :nodoc: + spec = spec.symbolize_keys + unless spec.key?(:adapter) then raise AdapterNotSpecified, "database configuration does not specify adapter" end + + begin + require "active_record/connection_adapters/#{spec[:adapter]}_adapter" + rescue LoadError => e + raise LoadError, "Please install the #{spec[:adapter]} adapter: `gem install activerecord-#{spec[:adapter]}-adapter` (#{e.message})", e.backtrace + end + + adapter_method = "#{spec[:adapter]}_connection" + unless respond_to?(adapter_method) + raise AdapterNotFound, "database configuration specifies nonexistent #{spec[:adapter]} adapter" + end + + remove_connection + ConnectionSpecification.new(spec, adapter_method) end def self.connection_url_to_hash(url) # :nodoc: |