Permalink
Browse files

cleanup establishing connections for MasterDatabase/SlaveDatabase abs…

…tract models
  • Loading branch information...
1 parent 88dee1a commit 14da49b7321d67418d82e7759dc1585b29b557ae @mislav committed Mar 20, 2009
Showing with 14 additions and 5 deletions.
  1. +14 −5 lib/active_reload/connection_proxy.rb
@@ -1,19 +1,27 @@
module ActiveReload
class MasterDatabase < ActiveRecord::Base
self.abstract_class = true
- establish_connection configurations[Rails.env]['master_database'] || configurations['master_database'] || configurations[Rails.env]
+
+ if config = configurations[Rails.env]['master_database'] || configurations['master_database']
+ establish_connection config
+ end
end
class SlaveDatabase < ActiveRecord::Base
self.abstract_class = true
+
def self.name
ActiveRecord::Base.name
end
- establish_connection configurations[Rails.env]['slave_database'] || configurations['slave_database'] || configurations[Rails.env]
+
+ if config = configurations[Rails.env]['slave_database'] || configurations['slave_database']
+ establish_connection config
+ end
end
-
+
+ # replaces the object at ActiveRecord::Base.connection to route read queries
+ # to slave and writes to master database
class ConnectionProxy
-
def initialize(master_class, slave_class)
@master = master_class
@slave = slave_class
@@ -41,7 +49,8 @@ def self.setup!
end
def self.slave_defined?
- ActiveRecord::Base.configurations[Rails.env]['slave_database']
+ configurations = ActiveRecord::Base.configurations
+ configurations[Rails.env]['slave_database'] or configurations['slave_database']
end
def self.setup_for(master, slave = nil)

0 comments on commit 14da49b

Please sign in to comment.