Permalink
Browse files

Support Rails-style database.yml files, if you're into that kind of t…

…hing
  • Loading branch information...
1 parent 8e30fc7 commit c54c946b3e995c1ecfa59c2d6f4526a5746c2487 @mpalmer committed Dec 26, 2011
Showing with 23 additions and 3 deletions.
  1. +12 −0 README.md
  2. +11 −3 lib/sinatra/activerecord.rb
View
@@ -87,6 +87,18 @@ path"):
sqlite:////home/foo/db/bar.db
+### Using a YAML config file
+
+If you want to store your database environment configurations in a YAML
+file, Rails-style, simply tell your app all about it:
+
+ set :database_config_yaml, "/path/to/some/file.yaml"
+
+And SAR will load the file and configure ActiveRecord appropriately. It
+will automatically key off your pre-existing `:environment` variable to
+decide which database to connect to.
+
+
### NOTE about the rip-off
This Code and README.md is a heavy adaption of [rtomayko's sinatra-sequel](http://github.com/rtomayko/sinatra-sequel/)
@@ -3,6 +3,7 @@
require 'sinatra/base'
require 'active_record'
require 'logger'
+require 'yaml'
module Sinatra
module ActiveRecordHelper
@@ -20,13 +21,21 @@ def database=(url)
def database
@database ||= (
- url = URI(database_url)
ActiveRecord::Base.logger = activerecord_logger
- ActiveRecord::Base.configurations[environment.to_s] = database_options
+ unless database_url.nil?
+ url = URI(database_url)
+ ActiveRecord::Base.configurations[environment.to_s] = database_options
+ end
+
ActiveRecord::Base.establish_connection(environment.to_s)
ActiveRecord::Base
)
end
+
+ def database_config_yaml=(yaml)
+ ActiveRecord::Base.configurations = YAML.load_file(yaml)
+ set :database_url, nil
+ end
protected
@@ -53,7 +62,6 @@ def self.registered(app)
app.set :database_url, lambda { ENV['DATABASE_URL'] || "sqlite://#{environment}.db" }
app.set :database_extras, Hash.new
app.set :activerecord_logger, Logger.new(STDOUT)
- app.database # force connection
app.helpers ActiveRecordHelper
end
end

0 comments on commit c54c946

Please sign in to comment.