forked from bmizerany/sinatra-activerecord
-
Notifications
You must be signed in to change notification settings - Fork 113
/
activerecord.rb
56 lines (47 loc) · 1.64 KB
/
activerecord.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'sinatra/base'
require 'active_record'
require 'active_support/core_ext/hash/keys'
require 'logger'
require 'pathname'
require 'yaml'
require 'erb'
module Sinatra
module ActiveRecordHelper
def database
settings.database
end
end
module ActiveRecordExtension
def self.registered(app)
app.set :database_file, "#{Dir.pwd}/config/database.yml" if File.exist?("#{Dir.pwd}/config/database.yml")
app.set :database, ENV['DATABASE_URL'] if ENV['DATABASE_URL']
unless defined?(Rake) || [:test, :production].include?(app.settings.environment)
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
app.helpers ActiveRecordHelper
# re-connect if database connection dropped
app.before { ActiveRecord::Base.verify_active_connections! if ActiveRecord::Base.respond_to?(:verify_active_connections!) }
app.after { ActiveRecord::Base.clear_active_connections! }
end
def database_file=(path)
path = File.join(root, path) if Pathname(path).relative? and root
spec = YAML.load(ERB.new(File.read(path)).result) || {}
set :database, spec
end
def database=(spec)
if spec.is_a?(Hash) and spec.symbolize_keys[environment.to_sym]
ActiveRecord::Base.configurations = spec.stringify_keys
ActiveRecord::Base.establish_connection(environment.to_sym)
else
ActiveRecord::Base.establish_connection(spec)
ActiveRecord::Base.configurations = {
environment.to_s => ActiveRecord::Base.connection.pool.spec.config
}
end
end
def database
ActiveRecord::Base
end
end
register ActiveRecordExtension
end