diff --git a/README.md b/README.md index e754be8..0c9c3cd 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,31 @@ the following code in about anywhere and it'll work just fine: erb :foos end +### Database file locations + +Giving a path to an SQLite database via a URL is slightly painful... far too +many slashes are needed. If you want to give a relative path, then you must +use three slashes before your path name: + + sqlite:///db/foo.db + +An absolute path has *four* slashes (an extra one to indicate "absolute +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/) diff --git a/lib/sinatra/activerecord.rb b/lib/sinatra/activerecord.rb index af8a4d5..baf6c41 100644 --- a/lib/sinatra/activerecord.rb +++ b/lib/sinatra/activerecord.rb @@ -3,6 +3,7 @@ require 'sinatra/base' require 'active_record' require 'logger' +require 'yaml' module Sinatra module ActiveRecordHelper @@ -20,12 +21,21 @@ def database=(url) def database @database ||= ( - url = URI(database_url) ActiveRecord::Base.logger = activerecord_logger - ActiveRecord::Base.establish_connection(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 @@ -42,7 +52,6 @@ def database_options case url.scheme when "sqlite" options[:adapter] = "sqlite3" - options[:database] = url.host when "postgres" options[:adapter] = "postgresql" end @@ -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 diff --git a/sinatra-activerecord.gemspec b/sinatra-activerecord.gemspec index 1989fec..1d71450 100644 --- a/sinatra-activerecord.gemspec +++ b/sinatra-activerecord.gemspec @@ -24,7 +24,6 @@ Gem::Specification.new do |s| s.extra_rdoc_files = %w[README.md] s.add_dependency 'sinatra', '>= 0.9.4' - s.has_rdoc = true s.homepage = "http://github.com/rtomayko/sinatra-activerecord" s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Sinatra::ActiveRecord"] s.require_paths = %w[lib] diff --git a/test/foo.db b/test/foo.db new file mode 100644 index 0000000..e69de29 diff --git a/test/sinatra_activerecord_test.rb b/test/sinatra_activerecord_test.rb index 1bf2f26..7bc8f9c 100644 --- a/test/sinatra_activerecord_test.rb +++ b/test/sinatra_activerecord_test.rb @@ -1,5 +1,6 @@ +require 'test/unit' require 'sinatra/base' -require 'sinatra/activerecord' +require File.expand_path('../../lib/sinatra/activerecord', __FILE__) class MockSinatraApp < Sinatra::Base register Sinatra::ActiveRecordExtension @@ -32,4 +33,13 @@ def test_establishes_a_database_connection_when_set assert @app.database.respond_to? :table_exists? end + def test_db_urls_with_a_path + @app.database = 'sqlite:///test/foo.db' + assert_equal 'test/foo.db', @app.database.connection.instance_variable_get(:@config)[:database] + end + + def test_db_urls_with_absolute_path + @app.database = 'sqlite:////tmp/foo.db' + assert_equal '/tmp/foo.db', @app.database.connection.instance_variable_get(:@config)[:database] + end end