otr-activerecord 1.4.1 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -4
- data/lib/otr-activerecord/activerecord.rb +42 -9
- data/lib/otr-activerecord/version.rb +1 -1
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd820c260dab9b0cbef1548223fd8c7173c5cf2995e133e32db0ce2c90c3c427
|
4
|
+
data.tar.gz: 2a170bd9fcaee46df1a0255180cc765eb4145d95fcb1ca7daf63a786cd43518d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c736dd5202985ab80f22318fbdab901c769e009ebc5dfb6b3de3c3be133f767e530222c231000bdef783c9fb3628cdbe69b5c1c3e9191bd1e3d1d5606debf198
|
7
|
+
data.tar.gz: 5e5bfb8062b5ab2508b5057a5b8e222a5f0034a96a3e0a9f632c74ae2e53ba05350c8d30bf84b2e47051deea66a3094dd62721afb6e4d6b731135d1491cea44b
|
data/README.md
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
An easy way to use ActiveRecord "off the rails." Works with Grape, Sinatra, plain old Rack, or even in a boring little script!. The defaults are all very Railsy (`config/database.yml`, `db/seeds.rb`, `db/migrate`, etc.), but you can easily change them. (Formerly known as `grape-activerecord`.) Supports:
|
4
4
|
|
5
|
-
* ActiveRecord 4
|
6
|
-
* ActiveRecord 5
|
7
5
|
* ActiveRecord 6
|
6
|
+
* ActiveRecord 5
|
7
|
+
* ActiveRecord 4
|
8
8
|
|
9
9
|
## How to use
|
10
10
|
|
@@ -23,7 +23,19 @@ After loading your gems, tell `OTR::ActiveRecord` about your database config usi
|
|
23
23
|
**Important note**: `configure_from_file!` won't work as expected if you have already `DATABASE_URL` set as part of your environment variables.
|
24
24
|
This is because in ActiveRecord when that env variable is set it will merge its properties into the current connection configuration.
|
25
25
|
|
26
|
-
#### 3.
|
26
|
+
#### 3. Connect to your database(s)
|
27
|
+
|
28
|
+
If you have a single database (most apps), use this helper:
|
29
|
+
|
30
|
+
OTR::ActiveRecord.establish_connection!
|
31
|
+
|
32
|
+
If you're using multiple databases, call your base class(es) instead:
|
33
|
+
|
34
|
+
MyBase.establish_connection :primary
|
35
|
+
MyBase.establish_connection :primary_replica
|
36
|
+
...
|
37
|
+
|
38
|
+
#### 4. Enable middleware for Rack apps
|
27
39
|
|
28
40
|
Add these middlewares in `config.ru`:
|
29
41
|
|
@@ -33,7 +45,7 @@ Add these middlewares in `config.ru`:
|
|
33
45
|
# Enable ActiveRecord's QueryCache for every request (optional)
|
34
46
|
use OTR::ActiveRecord::QueryCache
|
35
47
|
|
36
|
-
####
|
48
|
+
#### 5. Import ActiveRecord tasks into your Rakefile
|
37
49
|
|
38
50
|
This will give you most of the standard `db:` tasks you get in Rails. Add it to your `Rakefile`.
|
39
51
|
|
@@ -26,24 +26,57 @@ module OTR
|
|
26
26
|
def self.configure_from_hash!(spec)
|
27
27
|
config = spec.stringify_keys.merge("migrations_paths" => ::OTR::ActiveRecord.migrations_paths)
|
28
28
|
::ActiveRecord::Base.configurations = {rack_env.to_s => config}
|
29
|
-
::ActiveRecord::Base.establish_connection(rack_env)
|
30
29
|
end
|
31
30
|
|
32
31
|
# Connect to database with a DB URL. Example: "postgres://user:pass@localhost/db"
|
33
32
|
def self.configure_from_url!(url)
|
34
|
-
|
33
|
+
require 'uri'
|
34
|
+
uri = URI(url)
|
35
|
+
spec = {"adapter" => uri.scheme}
|
36
|
+
|
37
|
+
case spec["adapter"]
|
38
|
+
when /^sqlite/i
|
39
|
+
spec["database"] = url =~ /::memory:/ ? ":memory:" : "#{uri.host}#{uri.path}"
|
40
|
+
else
|
41
|
+
spec["host"] = uri.host if uri.host
|
42
|
+
spec["port"] = uri.port if uri.port
|
43
|
+
spec["database"] = uri.path.sub(/^\//, "")
|
44
|
+
spec["username"] = uri.user if uri.user
|
45
|
+
spec["password"] = uri.password if uri.password
|
46
|
+
end
|
47
|
+
|
48
|
+
if uri.query
|
49
|
+
opts_ary = URI.decode_www_form(uri.query)
|
50
|
+
opts = Hash[opts_ary]
|
51
|
+
spec.merge!(opts)
|
52
|
+
end
|
53
|
+
|
54
|
+
configure_from_hash! spec
|
35
55
|
end
|
36
56
|
|
37
57
|
# Connect to database with a yml file. Example: "config/database.yml"
|
38
58
|
def self.configure_from_file!(path)
|
39
59
|
raise "#{path} does not exist!" unless File.file? path
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
60
|
+
result =(YAML.safe_load(ERB.new(File.read(path)).result, [], [], true) || {})
|
61
|
+
::ActiveRecord::Base.configurations = begin
|
62
|
+
result.each do |_env, config|
|
63
|
+
if config.all? { |_, v| v.is_a?(Hash) }
|
64
|
+
config.each { |_, v| append_migration_path(v) }
|
65
|
+
else
|
66
|
+
append_migration_path(config)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Establish a connection to the given db (defaults to current rack env)
|
73
|
+
def self.establish_connection!(db = rack_env)
|
74
|
+
::ActiveRecord::Base.establish_connection(db)
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.append_migration_path(config)
|
78
|
+
config['migrations_paths'] = ::OTR::ActiveRecord.migrations_paths unless config.key?('migrations_paths')
|
79
|
+
config
|
47
80
|
end
|
48
81
|
|
49
82
|
# The current Rack environment
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: otr-activerecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jordan Hollinger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '4.0'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '6.
|
22
|
+
version: '6.3'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '4.0'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '6.
|
32
|
+
version: '6.3'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: hashie-forbidden_attributes
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -82,8 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
82
|
- !ruby/object:Gem::Version
|
83
83
|
version: '0'
|
84
84
|
requirements: []
|
85
|
-
|
86
|
-
rubygems_version: 2.7.6.2
|
85
|
+
rubygems_version: 3.0.3
|
87
86
|
signing_key:
|
88
87
|
specification_version: 4
|
89
88
|
summary: 'Off The Rails: Use ActiveRecord with Grape, Sinatra, Rack, or anything else!'
|