Skip to content

Commit

Permalink
Use an existing ActiveRecord connection if one is around
Browse files Browse the repository at this point in the history
  • Loading branch information
erithmetic committed Feb 10, 2011
1 parent f4c2689 commit ea53877
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
7 changes: 5 additions & 2 deletions lib/moneta/adapters/active_record.rb
Expand Up @@ -13,9 +13,12 @@ def initialize(options = {})
@options = options
unless self.class.const_defined?('Store')
self.class.const_set('Store', Class.new(::ActiveRecord::Base)) # this prevents loading issues when active_record gem is unavailable
Store.set_table_name(@options[:table] || 'moneta_store')
end

if @options[:connection]
Store.establish_connection @options[:connection]
end
Store.establish_connection(@options[:connection] || raise("Must specify :connection"))
Store.set_table_name(@options[:table] || 'moneta_store')
end

def migrate
Expand Down
34 changes: 25 additions & 9 deletions spec/moneta_active_record_spec.rb
Expand Up @@ -4,18 +4,34 @@

if defined?(ActiveRecord)
describe 'Moneta::ActiveRecord' do
before(:each) do
@cache = Moneta::Adapters::ActiveRecord.new(:connection => {
:adapter => 'sqlite3',
:database => 'reports_test.sqlite3'
})
@cache.migrate
@cache.clear
end
after :all do
FileUtils.rm_f File.expand_path('../reports_test.sqlite3', File.dirname(__FILE__))
end

it_should_behave_like "a read/write Moneta cache"
context 'with connection option set' do
before(:each) do
@cache = Moneta::Adapters::ActiveRecord.new(:connection => {
:adapter => 'sqlite3',
:database => 'reports_test.sqlite3'
})
@cache.migrate
@cache.clear
end

it_should_behave_like "a read/write Moneta cache"
end

context 'using preexisting ActiveRecord connection' do
describe '#initialize' do
it 'uses an existing connection' do
ActiveRecord::Base.establish_connection :adapter => 'sqlite3',
:database => 'reports_test.sqlite3'

cache = Moneta::Adapters::ActiveRecord.new
cache.migrate
Moneta::Adapters::ActiveRecord::Store.table_exists?.should be_true
end
end
end
end
end

0 comments on commit ea53877

Please sign in to comment.