Permalink
Browse files

allow some more flexibility on when exactly the proxy is setup

git-svn-id: http://ar-code.svn.engineyard.com/plugins/masochism@31 d98f8484-0aa7-43af-80ea-302c351499d6
  • Loading branch information...
1 parent 3942270 commit 720142f99ad2049d15ea4bd2583f8b8f6914c233 rick committed Oct 6, 2007
Showing with 58 additions and 5 deletions.
  1. +18 −0 LICENSE
  2. +28 −0 README
  3. +0 −2 init.rb
  4. +12 −3 lib/active_reload/connection_proxy.rb
View
18 LICENSE
@@ -0,0 +1,18 @@
+Copyright (c) 2007 Rick Olson
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
28 README
@@ -0,0 +1,28 @@
+masochism
+==============
+
+Connection proxy sends some queries (thoes in a transaction, update statements,
+and ActiveRecord::Base#reload) to a master database, and the rest to the slave
+database. The ActiveReload::MasterDatabase model uses a 'master_database'
+setting in database.yml to serve as the master database.
+
+To setup:
+
+Whether you want this in production only, or maybe just your deployment server,
+is up to you. Just call this method somewhere:
+
+ ActiveReload::ConnectionProxy.setup!
+
+Some suggestions:
+
+ * in a config/initializer
+ * config.after_initialize block
+
+If you want a model to always use the Master database, you can inherit
+ActiveRelaod::MasterDatabase. Any models with their own database connection
+will not be affected.
+
+Setting up your own proxies:
+
+ # Sets up MyMaster's connection as the master database connection for User.
+ ActiveReload::ConnectionProxy.setup_for MyMaster, User
View
@@ -1,2 +0,0 @@
-require 'active_reload/connection_proxy'
-ActiveRecord::Base.active_connections[ActiveRecord::Base.name] = ActiveReload::ConnectionProxy.new
@@ -5,14 +5,23 @@ class MasterDatabase < ActiveRecord::Base
end
class ConnectionProxy
- def initialize
- @slave = ActiveRecord::Base.connection
- @master = ActiveReload::MasterDatabase.connection
+ def initialize(master, slave)
+ @slave = slave.connection
+ @master = master.connection
@current = @slave
end
attr_accessor :slave, :master
+ def self.setup!
+ setup_for ActiveReload::MasterDatabase
+ end
+
+ def self.setup_for(master, slave = nil)
+ slave ||= ActiveRecord::Base
+ ActiveRecord::Base.active_connections[slave.name] = new(master, slave)
+ end
+
def with_master
set_to_master!
yield

0 comments on commit 720142f

Please sign in to comment.