-
Notifications
You must be signed in to change notification settings - Fork 76
/
database.rb
40 lines (35 loc) · 1.62 KB
/
database.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
# Log queries to STDOUT in development
ActiveRecord::Base.logger = Logger.new(STDOUT) if Sinatra::Application.development?
# Automatically load every file in APP_ROOT/app/models/*.rb, e.g.,
# autoload "Person", 'app/models/person.rb'
#
# We have to do this in case we have models that inherit from each other.
# If model Student inherits from model Person and app/models/student.rb is
# required first, it will throw an error saying "Person" is undefined.
#
# With this lazy-loading technique, Ruby will try to load app/models/person.rb
# the first time it sees "Person" and will only throw an exception if
# that file doesn't define the Person class.
#
# See http://www.rubyinside.com/ruby-techniques-revealed-autoload-1652.html
Dir[APP_ROOT.join('app', 'models', '*.rb')].each do |model_file|
filename = File.basename(model_file).gsub('.rb', '')
autoload ActiveSupport::Inflector.camelize(filename), model_file
end
# Heroku controls what database we connect to by setting the DATABASE_URL environment variable
# We need to respect that if we want our Sinatra apps to run on Heroku without modification
db = URI.parse(ENV['DATABASE_URL'] || "postgres://localhost/#{APP_NAME}_#{Sinatra::Application.environment}")
DB_NAME = db.path[1..-1]
# Note:
# Sinatra::Application.environment is set to the value of ENV['RACK_ENV']
# if ENV['RACK_ENV'] is set. If ENV['RACK_ENV'] is not set, it defaults
# to :development
ActiveRecord::Base.establish_connection(
adapter: db.scheme == 'postgres' ? 'postgresql' : db.scheme,
host: db.host,
port: db.port,
username: db.user,
password: db.password,
database: DB_NAME,
encoding: 'utf8'
)