Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
16 changes: 12 additions & 4 deletions lib/sinatra/activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'sinatra/base'
require 'active_record'
require 'logger'
require 'yaml'

module Sinatra
module ActiveRecordHelper
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion sinatra-activerecord.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Empty file added test/foo.db
Empty file.
12 changes: 11 additions & 1 deletion test/sinatra_activerecord_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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