activerecord_url_connections 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
@@ -2,14 +2,23 @@ require "active_record"
|
|
2
2
|
module ActiveRecordURLConnections
|
3
3
|
def self.parse(str)
|
4
4
|
config = URI.parse(str)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
5
|
+
adapter = config.scheme
|
6
|
+
adapter = "postgresql" if adapter == "postgres"
|
7
|
+
spec = { :adapter => adapter,
|
8
|
+
:username => config.user,
|
9
|
+
:password => config.password,
|
10
|
+
:port => config.port,
|
11
|
+
:database => config.path.sub(%r{^/},""),
|
12
|
+
:hostname => config.host }
|
13
|
+
spec.reject!{ |key,value| value.nil? }
|
14
|
+
spec.merge!(split_query_options(config.query))
|
15
|
+
spec
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def self.split_query_options(query = nil)
|
20
|
+
return {} unless query
|
21
|
+
Hash[query.split("&").map{ |pair| pair.split("=") }].symbolize_keys
|
13
22
|
end
|
14
23
|
end
|
15
24
|
|
@@ -19,26 +28,11 @@ ActiveRecord::Base.class_eval do
|
|
19
28
|
def establish_connection(spec = nil)
|
20
29
|
spec ||= ENV["DATABASE_URL"]
|
21
30
|
if spec.is_a?(String) && url = URI.parse(spec)
|
22
|
-
|
23
|
-
adapter = "postgresql" if adapter == "postgres"
|
24
|
-
spec = { :adapter => adapter,
|
25
|
-
:username => url.user,
|
26
|
-
:password => url.password,
|
27
|
-
:database => url.path.sub(%r{^/},""),
|
28
|
-
:port => url.port,
|
29
|
-
:hostname => url.host }
|
30
|
-
spec.reject!{ |key,value| value.nil? }
|
31
|
-
spec.merge!(split_query_options(url.query))
|
31
|
+
spec = ActiveRecordURLConnections.parse(spec)
|
32
32
|
end
|
33
33
|
establish_connection_without_url(spec)
|
34
34
|
rescue URI::InvalidURIError
|
35
35
|
establish_connection_without_url(spec)
|
36
36
|
end
|
37
|
-
|
38
|
-
private
|
39
|
-
def split_query_options(query = nil)
|
40
|
-
return {} unless query
|
41
|
-
Hash[query.split("&").map{ |pair| pair.split("=") }].symbolize_keys
|
42
|
-
end
|
43
37
|
end
|
44
38
|
end
|